1
0
mirror of synced 2024-11-12 04:18:52 +01:00
mat2/src/images.py

76 lines
2.9 KiB
Python
Raw Normal View History

2018-03-25 15:09:12 +02:00
import subprocess
import json
2018-04-01 00:43:36 +02:00
import os
2018-03-25 15:09:12 +02:00
import cairo
2018-03-25 15:09:12 +02:00
import gi
gi.require_version('GdkPixbuf', '2.0')
from gi.repository import GdkPixbuf
from . import abstract
class PNGParser(abstract.AbstractParser):
mimetypes = {'image/png', }
meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName',
'Directory', 'FileSize', 'FileModifyDate', 'FileAccessDate',
"FileInodeChangeDate", 'FilePermissions', 'FileType',
'FileTypeExtension', 'MIMEType', 'ImageWidth', 'BitDepth', 'ColorType',
'Compression', 'Filter', 'Interlace', 'BackgroundColor', 'ImageSize',
'Megapixels', 'ImageHeight'}
def get_meta(self):
out = subprocess.check_output(['exiftool', '-json', self.filename])
meta = json.loads(out.decode('utf-8'))[0]
for key in self.meta_whitelist:
meta.pop(key, None)
return meta
def remove_all(self):
surface = cairo.ImageSurface.create_from_png(self.filename)
surface.write_to_png(self.output_filename)
return True
2018-04-01 00:43:36 +02:00
class GdkPixbufAbstractParser(abstract.AbstractParser):
2018-04-02 23:40:08 +02:00
""" GdkPixbuf can handle a lot of surfaces, so we're rending images on it,
this has the side-effect of removing metadata completely.
"""
2018-03-25 15:09:12 +02:00
def get_meta(self):
out = subprocess.check_output(['exiftool', '-json', self.filename])
2018-03-25 17:42:29 +02:00
meta = json.loads(out.decode('utf-8'))[0]
2018-03-25 15:09:12 +02:00
for key in self.meta_whitelist:
meta.pop(key, None)
return meta
def remove_all(self):
2018-04-01 00:43:36 +02:00
_, extension = os.path.splitext(self.filename)
2018-03-25 15:09:12 +02:00
pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename)
2018-04-01 00:43:36 +02:00
if extension == '.jpg':
extension = '.jpeg'
pixbuf.savev(self.output_filename, extension[1:], ["quality"], ["100"])
2018-03-25 15:09:12 +02:00
return True
2018-04-01 00:43:36 +02:00
class JPGParser(GdkPixbufAbstractParser):
mimetypes = {'image/jpeg'}
2018-04-01 00:43:36 +02:00
meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName',
'Directory', 'FileSize', 'FileModifyDate', 'FileAccessDate',
"FileInodeChangeDate", 'FilePermissions', 'FileType',
'FileTypeExtension', 'MIMEType', 'ImageWidth',
'ImageSize', 'BitsPerSample', 'ColorComponents', 'EncodingProcess',
'JFIFVersion', 'ResolutionUnit', 'XResolution', 'YCbCrSubSampling',
'YResolution', 'Megapixels', 'ImageHeight'}
class TiffParser(GdkPixbufAbstractParser):
mimetypes = {'image/tiff'}
meta_whitelist = {'Compression', 'ExifByteOrder', 'ExtraSamples',
'FillOrder', 'PhotometricInterpretation', 'PlanarConfiguration',
'RowsPerStrip', 'SamplesPerPixel', 'StripByteCounts',
'StripOffsets', 'BitsPerSample', 'Directory', 'ExifToolVersion',
'FileAccessDate', 'FileInodeChangeDate', 'FileModifyDate',
'FileName', 'FilePermissions', 'FileSize', 'FileType',
'FileTypeExtension', 'ImageHeight', 'ImageSize', 'ImageWidth',
'MIMEType', 'Megapixels', 'SourceFile'}