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

PIL.Image.open is rotating jpeg images #4703

Closed
nicolasch96 opened this issue Jun 16, 2020 · 7 comments
Closed

PIL.Image.open is rotating jpeg images #4703

nicolasch96 opened this issue Jun 16, 2020 · 7 comments

Comments

@nicolasch96
Copy link

nicolasch96 commented Jun 16, 2020

Hello,
I am trying to load images with Image.open. However, the images are autorotated if they were jpg images.

@hugovk
Copy link
Member

hugovk commented Jun 16, 2020

Thank you for reporting an issue.

Follow these guidelines to ensure your issue is handled properly.

If you have a ...

  1. General question: consider asking the question on Stack Overflow
    with the python-imaging-library tag:

    Do not ask a question in both places.

    If you think you have found a bug or have an unexplained exception
    then file a bug report here.

  2. Bug report: include a self-contained, copy-pastable example that
    generates the issue if possible. Be concise with code posted.
    Guidelines on how to provide a good bug report:

    Bug reports which follow these guidelines are easier to diagnose,
    and are often handled much more quickly.

  3. Feature request: do a quick search of existing issues
    to make sure this has not been asked before.

We know asking good questions takes effort, and we appreciate your time.
Thank you.

What did you do?

What did you expect to happen?

What actually happened?

What are your OS, Python and Pillow versions?

  • OS:
  • Python:
  • Pillow:
code goes here

@radarhere radarhere changed the title PIL.IMAGE.OPEN is rotating jpeg images PIL.Image.open is rotating jpeg images Jun 16, 2020
@radarhere
Copy link
Member

radarhere commented Jun 17, 2020

I would imagine that the images you are dealing with have an EXIF orientation tag. This means that the image data is saved in one position, and then the image instructs the viewer to rotate it another way.

Pillow does not automatically apply this transformation to the data. However, it has a method to do so - https://pillow.readthedocs.io/en/stable/reference/ImageOps.html#PIL.ImageOps.exif_transpose

Here is an example using it. You can try this and check if it resolves your problem.

from PIL import Image, ImageOps
im = Image.open("file.jpg")
im = ImageOps.exif_transpose(im)

However, this is all a guess. To be sure, please provide an image, and ideally, the code that you are running over it.

@nicolasch96
Copy link
Author

I would imagine that the images you are dealing with have an EXIF orientation tag. This means that the image data is save in one position, and then the image instructs the viewer to rotate it another way.

Pillow does not automatically apply this transformation to the data. However, it has a method to do so - https://pillow.readthedocs.io/en/stable/reference/ImageOps.html#PIL.ImageOps.exif_transpose

Here is an example using it. You can try this and check if it resolves your problem.

from PIL import Image, ImageOps
im = Image.open("file.jpg")
im = ImageOps.exif_transpose(im)

However, this is all a guess. To be sure, please provide an image, and ideally, the code that you are running over it.

Hello, yes it is true. Thank you very much.

@Divjyot
Copy link

Divjyot commented Nov 19, 2020

Pillow 8.0.0 is automatically rotating images upon .open based on EXIF Orientation tag. Is there a way to stop that? Automatic rotation is NOT expected from Pillow.

I have an image with following EXIF:
{'ExifVersion': b'0221',
'ComponentsConfiguration': b'\x01\x02\x03\x00',
'FlashPixVersion': b'0100',
'ColorSpace': 1,
'ExifImageWidth': 4032,
'ExifImageHeight': 3024,
'SceneCaptureType': 0,
'ResolutionUnit': 2,
'ExifOffset': 102,
'Orientation': 3,
'YCbCrPositioning': 1,
'XResolution': 72.0,
'YResolution': 72.0}
When, I perform Image.open(file_bytes) , result is automatically rotated image.
I wanted to stop that behaviour and return a NON-Rotated image i.e. same as original.

@radarhere
Copy link
Member

@Divjyot would you be able to open a new issue with a copy of the image attached?

@kxrob
Copy link

kxrob commented Jan 11, 2023

Doing the exif_transpose() for a .jpg I get this error:

  File "C:\Python38\lib\site-packages\PIL\ImageOps.py", line 602, in exif_transpose
    transposed_image.info["exif"] = transposed_exif.tobytes()
  File "C:\Python38\lib\site-packages\PIL\Image.py", line 3628, in tobytes
    return b"Exif\x00\x00" + head + ifd.tobytes(offset)
  File "C:\Python38\lib\site-packages\PIL\TiffImagePlugin.py", line 878, in tobytes
    data = ifd.tobytes(offset)
  File "C:\Python38\lib\site-packages\PIL\TiffImagePlugin.py", line 887, in tobytes
    "<table: %d bytes>" % len(data) if len(data) >= 16 else str(values)
TypeError: object of type 'IFDRational' has no len()

At post-mortem:

>>> PIL.__version__
'9.3.0'
>>> data
2.2
>>> type(data)
<class 'PIL.TiffImagePlugin.IFDRational'>
>>> len(data)
Traceback (most recent call last):
  File "<interactive input>", line 1, in <module>
TypeError: object of type 'IFDRational' has no len()

# stack level with image:
>>> im.getexif()[274]  # Orientation
6
>>> im.getexif()[282]  # XResolution
72.0
>>> im.getexif()[283]  # YResolution
72.0
>>> type(_)
<class 'PIL.TiffImagePlugin.IFDRational'>

Is this a(nother) bug, or what to do?

@radarhere
Copy link
Member

The above comment was continued as #6882

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

5 participants