diff options
| author | jvoisin | 2018-10-23 16:14:21 +0200 |
|---|---|---|
| committer | jvoisin | 2018-10-23 16:22:11 +0200 |
| commit | f1a071d460507fd1bb1721deafd2a8d9f88f5b05 (patch) | |
| tree | e17067895ef1fc9b91b00c0ba56d2e86975ceef1 /libmat2 | |
| parent | 38df679a88a19db3a4a82fdb8e20a42c9a53d1a1 (diff) | |
Implement lightweight cleaning for png and tiff
Diffstat (limited to 'libmat2')
| -rw-r--r-- | libmat2/exiftool.py | 29 | ||||
| -rw-r--r-- | libmat2/images.py | 2 | ||||
| -rw-r--r-- | libmat2/video.py | 2 |
3 files changed, 32 insertions, 1 deletions
diff --git a/libmat2/exiftool.py b/libmat2/exiftool.py index 11dd36d..23d0d89 100644 --- a/libmat2/exiftool.py +++ b/libmat2/exiftool.py | |||
| @@ -1,4 +1,5 @@ | |||
| 1 | import json | 1 | import json |
| 2 | import logging | ||
| 2 | import os | 3 | import os |
| 3 | import subprocess | 4 | import subprocess |
| 4 | from typing import Dict, Union, Set | 5 | from typing import Dict, Union, Set |
| @@ -23,6 +24,34 @@ class ExiftoolParser(abstract.AbstractParser): | |||
| 23 | meta.pop(key, None) | 24 | meta.pop(key, None) |
| 24 | return meta | 25 | return meta |
| 25 | 26 | ||
| 27 | def _lightweight_cleanup(self): | ||
| 28 | if os.path.exists(self.output_filename): | ||
| 29 | try: | ||
| 30 | # exiftool can't force output to existing files | ||
| 31 | os.remove(self.output_filename) | ||
| 32 | except OSError as e: # pragma: no cover | ||
| 33 | logging.error("The output file %s is already existing and \ | ||
| 34 | can't be overwritten: %s.", self.filename, e) | ||
| 35 | return False | ||
| 36 | |||
| 37 | # Note: '-All=' must be followed by a known exiftool option. | ||
| 38 | # Also, '-CommonIFD0' is needed for .tiff files | ||
| 39 | cmd = [_get_exiftool_path(), | ||
| 40 | '-all=', # remove metadata | ||
| 41 | '-adobe=', # remove adobe-specific metadata | ||
| 42 | '-exif:all=', # remove all exif metadata | ||
| 43 | '-Time:All=', # remove all timestamps | ||
| 44 | '-quiet', # don't show useless logs | ||
| 45 | '-CommonIFD0=', # remove IFD0 metadata | ||
| 46 | '-o', self.output_filename, | ||
| 47 | self.filename] | ||
| 48 | try: | ||
| 49 | subprocess.check_call(cmd) | ||
| 50 | except subprocess.CalledProcessError as e: # pragma: no cover | ||
| 51 | logging.error("Something went wrong during the processing of %s: %s", self.filename, e) | ||
| 52 | return False | ||
| 53 | return True | ||
| 54 | |||
| 26 | def _get_exiftool_path() -> str: # pragma: no cover | 55 | def _get_exiftool_path() -> str: # pragma: no cover |
| 27 | exiftool_path = '/usr/bin/exiftool' | 56 | exiftool_path = '/usr/bin/exiftool' |
| 28 | if os.path.isfile(exiftool_path): | 57 | if os.path.isfile(exiftool_path): |
diff --git a/libmat2/images.py b/libmat2/images.py index ad80892..03cecd3 100644 --- a/libmat2/images.py +++ b/libmat2/images.py | |||
| @@ -35,6 +35,8 @@ class PNGParser(exiftool.ExiftoolParser): | |||
| 35 | raise ValueError | 35 | raise ValueError |
| 36 | 36 | ||
| 37 | def remove_all(self) -> bool: | 37 | def remove_all(self) -> bool: |
| 38 | if self.lightweight_cleaning: | ||
| 39 | return self._lightweight_cleanup() | ||
| 38 | surface = cairo.ImageSurface.create_from_png(self.filename) | 40 | surface = cairo.ImageSurface.create_from_png(self.filename) |
| 39 | surface.write_to_png(self.output_filename) | 41 | surface.write_to_png(self.output_filename) |
| 40 | return True | 42 | return True |
diff --git a/libmat2/video.py b/libmat2/video.py index fe2a1af..b7ba0a0 100644 --- a/libmat2/video.py +++ b/libmat2/video.py | |||
| @@ -26,7 +26,7 @@ class AVIParser(exiftool.ExiftoolParser): | |||
| 26 | 26 | ||
| 27 | def remove_all(self): | 27 | def remove_all(self): |
| 28 | cmd = [_get_ffmpeg_path(), | 28 | cmd = [_get_ffmpeg_path(), |
| 29 | '-i', self.filename, # input file | 29 | '-i', self.filename, # input file |
| 30 | '-y', # overwrite existing output file | 30 | '-y', # overwrite existing output file |
| 31 | '-loglevel', 'panic', # Don't show log | 31 | '-loglevel', 'panic', # Don't show log |
| 32 | '-hide_banner', # hide the banner | 32 | '-hide_banner', # hide the banner |
