summaryrefslogtreecommitdiff
path: root/libmat2/images.py
diff options
context:
space:
mode:
authorjvoisin2018-05-18 23:52:40 +0200
committerjvoisin2018-05-18 23:52:40 +0200
commit38fae60b8beaf9c7b37c65325d2d285e62b6cb85 (patch)
treee6bd4f699d6190dfada7618ebd04455eb7de9660 /libmat2/images.py
parent57d5cd04284276c49899034a9ad321b680624d8f (diff)
Rename some files to simplify packaging
- the `src` folder is now `libmat2` - the `main.py` script is now `mat2.py`
Diffstat (limited to 'libmat2/images.py')
-rw-r--r--libmat2/images.py101
1 files changed, 101 insertions, 0 deletions
diff --git a/libmat2/images.py b/libmat2/images.py
new file mode 100644
index 0000000..c84952a
--- /dev/null
+++ b/libmat2/images.py
@@ -0,0 +1,101 @@
1import subprocess
2import json
3import os
4
5import cairo
6
7import gi
8gi.require_version('GdkPixbuf', '2.0')
9from gi.repository import GdkPixbuf
10
11from . import abstract
12
13
14class PNGParser(abstract.AbstractParser):
15 mimetypes = {'image/png', }
16 meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName',
17 'Directory', 'FileSize', 'FileModifyDate',
18 'FileAccessDate', 'FileInodeChangeDate',
19 'FilePermissions', 'FileType', 'FileTypeExtension',
20 'MIMEType', 'ImageWidth', 'BitDepth', 'ColorType',
21 'Compression', 'Filter', 'Interlace', 'BackgroundColor',
22 'ImageSize', 'Megapixels', 'ImageHeight'}
23
24 def __init__(self, filename):
25 super().__init__(filename)
26 try: # better fail here than later
27 cairo.ImageSurface.create_from_png(self.filename)
28 except MemoryError:
29 raise ValueError
30
31 def get_meta(self):
32 out = subprocess.check_output(['/usr/bin/exiftool', '-json', self.filename])
33 meta = json.loads(out.decode('utf-8'))[0]
34 for key in self.meta_whitelist:
35 meta.pop(key, None)
36 return meta
37
38 def remove_all(self):
39 surface = cairo.ImageSurface.create_from_png(self.filename)
40 surface.write_to_png(self.output_filename)
41 return True
42
43
44class GdkPixbufAbstractParser(abstract.AbstractParser):
45 """ GdkPixbuf can handle a lot of surfaces, so we're rending images on it,
46 this has the side-effect of removing metadata completely.
47 """
48 def get_meta(self):
49 out = subprocess.check_output(['/usr/bin/exiftool', '-json', self.filename])
50 meta = json.loads(out.decode('utf-8'))[0]
51 for key in self.meta_whitelist:
52 meta.pop(key, None)
53 return meta
54
55 def remove_all(self):
56 _, extension = os.path.splitext(self.filename)
57 pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename)
58 if extension == '.jpg':
59 extension = '.jpeg'
60 pixbuf.savev(self.output_filename, extension[1:], [], [])
61 return True
62
63
64class JPGParser(GdkPixbufAbstractParser):
65 mimetypes = {'image/jpeg'}
66 meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName',
67 'Directory', 'FileSize', 'FileModifyDate',
68 'FileAccessDate', "FileInodeChangeDate",
69 'FilePermissions', 'FileType', 'FileTypeExtension',
70 'MIMEType', 'ImageWidth', 'ImageSize', 'BitsPerSample',
71 'ColorComponents', 'EncodingProcess', 'JFIFVersion',
72 'ResolutionUnit', 'XResolution', 'YCbCrSubSampling',
73 'YResolution', 'Megapixels', 'ImageHeight'}
74
75
76class TiffParser(GdkPixbufAbstractParser):
77 mimetypes = {'image/tiff'}
78 meta_whitelist = {'Compression', 'ExifByteOrder', 'ExtraSamples',
79 'FillOrder', 'PhotometricInterpretation',
80 'PlanarConfiguration', 'RowsPerStrip', 'SamplesPerPixel',
81 'StripByteCounts', 'StripOffsets', 'BitsPerSample',
82 'Directory', 'ExifToolVersion', 'FileAccessDate',
83 'FileInodeChangeDate', 'FileModifyDate', 'FileName',
84 'FilePermissions', 'FileSize', 'FileType',
85 'FileTypeExtension', 'ImageHeight', 'ImageSize',
86 'ImageWidth', 'MIMEType', 'Megapixels', 'SourceFile'}
87
88
89class BMPParser(GdkPixbufAbstractParser):
90 mimetypes = {'image/x-ms-bmp'}
91 meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', 'Directory',
92 'FileSize', 'FileModifyDate', 'FileAccessDate',
93 'FileInodeChangeDate', 'FilePermissions', 'FileType',
94 'FileTypeExtension', 'MIMEType', 'BMPVersion',
95 'ImageWidth', 'ImageHeight', 'Planes', 'BitDepth',
96 'Compression', 'ImageLength', 'PixelsPerMeterX',
97 'PixelsPerMeterY', 'NumColors', 'NumImportantColors',
98 'RedMask', 'GreenMask', 'BlueMask', 'AlphaMask',
99 'ColorSpace', 'RedEndpoint', 'GreenEndpoint',
100 'BlueEndpoint', 'GammaRed', 'GammaGreen', 'GammaBlue',
101 'ImageSize', 'Megapixels'}