chiark / gitweb /
m.images: gracefully handle sparse EXIF info in image grid.
authorVladimír Vondruš <mosra@centrum.cz>
Mon, 11 Dec 2017 15:09:46 +0000 (16:09 +0100)
committerVladimír Vondruš <mosra@centrum.cz>
Mon, 11 Dec 2017 15:18:10 +0000 (16:18 +0100)
And document EXIF modification for future self.

pelican-plugins/m/images.py
pelican-plugins/m/test/images/page.html
pelican-plugins/m/test/images/page.rst
pelican-plugins/m/test/images/sparseexif.jpg [new file with mode: 0644]
pelican-plugins/m/test/test_images.py

index 168bb93078ee57c6428ea7c34064959930a48c22..2ebc16db3404e6ec73bea09f00aeab14e0b369e2 100644 (file)
@@ -191,8 +191,16 @@ class ImageGrid(rst.Directive):
                     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'])
+
+                # Not all info might be present
+                caption = []
+                if 'FNumber' in exif:
+                    caption += ["F{}".format(float(float(exif['FNumber'][0])/float(exif['FNumber'][1])))]
+                if 'ExposureTime' in exif:
+                    caption += ["{}/{} s".format(exif['ExposureTime'][0], exif['ExposureTime'][1])]
+                if 'ISOSpeedRatings' in exif:
+                    caption += ["ISO {}".format(exif['ISOSpeedRatings'])]
+                caption = ', '.join(caption)
 
             # It's not (e.g. a PNG file), empty caption
             else: caption = ""
index 40fc9b26ff967413643ae564face0fa53b8a4688..abf048f5a8c38122f495342534725fd58caf1386 100644 (file)
@@ -82,14 +82,18 @@ Yes.</span>
 </figure>
 </div>
 </div>
-<p>Image grid with a PNG file:</p>
+<p>Image grid with a PNG file and a JPEG with sparse EXIF data:</p>
 <div class="m-imagegrid m-container-inflate">
 <div>
-<figure style="width: 100.000%">
+<figure style="width: 70.588%">
 <a href="./tiny.png"><img src="./tiny.png" /><div>
 </div>
 </a>
 </figure>
+<figure style="width: 29.412%">
+<a href="./sparseexif.jpg"><img src="./sparseexif.jpg" /><figcaption>F2.8</figcaption>
+</a>
+</figure>
 </div>
 </div>
 <!-- /content -->
index 2cc494f1c6266d75f0cca6ea75cc556c98d46d2d..13fe99aa700bbcf2a942bb8eff5790b532c06f25 100644 (file)
@@ -57,8 +57,9 @@ Image grid:
     {filename}/flowers.jpg
     {filename}/ship.jpg
 
-Image grid with a PNG file:
+Image grid with a PNG file and a JPEG with sparse EXIF data:
 
 .. image-grid::
 
     {filename}/tiny.png
+    {filename}/sparseexif.jpg
diff --git a/pelican-plugins/m/test/images/sparseexif.jpg b/pelican-plugins/m/test/images/sparseexif.jpg
new file mode 100644 (file)
index 0000000..19044d3
Binary files /dev/null and b/pelican-plugins/m/test/images/sparseexif.jpg differ
index c3bbc644127d74cf8d5574371079aa38a18dd6b3..699e1f0928255efd56e0489688610a461eec56ce 100644 (file)
@@ -31,7 +31,17 @@ class Images(PluginTestCase):
     def test(self):
         self.run_pelican({
             'PLUGINS': ['m.htmlsanity', 'm.images'],
-            'STATIC_PATHS': ['tiny.png', 'ship.jpg', 'flowers.jpg']
+            'STATIC_PATHS': ['tiny.png', 'ship.jpg', 'flowers.jpg', 'sparseexif.jpg']
         })
 
+        #
+        # Modifying EXIF in files for testing:
+        #
+        #   # Prints out all EXIF info
+        #   identify -format '%[EXIF:*]' file.jpg
+        #
+        #   # Removes *everything* and adds a new value (order is important)
+        #   exiftool -all= -exif:FNumber=28/10 file.jpg
+        #
+
         self.assertEqual(*self.actual_expected_contents('page.html'))