Skip to content

Commit

Permalink
Fix rotated images
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloalba committed Mar 29, 2024
1 parent 5e2a87a commit e6ac846
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion api/trotamundos.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,37 @@ def read_trips():
return {}


def reorient_image(im):
try:
image_exif = im._getexif()
image_orientation = image_exif[274]
if image_orientation in (2,'2'):
return im.transpose(Image.FLIP_LEFT_RIGHT)
elif image_orientation in (3,'3'):
return im.transpose(Image.ROTATE_180)
elif image_orientation in (4,'4'):
return im.transpose(Image.FLIP_TOP_BOTTOM)
elif image_orientation in (5,'5'):
return im.transpose(Image.ROTATE_90).transpose(Image.FLIP_TOP_BOTTOM)
elif image_orientation in (6,'6'):
return im.transpose(Image.ROTATE_270)
elif image_orientation in (7,'7'):
return im.transpose(Image.ROTATE_270).transpose(Image.FLIP_TOP_BOTTOM)
elif image_orientation in (8,'8'):
return im.transpose(Image.ROTATE_90)
else:
return im
except (KeyError, AttributeError, TypeError, IndexError):
return im

def save_image(filename, data):
filename = "data/images/" + secure_filename(filename)
with open(filename, 'wb') as f:
f.write(data)

image = Image.open(filename)
resized = image.thumbnail((1920, 1920))
image = reorient_image(image)
image.thumbnail((1920, 1920))
image.save(filename, 'jpeg')
return filename

Expand Down

0 comments on commit e6ac846

Please sign in to comment.