diff options
| author | jvoisin | 2018-03-25 15:09:12 +0200 |
|---|---|---|
| committer | jvoisin | 2018-03-25 15:09:12 +0200 |
| commit | d4d6f31655fb578825dbf1e639f165c7ad3eb17f (patch) | |
| tree | 65a5a64e2d0cf9dab964785efb1c50ce2af5eae0 | |
| parent | 7ad9ff08ad52d16b05e689ef383e395b7731d594 (diff) | |
Add support for jpeg
| -rw-r--r-- | src/parsers/jpg.py | 30 | ||||
| -rw-r--r-- | tests/data/dirty.jpg | bin | 0 -> 139309 bytes | |||
| -rw-r--r-- | tests/test_libmat2.py | 23 |
3 files changed, 52 insertions, 1 deletions
diff --git a/src/parsers/jpg.py b/src/parsers/jpg.py new file mode 100644 index 0000000..d1a4439 --- /dev/null +++ b/src/parsers/jpg.py | |||
| @@ -0,0 +1,30 @@ | |||
| 1 | import subprocess | ||
| 2 | import json | ||
| 3 | |||
| 4 | import gi | ||
| 5 | gi.require_version('GdkPixbuf', '2.0') | ||
| 6 | from gi.repository import GdkPixbuf | ||
| 7 | |||
| 8 | from . import abstract | ||
| 9 | |||
| 10 | class JPGParser(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): | ||
| 21 | out = subprocess.check_output(['exiftool', '-json', self.filename]) | ||
| 22 | meta = json.loads(out)[0] | ||
| 23 | for key in self.meta_whitelist: | ||
| 24 | meta.pop(key, None) | ||
| 25 | return meta | ||
| 26 | |||
| 27 | def remove_all(self): | ||
| 28 | pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename) | ||
| 29 | pixbuf.savev(self.output_filename, "jpeg", ["quality"], ["100"]) | ||
| 30 | return True | ||
diff --git a/tests/data/dirty.jpg b/tests/data/dirty.jpg new file mode 100644 index 0000000..15ca271 --- /dev/null +++ b/tests/data/dirty.jpg | |||
| Binary files differ | |||
diff --git a/tests/test_libmat2.py b/tests/test_libmat2.py index 9305080..1d31695 100644 --- a/tests/test_libmat2.py +++ b/tests/test_libmat2.py | |||
| @@ -5,7 +5,7 @@ import shutil | |||
| 5 | import os | 5 | import os |
| 6 | 6 | ||
| 7 | from src import parsers | 7 | from src import parsers |
| 8 | from src.parsers import pdf, png | 8 | from src.parsers import pdf, png, jpg |
| 9 | 9 | ||
| 10 | class TestGetMeta(unittest.TestCase): | 10 | class TestGetMeta(unittest.TestCase): |
| 11 | def test_pdf(self): | 11 | def test_pdf(self): |
| @@ -20,6 +20,11 @@ class TestGetMeta(unittest.TestCase): | |||
| 20 | self.assertEqual(meta['Comment'], 'This is a comment, be careful!') | 20 | self.assertEqual(meta['Comment'], 'This is a comment, be careful!') |
| 21 | self.assertEqual(meta['ModifyDate'], "2018:03:20 21:59:25") | 21 | self.assertEqual(meta['ModifyDate'], "2018:03:20 21:59:25") |
| 22 | 22 | ||
| 23 | def test_jpg(self): | ||
| 24 | p = jpg.JPGParser('./tests/data/dirty.jpg') | ||
| 25 | meta = p.get_meta() | ||
| 26 | self.assertEqual(meta['Comment'], 'Created with GIMP') | ||
| 27 | |||
| 23 | class TestCleaning(unittest.TestCase): | 28 | class TestCleaning(unittest.TestCase): |
| 24 | def test_pdf(self): | 29 | def test_pdf(self): |
| 25 | shutil.copy('./tests/data/dirty.pdf', './tests/data/clean.pdf') | 30 | shutil.copy('./tests/data/dirty.pdf', './tests/data/clean.pdf') |
| @@ -51,3 +56,19 @@ class TestCleaning(unittest.TestCase): | |||
| 51 | self.assertEqual(p.get_meta(), {}) | 56 | self.assertEqual(p.get_meta(), {}) |
| 52 | 57 | ||
| 53 | os.remove('./tests/data/clean.png') | 58 | os.remove('./tests/data/clean.png') |
| 59 | |||
| 60 | |||
| 61 | def test_jpg(self): | ||
| 62 | shutil.copy('./tests/data/dirty.jpg', './tests/data/clean.jpg') | ||
| 63 | p = jpg.JPGParser('./tests/data/clean.jpg') | ||
| 64 | |||
| 65 | meta = p.get_meta() | ||
| 66 | self.assertEqual(meta['Comment'], 'Created with GIMP') | ||
| 67 | |||
| 68 | ret = p.remove_all() | ||
| 69 | self.assertTrue(ret) | ||
| 70 | |||
| 71 | p = jpg.JPGParser('./tests/data/clean.jpg.cleaned') | ||
| 72 | self.assertEqual(p.get_meta(), {}) | ||
| 73 | |||
| 74 | os.remove('./tests/data/clean.jpg') | ||
