diff options
Diffstat (limited to 'libmat/exiftool.py')
| -rw-r--r-- | libmat/exiftool.py | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/libmat/exiftool.py b/libmat/exiftool.py new file mode 100644 index 0000000..9e38f04 --- /dev/null +++ b/libmat/exiftool.py | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | ''' Care about images with help of the amazing (perl) library Exiftool. | ||
| 2 | ''' | ||
| 3 | |||
| 4 | import parser | ||
| 5 | import subprocess | ||
| 6 | |||
| 7 | |||
| 8 | class ExiftoolStripper(parser.GenericParser): | ||
| 9 | ''' A generic stripper class using exiftool as backend | ||
| 10 | ''' | ||
| 11 | |||
| 12 | def __init__(self, filename, parser, mime, backup, is_writable, **kwargs): | ||
| 13 | super(ExiftoolStripper, self).__init__(filename, parser, mime, backup, is_writable, **kwargs) | ||
| 14 | self.allowed = set(['ExifTool Version Number', 'File Name', 'Directory', | ||
| 15 | 'File Size', 'File Modification Date/Time', 'File Access Date/Time', 'File Permissions', | ||
| 16 | 'File Type', 'MIME Type', 'Image Width', 'Image Height', | ||
| 17 | 'Image Size', 'File Inode Change Date/Time']) | ||
| 18 | self._set_allowed() | ||
| 19 | |||
| 20 | def _set_allowed(self): | ||
| 21 | ''' Virtual method. Set the allowed/harmless list of metadata | ||
| 22 | ''' | ||
| 23 | raise NotImplementedError | ||
| 24 | |||
| 25 | def remove_all(self): | ||
| 26 | ''' Remove all metadata with help of exiftool | ||
| 27 | ''' | ||
| 28 | try: | ||
| 29 | if self.backup: | ||
| 30 | self.create_backup_copy() | ||
| 31 | # Note: '-All=' must be followed by a known exiftool option. | ||
| 32 | subprocess.call(['exiftool', '-m', '-all=', | ||
| 33 | '-adobe=', '-overwrite_original', self.filename], | ||
| 34 | stdout=open('/dev/null')) | ||
| 35 | return True | ||
| 36 | except: | ||
| 37 | return False | ||
| 38 | |||
| 39 | def is_clean(self): | ||
| 40 | ''' Check if the file is clean with the help of exiftool | ||
| 41 | ''' | ||
| 42 | return not self.get_meta() | ||
| 43 | |||
| 44 | def get_meta(self): | ||
| 45 | ''' Return every harmful meta with help of exiftool. | ||
| 46 | Exiftool output looks like this: | ||
| 47 | field name : value | ||
| 48 | field name : value | ||
| 49 | ''' | ||
| 50 | output = subprocess.Popen(['exiftool', self.filename], | ||
| 51 | stdout=subprocess.PIPE).communicate()[0] | ||
| 52 | meta = {} | ||
| 53 | for i in output.split('\n')[:-1]: # chop last char ('\n') | ||
| 54 | key = i.split(':')[0].strip() | ||
| 55 | if key not in self.allowed: | ||
| 56 | meta[key] = i.split(':')[1].strip() # add the field name to the metadata set | ||
| 57 | return meta | ||
| 58 | |||
| 59 | |||
| 60 | class JpegStripper(ExiftoolStripper): | ||
| 61 | ''' Care about jpeg files with help | ||
| 62 | of exiftool | ||
| 63 | ''' | ||
| 64 | def _set_allowed(self): | ||
| 65 | self.allowed.update(['JFIF Version', 'Resolution Unit', | ||
| 66 | 'X Resolution', 'Y Resolution', 'Encoding Process', | ||
| 67 | 'Bits Per Sample', 'Color Components', 'Y Cb Cr Sub Sampling']) | ||
| 68 | |||
| 69 | |||
| 70 | class PngStripper(ExiftoolStripper): | ||
| 71 | ''' Care about png files with help | ||
| 72 | of exiftool | ||
| 73 | ''' | ||
| 74 | def _set_allowed(self): | ||
| 75 | self.allowed.update(['Bit Depth', 'Color Type', | ||
| 76 | 'Compression', 'Filter', 'Interlace', 'Pixels Per Unit X', | ||
| 77 | 'Pixels Per Unit Y', 'Pixel Units', 'Significant Bits', | ||
| 78 | 'Background Color', 'SRGB Rendering']) | ||
