diff options
| author | jvoisin | 2018-04-01 12:30:00 +0200 |
|---|---|---|
| committer | jvoisin | 2018-04-01 12:30:00 +0200 |
| commit | 27beda354d8b78c1716e659273c180d4ddfb144b (patch) | |
| tree | 5420d509f2538f742405771d8433e81ce2984148 /src/images.py | |
| parent | 711347c87f189a4fd1bd425144934016b79f099c (diff) | |
Move every image-related parser into a single file
Diffstat (limited to 'src/images.py')
| -rw-r--r-- | src/images.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/images.py b/src/images.py new file mode 100644 index 0000000..560886a --- /dev/null +++ b/src/images.py | |||
| @@ -0,0 +1,72 @@ | |||
| 1 | import subprocess | ||
| 2 | import json | ||
| 3 | import os | ||
| 4 | |||
| 5 | import cairo | ||
| 6 | |||
| 7 | import gi | ||
| 8 | gi.require_version('GdkPixbuf', '2.0') | ||
| 9 | from gi.repository import GdkPixbuf | ||
| 10 | |||
| 11 | from . import abstract | ||
| 12 | |||
| 13 | class PNGParser(abstract.AbstractParser): | ||
| 14 | mimetypes = {'image/png', } | ||
| 15 | meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', | ||
| 16 | 'Directory', 'FileSize', 'FileModifyDate', 'FileAccessDate', | ||
| 17 | "FileInodeChangeDate", 'FilePermissions', 'FileType', | ||
| 18 | 'FileTypeExtension', 'MIMEType', 'ImageWidth', 'BitDepth', 'ColorType', | ||
| 19 | 'Compression', 'Filter', 'Interlace', 'BackgroundColor', 'ImageSize', | ||
| 20 | 'Megapixels', 'ImageHeight'} | ||
| 21 | |||
| 22 | def get_meta(self): | ||
| 23 | out = subprocess.check_output(['exiftool', '-json', self.filename]) | ||
| 24 | meta = json.loads(out.decode('utf-8'))[0] | ||
| 25 | for key in self.meta_whitelist: | ||
| 26 | meta.pop(key, None) | ||
| 27 | return meta | ||
| 28 | |||
| 29 | def remove_all(self): | ||
| 30 | surface = cairo.ImageSurface.create_from_png(self.filename) | ||
| 31 | surface.write_to_png(self.output_filename) | ||
| 32 | return True | ||
| 33 | |||
| 34 | class GdkPixbufAbstractParser(abstract.AbstractParser): | ||
| 35 | def get_meta(self): | ||
| 36 | out = subprocess.check_output(['exiftool', '-json', self.filename]) | ||
| 37 | meta = json.loads(out.decode('utf-8'))[0] | ||
| 38 | for key in self.meta_whitelist: | ||
| 39 | meta.pop(key, None) | ||
| 40 | return meta | ||
| 41 | |||
| 42 | def remove_all(self): | ||
| 43 | _, extension = os.path.splitext(self.filename) | ||
| 44 | pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename) | ||
| 45 | if extension == '.jpg': | ||
| 46 | extension = '.jpeg' | ||
| 47 | pixbuf.savev(self.output_filename, extension[1:], ["quality"], ["100"]) | ||
| 48 | return True | ||
| 49 | |||
| 50 | |||
| 51 | class JPGParser(GdkPixbufAbstractParser): | ||
| 52 | mimetypes = {'image/jpg'} | ||
| 53 | meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', | ||
| 54 | 'Directory', 'FileSize', 'FileModifyDate', 'FileAccessDate', | ||
| 55 | "FileInodeChangeDate", 'FilePermissions', 'FileType', | ||
| 56 | 'FileTypeExtension', 'MIMEType', 'ImageWidth', | ||
| 57 | 'ImageSize', 'BitsPerSample', 'ColorComponents', 'EncodingProcess', | ||
| 58 | 'JFIFVersion', 'ResolutionUnit', 'XResolution', 'YCbCrSubSampling', | ||
| 59 | 'YResolution', 'Megapixels', 'ImageHeight'} | ||
| 60 | |||
| 61 | |||
| 62 | class TiffParser(GdkPixbufAbstractParser): | ||
| 63 | mimetypes = {'image/tiff'} | ||
| 64 | meta_whitelist = {'Compression', 'ExifByteOrder', 'ExtraSamples', | ||
| 65 | 'FillOrder', 'PhotometricInterpretation', 'PlanarConfiguration', | ||
| 66 | 'RowsPerStrip', 'SamplesPerPixel', 'StripByteCounts', | ||
| 67 | 'StripOffsets', 'BitsPerSample', 'Directory', 'ExifToolVersion', | ||
| 68 | 'FileAccessDate', 'FileInodeChangeDate', 'FileModifyDate', | ||
| 69 | 'FileName', 'FilePermissions', 'FileSize', 'FileType', | ||
| 70 | 'FileTypeExtension', 'ImageHeight', 'ImageSize', 'ImageWidth', | ||
| 71 | 'MIMEType', 'Megapixels', 'SourceFile'} | ||
| 72 | |||
