summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjvoisin2015-02-22 10:10:50 +0100
committerjvoisin2015-02-22 10:15:34 +0100
commit79ce29a7d5e41bb3bb2499bc7eb99164c423aa1d (patch)
tree3c8515874cfb9e0469bf0ea32d18b2fee1a66047
parent655dda11a1eaf740573f019c3e734ef202db2345 (diff)
Preliminary implementation of PIL for images.
PIL (Python Image Library) is used to open, then save images, in order to remove unknown metadata.
Diffstat (limited to '')
-rw-r--r--README1
-rw-r--r--libmat/exiftool.py7
-rw-r--r--libmat/images.py16
-rw-r--r--libmat/pillow.py27
4 files changed, 47 insertions, 4 deletions
diff --git a/README b/README
index 218655f..9e49136 100644
--- a/README
+++ b/README
@@ -21,6 +21,7 @@ DEPENDENCIES
21============ 21============
22 * python2.7 (at least) 22 * python2.7 (at least)
23 * python-hachoir-core and python-hachoir-parser 23 * python-hachoir-core and python-hachoir-parser
24 * python-pil for more secure images handling
24 * python-pdfrw, gir-poppler and python-gi-cairo for full PDF support 25 * python-pdfrw, gir-poppler and python-gi-cairo for full PDF support
25 * python-gi for the GUI 26 * python-gi for the GUI
26 * shred (should be already installed) 27 * 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 @@
1''' Care about images with help of the amazing (perl) library Exiftool. 1''' Care about images with help of the amazing (perl) library Exiftool.
2''' 2'''
3 3
4import parser
5import subprocess 4import subprocess
6 5
6import parser
7import pillow
8
7 9
8class ExiftoolStripper(parser.GenericParser): 10class ExiftoolStripper(parser.GenericParser, pillow.PillowStripper):
9 ''' A generic stripper class using exiftool as backend 11 ''' A generic stripper class using exiftool as backend
10 ''' 12 '''
11 13
@@ -25,6 +27,7 @@ class ExiftoolStripper(parser.GenericParser):
25 def remove_all(self): 27 def remove_all(self):
26 ''' Remove all metadata with help of exiftool 28 ''' Remove all metadata with help of exiftool
27 ''' 29 '''
30 self.open_and_save()
28 try: 31 try:
29 if self.backup: 32 if self.backup:
30 self.create_backup_copy() 33 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:
7''' 7'''
8 8
9import parser 9import parser
10import pillow
10 11
11 12
12class JpegStripper(parser.GenericParser): 13class ImageStripper(parser.GenericParser, pillow.PillowStripper):
14 ''' Common stripper for images.
15 Its purpose is to open then save
16 images with PIL, the goal being to remove
17 unknown metadata.
18 '''
19 def remove_all(self):
20 self.open_and_save()
21 super(ImageStripper, self).remove_all()
22
23
24class JpegStripper(ImageStripper):
13 ''' Represents a jpeg file. 25 ''' Represents a jpeg file.
14 Custom Huffman and Quantization tables 26 Custom Huffman and Quantization tables
15 are stripped: they may leak 27 are stripped: they may leak
@@ -34,7 +46,7 @@ class JpegStripper(parser.GenericParser):
34 return True 46 return True
35 47
36 48
37class PngStripper(parser.GenericParser): 49class PngStripper(ImageStripper):
38 ''' Represents a png file 50 ''' Represents a png file
39 ''' 51 '''
40 def _should_remove(self, field): 52 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 @@
1''' Care about images with help of PIL.
2This class doesn't remove metadata in the "conventional way";
3it opens, then saves the image. This should remove unknown metadata.
4'''
5
6# FIXME Implement this with a decorator instead
7
8import parser
9
10
11class PillowStripper(object):
12 ''' This class implements a single method, "open_and_save".
13 It's a class instead of a function so it can be inherited.
14 '''
15 def open_and_save(self):
16 ''' Open and save the image with PIL.
17 This should remove a lot of unknown metadata.
18 '''
19 try:
20 from PIL import Image
21 except ImportError:
22 logging.error('Unable to import PIL, image support degraded. Be careful.')
23
24 try:
25 Image.open(self.filename).save(self.filename)
26 except IOError:
27 logging.error('Can not save %s.' % self.filename)