Skip to content

Commit

Permalink
MPR Fix image orientation issue
Browse files Browse the repository at this point in the history
Fix image orientation issue
  • Loading branch information
pkong-ds authored Aug 19, 2024
2 parents 2f71277 + 0bd0cf7 commit 2f87736
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion mockup_package/mockup/image_generator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import mockup.helpers as h
from PIL import Image
from PIL import Image, ExifTags
import cv2
import numpy as np

Expand Down Expand Up @@ -38,6 +38,23 @@ def __init__(self, original_img_path, phone_slug, device_info):
def create_fit_resolution_image(self):
device_ratio = self.d_size[0] / self.d_size[1]
image = Image.open(self.original_img_path).convert("RGBA")

# Solve image orientation issue:
# some picture has orientation property, which the browser knows and can automatically rotate the image # noqa: E501
# but the property is lost when we mockup the image, so need to fix it manually. # noqa: E501
# ref: https://stackoverflow.com/questions/13872331/rotating-an-image-with-orientation-specified-in-exif-using-python-without-pil-in # noqa: E501
for orientation in ExifTags.TAGS.keys():
if ExifTags.TAGS[orientation] == "Orientation":
break

exif = image.getexif()
if exif[orientation] == 3:
image = image.rotate(180, expand=True)
elif exif[orientation] == 6:
image = image.rotate(270, expand=True)
elif exif[orientation] == 8:
image = image.rotate(90, expand=True)

find_original_image_dim_process = str(image.size[0]) + "x" + str(image.size[1])
original_image_dim = tuple(
map(
Expand Down
Binary file modified public/mockup.zip
Binary file not shown.

0 comments on commit 2f87736

Please sign in to comment.