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

Add support for .tiff files

This commit is contained in:
jvoisin 2018-04-01 00:43:36 +02:00
parent c186fc4292
commit 2d7c703c52
3 changed files with 56 additions and 15 deletions

View File

@ -1,5 +1,6 @@
import subprocess import subprocess
import json import json
import os
import gi import gi
gi.require_version('GdkPixbuf', '2.0') gi.require_version('GdkPixbuf', '2.0')
@ -7,16 +8,7 @@ from gi.repository import GdkPixbuf
from . import abstract from . import abstract
class JPGParser(abstract.AbstractParser): class GdkPixbufAbstractParser(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): def get_meta(self):
out = subprocess.check_output(['exiftool', '-json', self.filename]) out = subprocess.check_output(['exiftool', '-json', self.filename])
meta = json.loads(out.decode('utf-8'))[0] meta = json.loads(out.decode('utf-8'))[0]
@ -25,6 +17,33 @@ class JPGParser(abstract.AbstractParser):
return meta return meta
def remove_all(self): def remove_all(self):
_, extension = os.path.splitext(self.filename)
pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename) pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename)
pixbuf.savev(self.output_filename, "jpeg", ["quality"], ["100"]) if extension == '.jpg':
extension = '.jpeg'
pixbuf.savev(self.output_filename, extension[1:], ["quality"], ["100"])
return True return True
class JPGParser(GdkPixbufAbstractParser):
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'}
class TiffParser(GdkPixbufAbstractParser):
mimetypes = {'image/tiff'}
meta_whitelist = {'Compression', 'ExifByteOrder', 'ExtraSamples',
'FillOrder', 'PhotometricInterpretation', 'PlanarConfiguration',
'RowsPerStrip', 'SamplesPerPixel', 'StripByteCounts',
'StripOffsets', 'BitsPerSample', 'Directory', 'ExifToolVersion',
'FileAccessDate', 'FileInodeChangeDate', 'FileModifyDate',
'FileName', 'FilePermissions', 'FileSize', 'FileType',
'FileTypeExtension', 'ImageHeight', 'ImageSize', 'ImageWidth',
'MIMEType', 'Megapixels', 'SourceFile'}

BIN
tests/data/dirty.tiff Normal file

Binary file not shown.

View File

@ -6,7 +6,7 @@ import os
import zipfile import zipfile
import tempfile import tempfile
from src import pdf, png, jpg, audio, office, libreoffice, parser_factory from src import pdf, png, images_pixbuf, audio, office, libreoffice, parser_factory
class TestGetMeta(unittest.TestCase): class TestGetMeta(unittest.TestCase):
def test_pdf(self): def test_pdf(self):
@ -22,10 +22,17 @@ class TestGetMeta(unittest.TestCase):
self.assertEqual(meta['ModifyDate'], "2018:03:20 21:59:25") self.assertEqual(meta['ModifyDate'], "2018:03:20 21:59:25")
def test_jpg(self): def test_jpg(self):
p = jpg.JPGParser('./tests/data/dirty.jpg') p = images_pixbuf.JPGParser('./tests/data/dirty.jpg')
meta = p.get_meta() meta = p.get_meta()
self.assertEqual(meta['Comment'], 'Created with GIMP') self.assertEqual(meta['Comment'], 'Created with GIMP')
def test_tiff(self):
p = images_pixbuf.JPGParser('./tests/data/dirty.tiff')
meta = p.get_meta()
self.assertEqual(meta['Make'], 'OLYMPUS IMAGING CORP.')
self.assertEqual(meta['Model'], 'C7070WZ')
self.assertEqual(meta['ModifyDate'], '2005:12:26 17:09:35')
def test_mp3(self): def test_mp3(self):
p = audio.MP3Parser('./tests/data/dirty.mp3') p = audio.MP3Parser('./tests/data/dirty.mp3')
meta = p.get_meta() meta = p.get_meta()
@ -152,7 +159,7 @@ class TestCleaning(unittest.TestCase):
def test_jpg(self): def test_jpg(self):
shutil.copy('./tests/data/dirty.jpg', './tests/data/clean.jpg') shutil.copy('./tests/data/dirty.jpg', './tests/data/clean.jpg')
p = jpg.JPGParser('./tests/data/clean.jpg') p = images_pixbuf.JPGParser('./tests/data/clean.jpg')
meta = p.get_meta() meta = p.get_meta()
self.assertEqual(meta['Comment'], 'Created with GIMP') self.assertEqual(meta['Comment'], 'Created with GIMP')
@ -160,7 +167,7 @@ class TestCleaning(unittest.TestCase):
ret = p.remove_all() ret = p.remove_all()
self.assertTrue(ret) self.assertTrue(ret)
p = jpg.JPGParser('./tests/data/clean.jpg.cleaned') p = images_pixbuf.JPGParser('./tests/data/clean.jpg.cleaned')
self.assertEqual(p.get_meta(), {}) self.assertEqual(p.get_meta(), {})
os.remove('./tests/data/clean.jpg') os.remove('./tests/data/clean.jpg')
@ -240,3 +247,18 @@ class TestCleaning(unittest.TestCase):
self.assertEqual(p.get_meta(), {}) self.assertEqual(p.get_meta(), {})
os.remove('./tests/data/clean.odt') os.remove('./tests/data/clean.odt')
def test_tiff(self):
shutil.copy('./tests/data/dirty.tiff', './tests/data/clean.tiff')
p = images_pixbuf.TiffParser('./tests/data/clean.tiff')
meta = p.get_meta()
self.assertEqual(meta['Model'], 'C7070WZ')
ret = p.remove_all()
self.assertTrue(ret)
p = images_pixbuf.TiffParser('./tests/data/clean.tiff.cleaned')
self.assertEqual(p.get_meta(), {})
os.remove('./tests/data/clean.tiff')