Add support for .tiff files
This commit is contained in:
parent
c186fc4292
commit
2d7c703c52
@ -1,5 +1,6 @@
|
||||
import subprocess
|
||||
import json
|
||||
import os
|
||||
|
||||
import gi
|
||||
gi.require_version('GdkPixbuf', '2.0')
|
||||
@ -7,16 +8,7 @@ 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'}
|
||||
|
||||
class GdkPixbufAbstractParser(abstract.AbstractParser):
|
||||
def get_meta(self):
|
||||
out = subprocess.check_output(['exiftool', '-json', self.filename])
|
||||
meta = json.loads(out.decode('utf-8'))[0]
|
||||
@ -25,6 +17,33 @@ class JPGParser(abstract.AbstractParser):
|
||||
return meta
|
||||
|
||||
def remove_all(self):
|
||||
_, extension = os.path.splitext(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
|
||||
|
||||
|
||||
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
BIN
tests/data/dirty.tiff
Normal file
Binary file not shown.
@ -6,7 +6,7 @@ import os
|
||||
import zipfile
|
||||
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):
|
||||
def test_pdf(self):
|
||||
@ -22,10 +22,17 @@ class TestGetMeta(unittest.TestCase):
|
||||
self.assertEqual(meta['ModifyDate'], "2018:03:20 21:59:25")
|
||||
|
||||
def test_jpg(self):
|
||||
p = jpg.JPGParser('./tests/data/dirty.jpg')
|
||||
p = images_pixbuf.JPGParser('./tests/data/dirty.jpg')
|
||||
meta = p.get_meta()
|
||||
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):
|
||||
p = audio.MP3Parser('./tests/data/dirty.mp3')
|
||||
meta = p.get_meta()
|
||||
@ -152,7 +159,7 @@ class TestCleaning(unittest.TestCase):
|
||||
|
||||
def test_jpg(self):
|
||||
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()
|
||||
self.assertEqual(meta['Comment'], 'Created with GIMP')
|
||||
@ -160,7 +167,7 @@ class TestCleaning(unittest.TestCase):
|
||||
ret = p.remove_all()
|
||||
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(), {})
|
||||
|
||||
os.remove('./tests/data/clean.jpg')
|
||||
@ -240,3 +247,18 @@ class TestCleaning(unittest.TestCase):
|
||||
self.assertEqual(p.get_meta(), {})
|
||||
|
||||
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')
|
||||
|
Loading…
Reference in New Issue
Block a user