Skip to content

Commit

Permalink
fixup! WIP;Rotate the image before saving
Browse files Browse the repository at this point in the history
- This commit successfully rotates and saves an image with exif data
  • Loading branch information
VirginiaDooley committed Apr 11, 2023
1 parent 8cd60e3 commit dd6d004
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 33 deletions.
9 changes: 4 additions & 5 deletions ynr/apps/moderation_queue/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.urls import reverse
from moderation_queue.helpers import convert_image_to_png, rotate_photo_check
from moderation_queue.helpers import convert_image_to_png, rotate_photo
from people.forms.forms import StrippedCharField

from .models import CopyrightOptions, QueuedImage, SuggestedPostLock
Expand Down Expand Up @@ -54,10 +54,9 @@ def save(self, commit):
Before saving, rotate the image and convert the image to a PNG.
This is done while the image is still an InMemoryUpload object
"""
rotated_image = rotate_photo_check(
filename=self.instance.image.name, image=self.instance.image
)
png_image = convert_image_to_png(rotated_image)
photo = rotate_photo(image=self.instance.image)
# pass a PIL image to the convert_image_to_png function
png_image = convert_image_to_png(photo)
filename = self.instance.image.name
extension = filename.split(".")[-1]
filename = filename.replace(extension, "png")
Expand Down
48 changes: 20 additions & 28 deletions ynr/apps/moderation_queue/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
from .models import QueuedImage
from django.shortcuts import render
from PIL import Image as PillowImage
from PIL import ExifTags

# from PIL import ImageOps
from PIL import ExifTags, ImageOps


def upload_photo_response(request, person, image_form, url_form):
Expand Down Expand Up @@ -47,57 +45,51 @@ def image_form_valid_response(request, person, image_form):
)


def convert_image_to_png(image):
def convert_image_to_png(photo):
# Some uploaded images are CYMK, which gives you an error when
# you try to write them as PNG, so convert to RGBA (this is
# RGBA rather than RGB so that any alpha channel (transparency)
# is preserved).
original = PillowImage.open(image)
converted = original.convert("RGBA")
converted = photo.convert("RGBA")
bytes_obj = BytesIO()
converted.save(bytes_obj, "PNG")
return bytes_obj


def rotate_photo(image, filename):
def rotate_photo(image):
original = PillowImage.open(image)
# Warning: There may still be some images that are not rotated correctly
# and do not have exif data.

# TO DO: some images are not rotated correctly and do not have exif data. How
# can we detect this and rotate them correctly?

try:
for orientation in ExifTags.TAGS.keys():
if ExifTags.TAGS[orientation] == "Orientation":
break

exif = original._getexif()
fp = filename
if exif:
# clearing exif data does not rotate the image
if exif[orientation] == 1 or 2 or 5 or 6:
if exif[orientation] == 1:
# this usually indicates a horizontal photo,
# uploaded by mobile device

# The rotate() method of Python Image Processing Library Pillow
# Takes the number of degrees as a parameter and
# rotates the image in Counter Clockwise
# Direction to the number of degrees specified

# set exif data to 0 so it doesn't undermine the rotation step before save
exif[orientation] = 0
# rotate the photo
rotated_photo = original.rotate(angle=270)
rotated_photo.show()
if exif[orientation] == 3 or 4:
exif[orientation] = 0
original.rotate(angle=180)
elif exif[orientation] == 7 or 8:
exif[orientation] = 0
original.rotate(angle=90)
else:
pass
# save the rotated photo to the same file path as the original photo
rotated_photo.save(fp, image)
# If an image has an EXIF Orientation tag, other than 1,
# return a new image that is transposed accordingly.
# The new image will have the orientation data removed.
# Otherwise, return a copy of the image.
rotated_photo = ImageOps.exif_transpose(original)
# save the rotated photo to the same file path as the original photo
rotated_photo.save(image)
original.close()
# TO DO : return the rotated photo in a format that can be saved to the database
return original
return image
return rotated_photo
return original
except (AttributeError, KeyError, IndexError):
pass

Expand Down

0 comments on commit dd6d004

Please sign in to comment.