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
# 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))
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)