exif: log errors
@@ -6,6 +6,7 @@ from math import sqrt
from io import BytesIO
from subprocess import run, PIPE
from distutils.spawn import find_executable as which
+from ocrd_utils import getLogger
class OcrdExif():
"""Represents technical image metadata.
@@ -35,15 +36,8 @@ class OcrdExif():
self.height = img.height
self.photometricInterpretation = img.mode
self.n_frames = img.n_frames if 'n_frames' in img.__dict__ else 1
- # if img.format == 'PNG':
- # print(img.info)
for prop in ['compression', 'photometric_interpretation']:
setattr(self, prop, img.info[prop] if prop in img.info else None)
- if img.format == 'JPEG2000':
- # XXX find a way to get pixel density in jpeg2000
- self.xResolution = self.yResolution = 1
- self.resolutionUnit = 'inches'
- else:
if img.filename:
ret = run(['identify', '-format', r'%x %y %U', img.filename], check=False, stderr=PIPE, stdout=PIPE)
else:
@@ -51,9 +45,16 @@ class OcrdExif():
img.save(bio, format=img.format)
ret = run(['identify', '-format', r'%x %y %U', '/dev/stdin'], check=False, stderr=PIPE, stdout=PIPE, input=bio.getvalue())
if ret.returncode:
+ stderr = ret.stderr.decode('utf-8')
if not which('identify'):
raise Exception("The 'identify' command is not available. Install with 'sudo apt install imagemagick'")
- raise ValueError("'identify' returned non-zero exit code %s. stderr: %s" % (ret.returncode, ret.stderr))
+ if 'no decode delegate for this image format' in stderr:
+ getLogger('ocrd_exif').warning("ImageMagick does not support the '%s' image format. ", img.format)
+ else:
+ getLogger('ocrd_exif').error("identify exited with non-zero %s: %s", ret.returncode, stderr)
+ self.xResolution = self.yResolution = 1
+ self.resolutionUnit = 'inches'
+ else:
tokens = ret.stdout.decode('utf-8').split(' ', 3)
self.xResolution = int(float(tokens[0]))
self.yResolution = int(float(tokens[1]))
