From d9e88e87f96aed40ed5a782b141e17af346b96fa Mon Sep 17 00:00:00 2001 From: tudacs Date: Mon, 17 Dec 2018 22:31:58 +0100 Subject: [PATCH 1/4] Fixes sigal aborting if image contains malformed metadata --- sigal/image.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/sigal/image.py b/sigal/image.py index 728a1e1..5bee48a 100644 --- a/sigal/image.py +++ b/sigal/image.py @@ -235,18 +235,23 @@ def get_exif_data(filename): def get_iptc_data(filename): """Return a dict with the raw IPTC data.""" - img = _read_image(filename) - raw_iptc = IptcImagePlugin.getiptcinfo(img) - # IPTC fields are catalogued in: - # https://www.iptc.org/std/photometadata/specification/IPTC-PhotoMetadata iptc_data = {} - # 2:05 is the IPTC title property - if raw_iptc and (2, 5) in raw_iptc: - iptc_data["title"] = raw_iptc[(2, 5)].decode('utf-8') - # 2:120 is the IPTC description property - if raw_iptc and (2, 120) in raw_iptc: - iptc_data["description"] = raw_iptc[(2, 120)].decode('utf-8') + try: + img = _read_image(filename) + raw_iptc = IptcImagePlugin.getiptcinfo(img) + # IPTC fields are catalogued in: + # https://www.iptc.org/std/photometadata/specification/IPTC-PhotoMetadata + # 2:05 is the IPTC title property + if raw_iptc and (2, 5) in raw_iptc: + iptc_data["title"] = raw_iptc[(2, 5)].decode('utf-8') + + # 2:120 is the IPTC description property + if raw_iptc and (2, 120) in raw_iptc: + iptc_data["description"] = raw_iptc[(2, 120)].decode('utf-8') + except SyntaxError: + print("IPTC Error in %s\n"%filename) + iptc_data = {} return iptc_data From 859ac1117a0ed30b9c16ec9b3d16e9aeb8de6274 Mon Sep 17 00:00:00 2001 From: tudacs Date: Tue, 18 Dec 2018 08:19:54 +0100 Subject: [PATCH 2/4] Shortens try/sxcept block to keep code cleaner ... and adds a comment where the SyntaxError may come from. --- sigal/image.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/sigal/image.py b/sigal/image.py index 5bee48a..58fa53b 100644 --- a/sigal/image.py +++ b/sigal/image.py @@ -237,21 +237,24 @@ def get_iptc_data(filename): iptc_data = {} + # PILs IptcImagePlugin issues a SyntaxError in certain circumstances + # with malformed metadata, see PIL/IptcImagePlugin.py", line 71. + # ( https://github.com/python-pillow/Pillow/blob/9dd0348be2751beb2c617e32ff9985aa2f92ae5f/src/PIL/IptcImagePlugin.py#L71 ) try: img = _read_image(filename) raw_iptc = IptcImagePlugin.getiptcinfo(img) - # IPTC fields are catalogued in: - # https://www.iptc.org/std/photometadata/specification/IPTC-PhotoMetadata - # 2:05 is the IPTC title property - if raw_iptc and (2, 5) in raw_iptc: - iptc_data["title"] = raw_iptc[(2, 5)].decode('utf-8') - - # 2:120 is the IPTC description property - if raw_iptc and (2, 120) in raw_iptc: - iptc_data["description"] = raw_iptc[(2, 120)].decode('utf-8') except SyntaxError: print("IPTC Error in %s\n"%filename) - iptc_data = {} + + # IPTC fields are catalogued in: + # https://www.iptc.org/std/photometadata/specification/IPTC-PhotoMetadata + # 2:05 is the IPTC title property + if raw_iptc and (2, 5) in raw_iptc: + iptc_data["title"] = raw_iptc[(2, 5)].decode('utf-8') + + # 2:120 is the IPTC description property + if raw_iptc and (2, 120) in raw_iptc: + iptc_data["description"] = raw_iptc[(2, 120)].decode('utf-8') return iptc_data From 4f7eb0548baec6658e31aa10f600640fb9543281 Mon Sep 17 00:00:00 2001 From: tudacs Date: Tue, 18 Dec 2018 08:22:50 +0100 Subject: [PATCH 3/4] Use logger instead of (dump) print --- sigal/image.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sigal/image.py b/sigal/image.py index 58fa53b..07e8bcd 100644 --- a/sigal/image.py +++ b/sigal/image.py @@ -235,6 +235,8 @@ def get_exif_data(filename): def get_iptc_data(filename): """Return a dict with the raw IPTC data.""" + logger = logging.getLogger(__name__) + iptc_data = {} # PILs IptcImagePlugin issues a SyntaxError in certain circumstances @@ -244,7 +246,7 @@ def get_iptc_data(filename): img = _read_image(filename) raw_iptc = IptcImagePlugin.getiptcinfo(img) except SyntaxError: - print("IPTC Error in %s\n"%filename) + logger.info('IPTC Error in %s',filename) # IPTC fields are catalogued in: # https://www.iptc.org/std/photometadata/specification/IPTC-PhotoMetadata From a65a3de12cf4d19160fc8cd5992525ff99463ec9 Mon Sep 17 00:00:00 2001 From: tudacs Date: Tue, 18 Dec 2018 15:16:05 +0100 Subject: [PATCH 4/4] Initialize raw_iptc prior to try/except --- sigal/image.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sigal/image.py b/sigal/image.py index 07e8bcd..d242a23 100644 --- a/sigal/image.py +++ b/sigal/image.py @@ -238,6 +238,7 @@ def get_iptc_data(filename): logger = logging.getLogger(__name__) iptc_data = {} + raw_iptc = {} # PILs IptcImagePlugin issues a SyntaxError in certain circumstances # with malformed metadata, see PIL/IptcImagePlugin.py", line 71.