1
0
mirror of synced 2024-11-22 09:14:23 +01:00

Add support for jpeg

This commit is contained in:
jvoisin 2018-03-25 15:09:12 +02:00
parent 7ad9ff08ad
commit d4d6f31655
3 changed files with 52 additions and 1 deletions

30
src/parsers/jpg.py Normal file
View File

@ -0,0 +1,30 @@
import subprocess
import json
import gi
gi.require_version('GdkPixbuf', '2.0')
from gi.repository import GdkPixbuf
from . import abstract
class JPGParser(abstract.AbstractParser):
mimetypes = {'image/jpg', }
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'}
def get_meta(self):
out = subprocess.check_output(['exiftool', '-json', self.filename])
meta = json.loads(out)[0]
for key in self.meta_whitelist:
meta.pop(key, None)
return meta
def remove_all(self):
pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename)
pixbuf.savev(self.output_filename, "jpeg", ["quality"], ["100"])
return True

BIN
tests/data/dirty.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 136 KiB

View File

@ -5,7 +5,7 @@ import shutil
import os import os
from src import parsers from src import parsers
from src.parsers import pdf, png from src.parsers import pdf, png, jpg
class TestGetMeta(unittest.TestCase): class TestGetMeta(unittest.TestCase):
def test_pdf(self): def test_pdf(self):
@ -20,6 +20,11 @@ class TestGetMeta(unittest.TestCase):
self.assertEqual(meta['Comment'], 'This is a comment, be careful!') self.assertEqual(meta['Comment'], 'This is a comment, be careful!')
self.assertEqual(meta['ModifyDate'], "2018:03:20 21:59:25") self.assertEqual(meta['ModifyDate'], "2018:03:20 21:59:25")
def test_jpg(self):
p = jpg.JPGParser('./tests/data/dirty.jpg')
meta = p.get_meta()
self.assertEqual(meta['Comment'], 'Created with GIMP')
class TestCleaning(unittest.TestCase): class TestCleaning(unittest.TestCase):
def test_pdf(self): def test_pdf(self):
shutil.copy('./tests/data/dirty.pdf', './tests/data/clean.pdf') shutil.copy('./tests/data/dirty.pdf', './tests/data/clean.pdf')
@ -51,3 +56,19 @@ class TestCleaning(unittest.TestCase):
self.assertEqual(p.get_meta(), {}) self.assertEqual(p.get_meta(), {})
os.remove('./tests/data/clean.png') os.remove('./tests/data/clean.png')
def test_jpg(self):
shutil.copy('./tests/data/dirty.jpg', './tests/data/clean.jpg')
p = jpg.JPGParser('./tests/data/clean.jpg')
meta = p.get_meta()
self.assertEqual(meta['Comment'], 'Created with GIMP')
ret = p.remove_all()
self.assertTrue(ret)
p = jpg.JPGParser('./tests/data/clean.jpg.cleaned')
self.assertEqual(p.get_meta(), {})
os.remove('./tests/data/clean.jpg')