diff options
| -rw-r--r-- | src/images_pixbuf.py (renamed from src/jpg.py) | 41 | ||||
| -rw-r--r-- | tests/data/dirty.tiff | bin | 0 -> 936524 bytes | |||
| -rw-r--r-- | tests/test_libmat2.py | 30 |
3 files changed, 56 insertions, 15 deletions
diff --git a/src/jpg.py b/src/images_pixbuf.py index 34fc04c..8eeffbe 100644 --- a/src/jpg.py +++ b/src/images_pixbuf.py | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | import subprocess | 1 | import subprocess |
| 2 | import json | 2 | import json |
| 3 | import os | ||
| 3 | 4 | ||
| 4 | import gi | 5 | import gi |
| 5 | gi.require_version('GdkPixbuf', '2.0') | 6 | gi.require_version('GdkPixbuf', '2.0') |
| @@ -7,16 +8,7 @@ from gi.repository import GdkPixbuf | |||
| 7 | 8 | ||
| 8 | from . import abstract | 9 | from . import abstract |
| 9 | 10 | ||
| 10 | class JPGParser(abstract.AbstractParser): | 11 | class GdkPixbufAbstractParser(abstract.AbstractParser): |
| 11 | mimetypes = {'image/jpg', } | ||
| 12 | meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', | ||
| 13 | 'Directory', 'FileSize', 'FileModifyDate', 'FileAccessDate', | ||
| 14 | "FileInodeChangeDate", 'FilePermissions', 'FileType', | ||
| 15 | 'FileTypeExtension', 'MIMEType', 'ImageWidth', | ||
| 16 | 'ImageSize', 'BitsPerSample', 'ColorComponents', 'EncodingProcess', | ||
| 17 | 'JFIFVersion', 'ResolutionUnit', 'XResolution', 'YCbCrSubSampling', | ||
| 18 | 'YResolution', 'Megapixels', 'ImageHeight'} | ||
| 19 | |||
| 20 | def get_meta(self): | 12 | def get_meta(self): |
| 21 | out = subprocess.check_output(['exiftool', '-json', self.filename]) | 13 | out = subprocess.check_output(['exiftool', '-json', self.filename]) |
| 22 | meta = json.loads(out.decode('utf-8'))[0] | 14 | meta = json.loads(out.decode('utf-8'))[0] |
| @@ -25,6 +17,33 @@ class JPGParser(abstract.AbstractParser): | |||
| 25 | return meta | 17 | return meta |
| 26 | 18 | ||
| 27 | def remove_all(self): | 19 | def remove_all(self): |
| 20 | _, extension = os.path.splitext(self.filename) | ||
| 28 | pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename) | 21 | pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename) |
| 29 | pixbuf.savev(self.output_filename, "jpeg", ["quality"], ["100"]) | 22 | if extension == '.jpg': |
| 23 | extension = '.jpeg' | ||
| 24 | pixbuf.savev(self.output_filename, extension[1:], ["quality"], ["100"]) | ||
| 30 | return True | 25 | return True |
| 26 | |||
| 27 | |||
| 28 | class JPGParser(GdkPixbufAbstractParser): | ||
| 29 | mimetypes = {'image/jpg'} | ||
| 30 | meta_whitelist = {'SourceFile', 'ExifToolVersion', 'FileName', | ||
| 31 | 'Directory', 'FileSize', 'FileModifyDate', 'FileAccessDate', | ||
| 32 | "FileInodeChangeDate", 'FilePermissions', 'FileType', | ||
| 33 | 'FileTypeExtension', 'MIMEType', 'ImageWidth', | ||
| 34 | 'ImageSize', 'BitsPerSample', 'ColorComponents', 'EncodingProcess', | ||
| 35 | 'JFIFVersion', 'ResolutionUnit', 'XResolution', 'YCbCrSubSampling', | ||
| 36 | 'YResolution', 'Megapixels', 'ImageHeight'} | ||
| 37 | |||
| 38 | |||
| 39 | class TiffParser(GdkPixbufAbstractParser): | ||
| 40 | mimetypes = {'image/tiff'} | ||
| 41 | meta_whitelist = {'Compression', 'ExifByteOrder', 'ExtraSamples', | ||
| 42 | 'FillOrder', 'PhotometricInterpretation', 'PlanarConfiguration', | ||
| 43 | 'RowsPerStrip', 'SamplesPerPixel', 'StripByteCounts', | ||
| 44 | 'StripOffsets', 'BitsPerSample', 'Directory', 'ExifToolVersion', | ||
| 45 | 'FileAccessDate', 'FileInodeChangeDate', 'FileModifyDate', | ||
| 46 | 'FileName', 'FilePermissions', 'FileSize', 'FileType', | ||
| 47 | 'FileTypeExtension', 'ImageHeight', 'ImageSize', 'ImageWidth', | ||
| 48 | 'MIMEType', 'Megapixels', 'SourceFile'} | ||
| 49 | |||
diff --git a/tests/data/dirty.tiff b/tests/data/dirty.tiff new file mode 100644 index 0000000..05b97ad --- /dev/null +++ b/tests/data/dirty.tiff | |||
| Binary files differ | |||
diff --git a/tests/test_libmat2.py b/tests/test_libmat2.py index 888c782..89e690e 100644 --- a/tests/test_libmat2.py +++ b/tests/test_libmat2.py | |||
| @@ -6,7 +6,7 @@ import os | |||
| 6 | import zipfile | 6 | import zipfile |
| 7 | import tempfile | 7 | import tempfile |
| 8 | 8 | ||
| 9 | from src import pdf, png, jpg, audio, office, libreoffice, parser_factory | 9 | from src import pdf, png, images_pixbuf, audio, office, libreoffice, parser_factory |
| 10 | 10 | ||
| 11 | class TestGetMeta(unittest.TestCase): | 11 | class TestGetMeta(unittest.TestCase): |
| 12 | def test_pdf(self): | 12 | def test_pdf(self): |
| @@ -22,10 +22,17 @@ class TestGetMeta(unittest.TestCase): | |||
| 22 | self.assertEqual(meta['ModifyDate'], "2018:03:20 21:59:25") | 22 | self.assertEqual(meta['ModifyDate'], "2018:03:20 21:59:25") |
| 23 | 23 | ||
| 24 | def test_jpg(self): | 24 | def test_jpg(self): |
| 25 | p = jpg.JPGParser('./tests/data/dirty.jpg') | 25 | p = images_pixbuf.JPGParser('./tests/data/dirty.jpg') |
| 26 | meta = p.get_meta() | 26 | meta = p.get_meta() |
| 27 | self.assertEqual(meta['Comment'], 'Created with GIMP') | 27 | self.assertEqual(meta['Comment'], 'Created with GIMP') |
| 28 | 28 | ||
| 29 | def test_tiff(self): | ||
| 30 | p = images_pixbuf.JPGParser('./tests/data/dirty.tiff') | ||
| 31 | meta = p.get_meta() | ||
| 32 | self.assertEqual(meta['Make'], 'OLYMPUS IMAGING CORP.') | ||
| 33 | self.assertEqual(meta['Model'], 'C7070WZ') | ||
| 34 | self.assertEqual(meta['ModifyDate'], '2005:12:26 17:09:35') | ||
| 35 | |||
| 29 | def test_mp3(self): | 36 | def test_mp3(self): |
| 30 | p = audio.MP3Parser('./tests/data/dirty.mp3') | 37 | p = audio.MP3Parser('./tests/data/dirty.mp3') |
| 31 | meta = p.get_meta() | 38 | meta = p.get_meta() |
| @@ -152,7 +159,7 @@ class TestCleaning(unittest.TestCase): | |||
| 152 | 159 | ||
| 153 | def test_jpg(self): | 160 | def test_jpg(self): |
| 154 | shutil.copy('./tests/data/dirty.jpg', './tests/data/clean.jpg') | 161 | shutil.copy('./tests/data/dirty.jpg', './tests/data/clean.jpg') |
| 155 | p = jpg.JPGParser('./tests/data/clean.jpg') | 162 | p = images_pixbuf.JPGParser('./tests/data/clean.jpg') |
| 156 | 163 | ||
| 157 | meta = p.get_meta() | 164 | meta = p.get_meta() |
| 158 | self.assertEqual(meta['Comment'], 'Created with GIMP') | 165 | self.assertEqual(meta['Comment'], 'Created with GIMP') |
| @@ -160,7 +167,7 @@ class TestCleaning(unittest.TestCase): | |||
| 160 | ret = p.remove_all() | 167 | ret = p.remove_all() |
| 161 | self.assertTrue(ret) | 168 | self.assertTrue(ret) |
| 162 | 169 | ||
| 163 | p = jpg.JPGParser('./tests/data/clean.jpg.cleaned') | 170 | p = images_pixbuf.JPGParser('./tests/data/clean.jpg.cleaned') |
| 164 | self.assertEqual(p.get_meta(), {}) | 171 | self.assertEqual(p.get_meta(), {}) |
| 165 | 172 | ||
| 166 | os.remove('./tests/data/clean.jpg') | 173 | os.remove('./tests/data/clean.jpg') |
| @@ -240,3 +247,18 @@ class TestCleaning(unittest.TestCase): | |||
| 240 | self.assertEqual(p.get_meta(), {}) | 247 | self.assertEqual(p.get_meta(), {}) |
| 241 | 248 | ||
| 242 | os.remove('./tests/data/clean.odt') | 249 | os.remove('./tests/data/clean.odt') |
| 250 | |||
| 251 | def test_tiff(self): | ||
| 252 | shutil.copy('./tests/data/dirty.tiff', './tests/data/clean.tiff') | ||
| 253 | p = images_pixbuf.TiffParser('./tests/data/clean.tiff') | ||
| 254 | |||
| 255 | meta = p.get_meta() | ||
| 256 | self.assertEqual(meta['Model'], 'C7070WZ') | ||
| 257 | |||
| 258 | ret = p.remove_all() | ||
| 259 | self.assertTrue(ret) | ||
| 260 | |||
| 261 | p = images_pixbuf.TiffParser('./tests/data/clean.tiff.cleaned') | ||
| 262 | self.assertEqual(p.get_meta(), {}) | ||
| 263 | |||
| 264 | os.remove('./tests/data/clean.tiff') | ||
