]> git.ipfire.org Git - ipfire.org.git/commitdiff
util: Optionally rotate JPEG images if coded into EXIF
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 6 Jan 2024 15:27:31 +0000 (15:27 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 6 Jan 2024 15:27:31 +0000 (15:27 +0000)
Some pictures store the rotation of the picture in the EXIF data which
we ignored in the past.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/backend/util.py

index 00747502ad023b7234eae07f87eac1367bf084c9..cddd677b48e0bb05773ae0964ba89de4f039df86 100644 (file)
@@ -1,5 +1,6 @@
 #!/usr/bin/python3
 
+import PIL.ExifTags
 import PIL.Image
 import PIL.ImageFilter
 import PIL.ImageOps
@@ -218,6 +219,20 @@ def generate_thumbnail(data, size, square=False, **args):
        # Save image format
        format = image.format
 
+       # Fetch any EXIF data
+       exif = image._getexif()
+
+       # Rotate the image
+       if exif:
+               for tag in PIL.ExifTags.TAGS:
+                       if PIL.ExifTags.TAGS[tag] == "Orientation":
+                               if exif[tag] == 3:
+                                       image = image.rotate(180, expand=True)
+                               elif exif[tag] == 6:
+                                       image = image.rotate(270, expand=True)
+                               elif exif[tag] == 8:
+                                       image = image.rotate( 90, expand=True)
+
        # Remove any alpha-channels
        if image.format == "JPEG" and not image.mode == "RGB":
                # Make a white background