1
0
Fork 0
mirror of synced 2025-07-03 20:07:28 +02:00

Implement lightweight cleaning for png and tiff

This commit is contained in:
jvoisin 2018-10-23 16:14:21 +02:00
parent 38df679a88
commit f1a071d460
7 changed files with 111 additions and 43 deletions

View file

@ -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):

View file

@ -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

View file

@ -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