From 79ce29a7d5e41bb3bb2499bc7eb99164c423aa1d Mon Sep 17 00:00:00 2001 From: jvoisin Date: Sun, 22 Feb 2015 10:10:50 +0100 Subject: Preliminary implementation of PIL for images. PIL (Python Image Library) is used to open, then save images, in order to remove unknown metadata. --- README | 1 + libmat/exiftool.py | 7 +++++-- libmat/images.py | 16 ++++++++++++++-- libmat/pillow.py | 27 +++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 libmat/pillow.py diff --git a/README b/README index 218655f..9e49136 100644 --- a/README +++ b/README @@ -21,6 +21,7 @@ DEPENDENCIES ============ * python2.7 (at least) * python-hachoir-core and python-hachoir-parser + * python-pil for more secure images handling * python-pdfrw, gir-poppler and python-gi-cairo for full PDF support * python-gi for the GUI * shred (should be already installed) diff --git a/libmat/exiftool.py b/libmat/exiftool.py index 9e38f04..2c2a2b8 100644 --- a/libmat/exiftool.py +++ b/libmat/exiftool.py @@ -1,11 +1,13 @@ ''' Care about images with help of the amazing (perl) library Exiftool. ''' -import parser import subprocess +import parser +import pillow + -class ExiftoolStripper(parser.GenericParser): +class ExiftoolStripper(parser.GenericParser, pillow.PillowStripper): ''' A generic stripper class using exiftool as backend ''' @@ -25,6 +27,7 @@ class ExiftoolStripper(parser.GenericParser): def remove_all(self): ''' Remove all metadata with help of exiftool ''' + self.open_and_save() try: if self.backup: self.create_backup_copy() diff --git a/libmat/images.py b/libmat/images.py index 67c710f..2daea88 100644 --- a/libmat/images.py +++ b/libmat/images.py @@ -7,9 +7,21 @@ References: ''' import parser +import pillow -class JpegStripper(parser.GenericParser): +class ImageStripper(parser.GenericParser, pillow.PillowStripper): + ''' Common stripper for images. + Its purpose is to open then save + images with PIL, the goal being to remove + unknown metadata. + ''' + def remove_all(self): + self.open_and_save() + super(ImageStripper, self).remove_all() + + +class JpegStripper(ImageStripper): ''' Represents a jpeg file. Custom Huffman and Quantization tables are stripped: they may leak @@ -34,7 +46,7 @@ class JpegStripper(parser.GenericParser): return True -class PngStripper(parser.GenericParser): +class PngStripper(ImageStripper): ''' Represents a png file ''' def _should_remove(self, field): diff --git a/libmat/pillow.py b/libmat/pillow.py new file mode 100644 index 0000000..556d58a --- /dev/null +++ b/libmat/pillow.py @@ -0,0 +1,27 @@ +''' Care about images with help of PIL. +This class doesn't remove metadata in the "conventional way"; +it opens, then saves the image. This should remove unknown metadata. +''' + +# FIXME Implement this with a decorator instead + +import parser + + +class PillowStripper(object): + ''' This class implements a single method, "open_and_save". + It's a class instead of a function so it can be inherited. + ''' + def open_and_save(self): + ''' Open and save the image with PIL. + This should remove a lot of unknown metadata. + ''' + try: + from PIL import Image + except ImportError: + logging.error('Unable to import PIL, image support degraded. Be careful.') + + try: + Image.open(self.filename).save(self.filename) + except IOError: + logging.error('Can not save %s.' % self.filename) -- cgit v1.3