Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The image is rotated 270 degrees after conversion #534

Closed
wendyAndJken opened this issue Jan 6, 2025 · 2 comments
Closed

The image is rotated 270 degrees after conversion #534

wendyAndJken opened this issue Jan 6, 2025 · 2 comments

Comments

@wendyAndJken
Copy link

The image is rotated 270 degrees after conversion。
The width and height read from the original file are wrong。

image

I can't find the relevant reason, can you help me take a look?Thank you so much!
Original file:
1

@ARM-software ARM-software deleted a comment from Dreamer1 Jan 6, 2025
@solidpixel
Copy link
Contributor

solidpixel commented Jan 6, 2025

Hi @wendyAndJken,

This issue occurs because your original JPEG is actually stored horizontally in the pixel data, and is presented as a portrait image in the OS because it is defined as "mirrored and rotated 270" using image EXIF metadata.

The current image loader (stb_image) in the compressor doesn't understand EXIF metadata, so the specified EXIF rotation is ignored and the compressor is using the raw pixels in their storage orientation.

The quickest workaround for this is to rotate the raw pixels before using the compressor. In Python 3, if you have the Pillow image processing package installed, the following code will apply the current EXIF transform to the pixels:

from PIL import Image, ImageOps
from PIL import ExifTags

# Input and output file names
input_image = 'issue_534_orig.jpg'
output_image = 'issue_534_transposed.jpg'

img = Image.open(input_image)

# Extract and print the EXIF info from the input image
img_exif = img.getexif()
orientation = img_exif.get(ExifTags.Base.Orientation)

if orientation is None:
    orientation_str = 'No EXIF rotation'
elif orientation == 1:
    orientation_str = '0° EXIF rotation'
elif orientation == 2:
    orientation_str = '0° EXIF rotation with mirror'
elif orientation == 3:
    orientation_str = '180° EXIF rotation'
elif orientation == 4:
    orientation_str = '180° EXIF rotation with mirror'
elif orientation == 5:
    orientation_str = '270° EXIF rotation with mirror'
elif orientation == 7:
    orientation_str = '90° EXIF rotation'
elif orientation == 6:
    orientation_str = '90° EXIF rotation with mirror'
elif orientation == 8:
    orientation_str = '270° EXIF rotation'
else:
    orientation_str = f'Unknown EXIF rotation value {orientation}'

print(f'Input image orientation: {orientation_str}')

# Rotate the pixel data to match the EXIF information
ImageOps.exif_transpose(img, in_place=True)
print(f'Input image orientation: 0° EXIF rotation')

# Save the transposed output file
img.save(output_image, quality=85)

@solidpixel
Copy link
Contributor

Raised #539 to look at adding EXIF support, but this won't be a quick fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants