diff --git a/sigal/gallery.py b/sigal/gallery.py index 51c2ee8..63dfd2e 100644 --- a/sigal/gallery.py +++ b/sigal/gallery.py @@ -170,19 +170,16 @@ class Image(Media): def _get_metadata(self): super(Image, self)._get_metadata() # If a title or description hasn't been obtained by other means, look - # for the information in IPTC fields as catalogued in: - # https://www.iptc.org/std/photometadata/specification/IPTC-PhotoMetadata - if not self.title or not self.description: - iptc_data = get_iptc_data(self.src_path) - if iptc_data and not self.title: - # 2:05 is the IPTC title property - if (2, 5) in iptc_data: - self.title = iptc_data[(2, 5)].decode('utf-8') + # for the information in IPTC fields + if self.title and self.description: + # Nothing to do - we already have title and description + return - if iptc_data and not self.description: - # 2:120 is the IPTC description property - if (2, 120) in iptc_data: - self.description = iptc_data[(2, 120)].decode('utf-8') + iptc_data = get_iptc_data(self.src_path) + if not self.title and iptc_data.get('title'): + self.title = iptc_data['title'] + if not self.description and iptc_data.get('description'): + self.description = iptc_data['description'] @cached_property def raw_exif(self): diff --git a/sigal/image.py b/sigal/image.py index 597894e..43252a4 100644 --- a/sigal/image.py +++ b/sigal/image.py @@ -241,7 +241,19 @@ def get_iptc_data(filename): """Return a dict with the raw IPTC data.""" img = _read_image(filename) - return IptcImagePlugin.getiptcinfo(img) + 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') + + return iptc_data def dms_to_degrees(v): diff --git a/tests/test_image.py b/tests/test_image.py index 7fea0b6..9c1d805 100644 --- a/tests/test_image.py +++ b/tests/test_image.py @@ -168,19 +168,19 @@ def test_get_iptc_data(): test_image) data = get_iptc_data(src_file) # Title - assert data[(2, 5)] == b'Haemostratulus clouds over Canberra - ' + \ - b'2005-12-28 at 03-25-07' + assert data["title"] == 'Haemostratulus clouds over Canberra - ' + \ + '2005-12-28 at 03-25-07' # Description - assert data[(2, 120)] == b'"Haemo" because they look like haemoglobin ' + \ - b'cells and "stratulus" because I can\'t work out whether ' + \ - b'they\'re Stratus or Cumulus clouds.\nWe\'re driving down ' + \ - b'the main drag in Canberra so it\'s Parliament House that ' + \ - b'you can see at the end of the road.' + assert data["description"] == '"Haemo" because they look like haemoglobin ' + \ + 'cells and "stratulus" because I can\'t work out whether ' + \ + 'they\'re Stratus or Cumulus clouds.\nWe\'re driving down ' + \ + 'the main drag in Canberra so it\'s Parliament House that ' + \ + 'you can see at the end of the road.' # This file has no IPTC data test_image = '21.jpg' src_file = os.path.join(CURRENT_DIR, 'sample', 'pictures', 'exifTest', test_image) - assert get_iptc_data(src_file) is None + assert get_iptc_data(src_file) == {} def test_iso_speed_ratings():