From ecf1fc783488e4e776150694b19771286e61113a Mon Sep 17 00:00:00 2001
From: Anton Sarukhanov <code@ant.sr>
Date: Wed, 9 Sep 2020 10:31:03 -0400
Subject: [PATCH] WIP Photos: Fix rotation & thumbnails

---
 my_plugins/photos/__init__.py | 36 +++--------------------------------
 pelicanconf.py                |  6 ++++--
 requirements.txt              |  2 +-
 theme/css/style.css           |  7 +++++++
 4 files changed, 15 insertions(+), 36 deletions(-)

diff --git a/my_plugins/photos/__init__.py b/my_plugins/photos/__init__.py
index 2dcf4ae..f380cef 100644
--- a/my_plugins/photos/__init__.py
+++ b/my_plugins/photos/__init__.py
@@ -50,7 +50,6 @@ def initialized(pelican):
     DEFAULT_CONFIG.setdefault('VIDEO_COPY_JOBS', 1)
     DEFAULT_CONFIG.setdefault('PHOTO_EXIF_KEEP', False)
     DEFAULT_CONFIG.setdefault('PHOTO_EXIF_REMOVE_GPS', False)
-    DEFAULT_CONFIG.setdefault('PHOTO_EXIF_AUTOROTATE', True)
     DEFAULT_CONFIG.setdefault('PHOTO_EXIF_COPYRIGHT', False)
     DEFAULT_CONFIG.setdefault('PHOTO_EXIF_COPYRIGHT_AUTHOR', DEFAULT_CONFIG['SITENAME'])
     DEFAULT_CONFIG.setdefault('PHOTO_LIGHTBOX_GALLERY_ATTR', 'data-lightbox')
@@ -72,7 +71,6 @@ def initialized(pelican):
         pelican.settings.setdefault('VIDEO_COPY_JOBS', 1)
         pelican.settings.setdefault('PHOTO_EXIF_KEEP', False)
         pelican.settings.setdefault('PHOTO_EXIF_REMOVE_GPS', False)
-        pelican.settings.setdefault('PHOTO_EXIF_AUTOROTATE', True)
         pelican.settings.setdefault('PHOTO_EXIF_COPYRIGHT', False)
         pelican.settings.setdefault('PHOTO_EXIF_COPYRIGHT_AUTHOR', pelican.settings['AUTHOR'])
         pelican.settings.setdefault('PHOTO_LIGHTBOX_GALLERY_ATTR', 'data-lightbox')
@@ -125,29 +123,6 @@ def enqueue_copy(orig, copied):
                      .format(copied, DEFAULT_CONFIG['queue_copy'][copied],
                              orig))
 
-
-def rotate_image(img, exif_dict):
-
-    if "exif" in img.info and piexif.ImageIFD.Orientation in exif_dict["0th"]:
-        orientation = exif_dict["0th"].pop(piexif.ImageIFD.Orientation)
-        if orientation == 2:
-            img = img.transpose(Image.FLIP_LEFT_RIGHT)
-        elif orientation == 3:
-            img = img.rotate(180)
-        elif orientation == 4:
-            img = img.rotate(180).transpose(Image.FLIP_LEFT_RIGHT)
-        elif orientation == 5:
-            img = img.rotate(-90).transpose(Image.FLIP_LEFT_RIGHT)
-        elif orientation == 6:
-            img = img.rotate(-90, expand=True)
-        elif orientation == 7:
-            img = img.rotate(90).transpose(Image.FLIP_LEFT_RIGHT)
-        elif orientation == 8:
-            img = img.rotate(90)
-
-    return (img, exif_dict)
-
-
 def manipulate_exif(img, settings):
 
     try:
@@ -156,9 +131,6 @@ def manipulate_exif(img, settings):
         logger.debug('EXIF information not found')
         exif = {}
 
-    if settings['PHOTO_EXIF_AUTOROTATE']:
-        img, exif = rotate_image(img, exif)
-
     if settings['PHOTO_EXIF_REMOVE_GPS']:
         exif.pop('GPS')
 
@@ -169,13 +141,11 @@ def resize_worker(orig, resized, spec, settings):
 
     logger.info('photos: make photo {} -> {}'.format(orig, resized))
     im = Image.open(orig)
-
-    if ispiexif and settings['PHOTO_EXIF_KEEP'] and im.format == 'JPEG':
-        # Only works with JPEG exif for sure.
+    if ispiexif and settings['PHOTO_EXIF_KEEP'] and im.format in ('JPEG', 'MPO'):
         try:
             im, exif_copy = manipulate_exif(im, settings)
-        except Exception:
-            logger.info('photos: no EXIF or EXIF error in {}'.format(orig))
+        except Exception as e:
+            logger.info('photos: no EXIF or EXIF error in {}:\n{}'.format(orig, e))
             exif_copy = b''
     else:
         exif_copy = b''
diff --git a/pelicanconf.py b/pelicanconf.py
index a2a2cfd..40af017 100644
--- a/pelicanconf.py
+++ b/pelicanconf.py
@@ -14,8 +14,10 @@ PLUGINS = ['advthumbnailer', 'photos']
 
 # Photos
 PHOTO_LIBRARY = "photos"
-PHOTO_GALLERY = (3840, 2160, 80)
-PHOTO_ARTICLE = (3840, 2160, 80)
+PHOTO_GALLERY = (3840, 2160, 90)
+PHOTO_ARTICLE = (3840, 2160, 90)
+PHOTO_THUMB = (384, 256, 90)
+PHOTO_EXIF_KEEP = True
 PHOTO_RESIZE_JOBS = 8
 
 MARKDOWN = {'extensions': ['toc(permalink=🔗)', 'codehilite(linenums=False)', 'extra']}
diff --git a/requirements.txt b/requirements.txt
index b2e16ac..58ccf80 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -4,4 +4,4 @@ Markdown==2.6.6
 pelican-advthumbnailer==0.7.0
 pelican-webassets==1.0.0
 piexif==1.1.3
-Pillow==5.0.0
+Pillow==7.2.0
diff --git a/theme/css/style.css b/theme/css/style.css
index 37a445e..d9fd006 100644
--- a/theme/css/style.css
+++ b/theme/css/style.css
@@ -362,6 +362,13 @@ main li {
 }
 
 
+/********** Gallery **********/
+.gallery img {
+    max-width: 192px;
+    max-height: 128px;
+}
+
+
 /********** Footer **********/
 body > footer {
     text-align: center;
-- 
GitLab