2018-06-22 20:38:29 +02:00
|
|
|
import imghdr
|
2018-04-01 00:43:36 +02:00
|
|
|
import os
|
2019-09-01 18:28:46 +02:00
|
|
|
import re
|
|
|
|
from typing import Set, Dict, Union, Any
|
2018-03-25 15:09:12 +02:00
|
|
|
|
2018-04-01 12:30:00 +02:00
|
|
|
import cairo
|
|
|
|
|
2018-03-25 15:09:12 +02:00
|
|
|
import gi
|
|
|
|
gi.require_version('GdkPixbuf', '2.0')
|
2019-07-13 21:26:05 +02:00
|
|
|
gi.require_version('Rsvg', '2.0')
|
|
|
|
from gi.repository import GdkPixbuf, GLib, Rsvg
|
2018-03-25 15:09:12 +02:00
|
|
|
|
2019-09-01 18:28:46 +02:00
|
|
|
from . import exiftool, abstract
|
2018-03-25 15:09:12 +02:00
|
|
|
|
2018-07-08 22:40:36 +02:00
|
|
|
# Make pyflakes happy
|
|
|
|
assert Set
|
2019-09-01 18:28:46 +02:00
|
|
|
assert Any
|
2018-04-04 23:21:48 +02:00
|
|
|
|
2019-07-13 21:26:05 +02:00
|
|
|
class SVGParser(exiftool.ExiftoolParser):
|
|
|
|
mimetypes = {'image/svg+xml', }
|
|
|
|
meta_allowlist = {'Directory', 'ExifToolVersion', 'FileAccessDate',
|
|
|
|
'FileInodeChangeDate', 'FileModifyDate', 'FileName',
|
|
|
|
'FilePermissions', 'FileSize', 'FileType',
|
|
|
|
'FileTypeExtension', 'ImageHeight', 'ImageWidth',
|
|
|
|
'MIMEType', 'SVGVersion', 'SourceFile', 'ViewBox'
|
|
|
|
}
|
|
|
|
|
|
|
|
def remove_all(self) -> bool:
|
|
|
|
svg = Rsvg.Handle.new_from_file(self.filename)
|
|
|
|
dimensions = svg.get_dimensions()
|
|
|
|
surface = cairo.SVGSurface(self.output_filename,
|
|
|
|
dimensions.height,
|
|
|
|
dimensions.width)
|
|
|
|
context = cairo.Context(surface)
|
|
|
|
svg.render_cairo(context)
|
|
|
|
surface.finish()
|
|
|
|
return True
|
|
|
|
|
|
|
|
def get_meta(self) -> Dict[str, Union[str, dict]]:
|
|
|
|
meta = super().get_meta()
|
|
|
|
|
2019-07-22 23:20:37 +02:00
|
|
|
# The namespace is mandatory, but only the …/2000/svg is valid.
|
2019-07-13 21:26:05 +02:00
|
|
|
ns = 'http://www.w3.org/2000/svg'
|
|
|
|
if meta.get('Xmlns', ns) == ns:
|
|
|
|
meta.pop('Xmlns')
|
|
|
|
return meta
|
|
|
|
|
2018-10-18 19:19:56 +02:00
|
|
|
class PNGParser(exiftool.ExiftoolParser):
|
2018-04-01 12:30:00 +02:00
|
|
|
mimetypes = {'image/png', }
|
2019-02-20 00:45:27 +01:00
|
|
|
meta_allowlist = {'SourceFile', 'ExifToolVersion', 'FileName',
|
2018-05-16 22:36:59 +02:00
|
|
|
'Directory', 'FileSize', 'FileModifyDate',
|
|
|
|
'FileAccessDate', 'FileInodeChangeDate',
|
|
|
|
'FilePermissions', 'FileType', 'FileTypeExtension',
|
|
|
|
'MIMEType', 'ImageWidth', 'BitDepth', 'ColorType',
|
|
|
|
'Compression', 'Filter', 'Interlace', 'BackgroundColor',
|
|
|
|
'ImageSize', 'Megapixels', 'ImageHeight'}
|
2018-04-01 12:30:00 +02:00
|
|
|
|
2018-05-06 21:58:31 +02:00
|
|
|
def __init__(self, filename):
|
|
|
|
super().__init__(filename)
|
2018-09-09 19:09:05 +02:00
|
|
|
|
|
|
|
if imghdr.what(filename) != 'png':
|
|
|
|
raise ValueError
|
|
|
|
|
2018-05-06 21:58:31 +02:00
|
|
|
try: # better fail here than later
|
|
|
|
cairo.ImageSurface.create_from_png(self.filename)
|
2018-09-12 14:54:54 +02:00
|
|
|
except MemoryError: # pragma: no cover
|
2018-05-06 21:58:31 +02:00
|
|
|
raise ValueError
|
|
|
|
|
2018-10-12 11:58:01 +02:00
|
|
|
def remove_all(self) -> bool:
|
2018-10-23 16:14:21 +02:00
|
|
|
if self.lightweight_cleaning:
|
|
|
|
return self._lightweight_cleanup()
|
2018-04-01 12:30:00 +02:00
|
|
|
surface = cairo.ImageSurface.create_from_png(self.filename)
|
|
|
|
surface.write_to_png(self.output_filename)
|
|
|
|
return True
|
|
|
|
|
2018-04-04 23:21:48 +02:00
|
|
|
|
2019-02-03 21:01:58 +01:00
|
|
|
class GIFParser(exiftool.ExiftoolParser):
|
|
|
|
mimetypes = {'image/gif'}
|
2019-02-20 00:45:27 +01:00
|
|
|
meta_allowlist = {'AnimationIterations', 'BackgroundColor', 'BitsPerPixel',
|
2019-02-03 21:01:58 +01:00
|
|
|
'ColorResolutionDepth', 'Directory', 'Duration',
|
|
|
|
'ExifToolVersion', 'FileAccessDate',
|
|
|
|
'FileInodeChangeDate', 'FileModifyDate', 'FileName',
|
|
|
|
'FilePermissions', 'FileSize', 'FileType',
|
|
|
|
'FileTypeExtension', 'FrameCount', 'GIFVersion',
|
|
|
|
'HasColorMap', 'ImageHeight', 'ImageSize', 'ImageWidth',
|
|
|
|
'MIMEType', 'Megapixels', 'SourceFile',}
|
|
|
|
|
|
|
|
def remove_all(self) -> bool:
|
|
|
|
return self._lightweight_cleanup()
|
|
|
|
|
|
|
|
|
2018-10-18 19:19:56 +02:00
|
|
|
class GdkPixbufAbstractParser(exiftool.ExiftoolParser):
|
2018-04-02 23:40:08 +02:00
|
|
|
""" GdkPixbuf can handle a lot of surfaces, so we're rending images on it,
|
2018-07-19 23:10:27 +02:00
|
|
|
this has the side-effect of completely removing metadata.
|
2018-04-02 23:40:08 +02:00
|
|
|
"""
|
2018-06-22 20:38:29 +02:00
|
|
|
_type = ''
|
|
|
|
|
2018-10-12 11:58:01 +02:00
|
|
|
def __init__(self, filename):
|
|
|
|
super().__init__(filename)
|
2018-10-24 19:35:07 +02:00
|
|
|
# we can't use imghdr here because of https://bugs.python.org/issue28591
|
|
|
|
try:
|
|
|
|
GdkPixbuf.Pixbuf.new_from_file(self.filename)
|
|
|
|
except GLib.GError:
|
2018-10-12 11:58:01 +02:00
|
|
|
raise ValueError
|
|
|
|
|
|
|
|
def remove_all(self) -> bool:
|
2018-10-24 19:35:07 +02:00
|
|
|
if self.lightweight_cleaning:
|
|
|
|
return self._lightweight_cleanup()
|
|
|
|
|
2018-04-01 00:43:36 +02:00
|
|
|
_, extension = os.path.splitext(self.filename)
|
2018-03-25 15:09:12 +02:00
|
|
|
pixbuf = GdkPixbuf.Pixbuf.new_from_file(self.filename)
|
2018-08-23 20:43:27 +02:00
|
|
|
if extension.lower() == '.jpg':
|
2018-06-08 17:34:53 +02:00
|
|
|
extension = '.jpeg' # gdk is picky
|
2018-10-24 19:35:07 +02:00
|
|
|
pixbuf.savev(self.output_filename, type=extension[1:], option_keys=[], option_values=[])
|
2018-03-25 15:09:12 +02:00
|
|
|
return True
|
2018-04-01 00:43:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
class JPGParser(GdkPixbufAbstractParser):
|
2018-06-22 20:38:29 +02:00
|
|
|
_type = 'jpeg'
|
2018-04-03 23:56:39 +02:00
|
|
|
mimetypes = {'image/jpeg'}
|
2019-02-20 00:45:27 +01:00
|
|
|
meta_allowlist = {'SourceFile', 'ExifToolVersion', 'FileName',
|
2018-05-16 22:36:59 +02:00
|
|
|
'Directory', 'FileSize', 'FileModifyDate',
|
|
|
|
'FileAccessDate', "FileInodeChangeDate",
|
|
|
|
'FilePermissions', 'FileType', 'FileTypeExtension',
|
|
|
|
'MIMEType', 'ImageWidth', 'ImageSize', 'BitsPerSample',
|
|
|
|
'ColorComponents', 'EncodingProcess', 'JFIFVersion',
|
|
|
|
'ResolutionUnit', 'XResolution', 'YCbCrSubSampling',
|
|
|
|
'YResolution', 'Megapixels', 'ImageHeight'}
|
2018-04-01 00:43:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TiffParser(GdkPixbufAbstractParser):
|
2018-06-22 20:38:29 +02:00
|
|
|
_type = 'tiff'
|
2018-04-01 00:43:36 +02:00
|
|
|
mimetypes = {'image/tiff'}
|
2019-02-20 00:45:27 +01:00
|
|
|
meta_allowlist = {'Compression', 'ExifByteOrder', 'ExtraSamples',
|
2018-05-16 22:36:59 +02:00
|
|
|
'FillOrder', 'PhotometricInterpretation',
|
|
|
|
'PlanarConfiguration', 'RowsPerStrip', 'SamplesPerPixel',
|
|
|
|
'StripByteCounts', 'StripOffsets', 'BitsPerSample',
|
|
|
|
'Directory', 'ExifToolVersion', 'FileAccessDate',
|
|
|
|
'FileInodeChangeDate', 'FileModifyDate', 'FileName',
|
|
|
|
'FilePermissions', 'FileSize', 'FileType',
|
|
|
|
'FileTypeExtension', 'ImageHeight', 'ImageSize',
|
|
|
|
'ImageWidth', 'MIMEType', 'Megapixels', 'SourceFile'}
|
2019-09-01 18:28:46 +02:00
|
|
|
|
|
|
|
class PPMParser(abstract.AbstractParser):
|
|
|
|
mimetypes = {'image/x-portable-pixmap'}
|
|
|
|
|
|
|
|
def get_meta(self) -> Dict[str, Union[str, dict]]:
|
|
|
|
meta = {} # type: Dict[str, Union[str, Dict[Any, Any]]]
|
|
|
|
with open(self.filename) as f:
|
|
|
|
for idx, line in enumerate(f):
|
|
|
|
if line.lstrip().startswith('#'):
|
|
|
|
meta[str(idx)] = line.lstrip().rstrip()
|
|
|
|
return meta
|
|
|
|
|
|
|
|
def remove_all(self) -> bool:
|
|
|
|
with open(self.filename) as fin:
|
|
|
|
with open(self.output_filename, 'w') as fout:
|
|
|
|
for line in fin:
|
|
|
|
if not line.lstrip().startswith('#'):
|
|
|
|
line = re.sub(r"\s+", "", line, flags=re.UNICODE)
|
|
|
|
fout.write(line)
|
|
|
|
return True
|