From f1a071d460507fd1bb1721deafd2a8d9f88f5b05 Mon Sep 17 00:00:00 2001 From: jvoisin Date: Tue, 23 Oct 2018 16:14:21 +0200 Subject: Implement lightweight cleaning for png and tiff --- libmat2/exiftool.py | 29 +++++++++++++++++++++++++++++ libmat2/images.py | 2 ++ libmat2/video.py | 2 +- 3 files changed, 32 insertions(+), 1 deletion(-) (limited to 'libmat2') 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 @@ import json +import logging import os import subprocess from typing import Dict, Union, Set @@ -23,6 +24,34 @@ class ExiftoolParser(abstract.AbstractParser): meta.pop(key, None) return meta + def _lightweight_cleanup(self): + if os.path.exists(self.output_filename): + try: + # exiftool can't force output to existing files + os.remove(self.output_filename) + except OSError as e: # pragma: no cover + logging.error("The output file %s is already existing and \ + can't be overwritten: %s.", self.filename, e) + return False + + # Note: '-All=' must be followed by a known exiftool option. + # Also, '-CommonIFD0' is needed for .tiff files + cmd = [_get_exiftool_path(), + '-all=', # remove metadata + '-adobe=', # remove adobe-specific metadata + '-exif:all=', # remove all exif metadata + '-Time:All=', # remove all timestamps + '-quiet', # don't show useless logs + '-CommonIFD0=', # remove IFD0 metadata + '-o', self.output_filename, + self.filename] + try: + subprocess.check_call(cmd) + except subprocess.CalledProcessError as e: # pragma: no cover + logging.error("Something went wrong during the processing of %s: %s", self.filename, e) + return False + return True + def _get_exiftool_path() -> str: # pragma: no cover exiftool_path = '/usr/bin/exiftool' 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): raise ValueError def remove_all(self) -> bool: + if self.lightweight_cleaning: + return self._lightweight_cleanup() surface = cairo.ImageSurface.create_from_png(self.filename) surface.write_to_png(self.output_filename) 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): def remove_all(self): cmd = [_get_ffmpeg_path(), - '-i', self.filename, # input file + '-i', self.filename, # input file '-y', # overwrite existing output file '-loglevel', 'panic', # Don't show log '-hide_banner', # hide the banner -- cgit v1.3