chiark / gitweb /
m.images: don't freak out if there's no EXIF.
authorVladimír Vondruš <mosra@centrum.cz>
Fri, 1 Dec 2017 10:33:07 +0000 (11:33 +0100)
committerVladimír Vondruš <mosra@centrum.cz>
Fri, 1 Dec 2017 10:34:20 +0000 (11:34 +0100)
Instead it just doesn't display anything as a caption.

doc/plugins/images-test.rst
doc/plugins/images.rst
doc/static/tiny.png [new file with mode: 0644]
pelican-plugins/m/images.py

index cea68ab8c4e4653902f7aac29b90df10694656a6..e3c49f47ea36f2b5ab2f8802b6563d321ddea065 100644 (file)
@@ -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
index 245700088614f883c834d607ca54337d07077699..7cf64e630e84c1a5755f538fd1673f797f0e673b 100644 (file)
@@ -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 (file)
index 0000000..cacab70
Binary files /dev/null and b/doc/static/tiny.png differ
index 3eaaaf1ba3246fcaa95ef6f404a984fd89745b2f..168bb93078ee57c6428ea7c34064959930a48c22 100644 (file)
@@ -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)
+
+                # <figurecaption> 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 <div>
+                else: overlay_node = nodes.container()
+
                 link_node = nodes.reference('', refuri=image_reference)
                 link_node.append(image_node)
                 link_node.append(overlay_node)