From: Vladimír Vondruš Date: Fri, 1 Dec 2017 10:33:07 +0000 (+0100) Subject: m.images: don't freak out if there's no EXIF. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~cjwatson/git?a=commitdiff_plain;h=525dedb96b43ce87e00a842c51a3b24f8877e71e;p=blog.git m.images: don't freak out if there's no EXIF. Instead it just doesn't display anything as a caption. --- diff --git a/doc/plugins/images-test.rst b/doc/plugins/images-test.rst index cea68ab8..e3c49f47 100644 --- a/doc/plugins/images-test.rst +++ b/doc/plugins/images-test.rst @@ -81,3 +81,9 @@ Image grid, inflated: {filename}/static/flowers.jpg {filename}/static/ship.jpg + +Image grid with a PNG file: + +.. image-grid:: + + {filename}/static/tiny.png diff --git a/doc/plugins/images.rst b/doc/plugins/images.rst index 24570008..7cf64e63 100644 --- a/doc/plugins/images.rst +++ b/doc/plugins/images.rst @@ -125,10 +125,10 @@ option for both images and figures. Use the :rst:`.. image-grid::` directive for creating `image grid <{filename}/css/components.rst#image-grid>`_. Directive contents are a list of image URLs, blank lines separate grid rows. The plugin -automatically extracts size information and scales the images accordingly, in -addition EXIF properties such as aperture, shutter speed and ISO are extracted -and displayed in the caption on hover. The images are also made clickable, the -target is the image file itself. +automatically extracts size information and scales the images accordingly. If +the image has EXIF information, properties such as aperture, shutter speed and +ISO are extracted and displayed in the caption on hover. The images are also +made clickable, the target is the image file itself. Example of a two-row image grid is below. Sorry for reusing the same two images all over (I'm making it easier for myself); if you want to see a live example diff --git a/doc/static/tiny.png b/doc/static/tiny.png new file mode 100644 index 00000000..cacab704 Binary files /dev/null and b/doc/static/tiny.png differ diff --git a/pelican-plugins/m/images.py b/pelican-plugins/m/images.py index 3eaaaf1b..168bb930 100644 --- a/pelican-plugins/m/images.py +++ b/pelican-plugins/m/images.py @@ -183,13 +183,20 @@ class ImageGrid(rst.Directive): # Open the files and calculate the overall width absuri = uri.format(filename=os.path.join(os.getcwd(), settings['PATH'])) im = PIL.Image.open(absuri) - exif = { - PIL.ExifTags.TAGS[k]: v - for k, v in im._getexif().items() - if k in PIL.ExifTags.TAGS and len(str(v)) < 256 - } - # Can't use just *exif['ExposureTime'] on Py3.4 - caption = "F{}, {}/{} s, ISO {}".format(float(exif['FNumber'][0])/float(exif['FNumber'][1]), exif['ExposureTime'][0], exif['ExposureTime'][1], exif['ISOSpeedRatings']) + + # Get EXIF info, if it's there + if hasattr(im, '_getexif'): + exif = { + PIL.ExifTags.TAGS[k]: v + for k, v in im._getexif().items() + if k in PIL.ExifTags.TAGS and len(str(v)) < 256 + } + # Can't use just *exif['ExposureTime'] on Py3.4 + caption = "F{}, {}/{} s, ISO {}".format(float(exif['FNumber'][0])/float(exif['FNumber'][1]), exif['ExposureTime'][0], exif['ExposureTime'][1], exif['ISOSpeedRatings']) + + # It's not (e.g. a PNG file), empty caption + else: caption = "" + rel_width = float(im.width)/im.height total_widths[-1] += rel_width rows[-1].append((uri, rel_width, caption)) @@ -200,10 +207,17 @@ class ImageGrid(rst.Directive): for uri, rel_width, caption in row: image_reference = rst.directives.uri(uri) image_node = nodes.image('', uri=image_reference) - text_nodes, _ = self.state.inline_text(caption, self.lineno) - text_node = nodes.paragraph('', '', *text_nodes) - overlay_node = nodes.caption() - overlay_node.append(text_node) + + # in case there's a caption + if caption: + text_nodes, _ = self.state.inline_text(caption, self.lineno) + text_node = nodes.paragraph('', '', *text_nodes) + overlay_node = nodes.caption() + overlay_node.append(text_node) + + # Otherwise an empty
+ else: overlay_node = nodes.container() + link_node = nodes.reference('', refuri=image_reference) link_node.append(image_node) link_node.append(overlay_node)