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

Cannot get exif data #6827

Closed
nsosnsos opened this issue Dec 26, 2022 · 17 comments
Closed

Cannot get exif data #6827

nsosnsos opened this issue Dec 26, 2022 · 17 comments
Labels

Comments

@nsosnsos
Copy link

nsosnsos commented Dec 26, 2022

#!/usr/bin/env python3
# -*- coding:utf-8 -*-

import sys
from PIL import Image, ExifTags


if __name__ == '__main__':
    image = Image.open(sys.argv[1])
    exif_info = image.getexif()
    exif_dict = {ExifTags.TAGS[tag]: value for tag, value in exif_info.items()}
    print(exif_dict)

What did you do?

call getexif interface

What did you expect to happen?

return full exif information.

What actually happened?

No exif data, return empty dict. Actually exif_info is an empty dict.

What are your OS, Python and Pillow versions?

  • OS: Ubuntu 22.04.1 LTS
  • Python: 3.10.6
  • Pillow: 9.3.0
@radarhere
Copy link
Member

Could you attach your image?

@nsosnsos
Copy link
Author

nsosnsos commented Dec 26, 2022

I tried many images, and I verified that there are exif info by other exif tools.

@radarhere
Copy link
Member

If I run your code against https://github.com/python-pillow/Pillow/blob/main/Tests/images/exif.png, I get {'Orientation': 1}.

Unless you don't see that for exif.png, then there must be something specific about your image.

@radarhere radarhere added the Exif label Dec 26, 2022
@radarhere radarhere changed the title Can not get exif data Cannot get exif data Dec 26, 2022
@nsosnsos
Copy link
Author

nsosnsos commented Dec 26, 2022

You are right. I get data as you did.
But, I thought getexif could get the same data as other exif tools. Does Pillow defined on what conditions and what format is valid?

@radarhere
Copy link
Member

For JPG and MPO, we read information from the APP1 marker.
For PNG, we read information from the eXIf chunk, or an EXIF tEXt chunk.
For WebP, we read information from the EXIF chunk.

Is your image in one of those formats, or something else?

@nsosnsos
Copy link
Author

nsosnsos commented Dec 28, 2022

IMG_20031001_113300
This is my picture, some online metadata check could parse "datetime original" from exif.
At least, windows operating system can identify the taken date.
So, I insist that pillow should enhance the ability to parse EXIF robustly.

@radarhere
Copy link
Member

Ok, it is a JPG image.

Somewhat confusingly, https://exiftool.org/TagNames/EXIF.html lists EXIF tags, and one of the groups listed is an ExifIFD. DateTimeOriginal is part of the ExifIFD.

So if I run the following code with your image,

import sys
from PIL import Image, ExifTags


if __name__ == '__main__':
    image = Image.open(sys.argv[1])
    exif_info = image.getexif()
    for tag, value in exif_info.items():
    	print("Base:", ExifTags.TAGS.get(tag, "Unknown "+str(tag)), value)
    for tag, value in exif_info.get_ifd(34665).items():
    	print("Exif:", ExifTags.TAGS.get(tag, "Unknown "+str(tag)), value)

part of the output is

Exif: SubsecTimeOriginal 83
Exif: SubsecTimeDigitized 83
Exif: DateTimeOriginal 2003:10:01 11:33:44
Exif: DateTimeDigitized 2003:10:01 11:33:44

When the next version of Pillow is released in a few days with #6748, you can replace exif_info.get_ifd(34665) with the clearer exif_info.get_ifd(ExifTags.IFD.Exif).

Alternatively, if you would prefer that Pillow should give you all of this information at once, we do still have _getexif() which does that.

import sys
from PIL import Image, ExifTags


if __name__ == '__main__':
    image = Image.open(sys.argv[1])
    exif_info = image._getexif()
    exif_dict = {ExifTags.TAGS.get(tag): value for tag, value in exif_info.items()}
    print(exif_dict)

gives

{ ... 'SubsecTimeOriginal': '83', 'SubsecTimeDigitized': '83', 'DateTimeOriginal': '2003:10:01 11:33:44', 'DateTimeDigitized': '2003:10:01 11:33:44'}

@nsosnsos
Copy link
Author

nsosnsos commented Dec 29, 2022

image

Unfortunately, it does not work for me.

@radarhere
Copy link
Member

_getexif() returning "None" means that there isn't any im.info["exif"] data. That is very strange if I'm looking at the same image that you are. Normally I'd ask what version of libjpeg you are running, but this should be before libjpeg does anything.

Here is the output of a GitHub Actions job with a fresh install of Pillow in Ubuntu 22.04 with Python 3.10, to demonstrate that this should be working - https://github.com/radarhere/Pillow/actions/runs/3798463170/jobs/6460192381#step:7:12

What does this output?

from PIL import Image
image = Image.open("/home/ubuntu/IMG_20031001_113300.jpg")
print(list(image.info.keys()))
print(image.applist)

How did you install Pillow? It might sound weird, but have you tried uninstalling Pillow and re-installing it?

If I put together a branch with some debugging statements to try and investigate this further, would you be willing to install that version of Pillow to see what happens?

@nsosnsos
Copy link
Author

nsosnsos commented Dec 29, 2022

image

I setup python virtualenv, then pip3 install pillow. Never thought it could be some error on installation.

Of course, I would like to try everything, as long as I can get a solution.

BTW, I am on a ARM arch vm, and I don't know if because of some system related compatibility error.

@radarhere
Copy link
Member

Ok, if you could run

pip3 install git+https://github.com/radarhere/Pillow.git@6827_debug

That will install https://github.com/radarhere/Pillow/tree/6827_debug with the print statements in radarhere@2a9b9e2.

Then call Image.open on the file again, and let me know what the debugging statements print out.

@nsosnsos
Copy link
Author

image
image

Oops!

@radarhere
Copy link
Member

You're building from Pillow from source code now, so some dependencies are required.

The minimal set of dependencies should be sudo apt-get install libjpeg8-dev zlib1g-dev.

@nsosnsos
Copy link
Author

You're building from Pillow from source code now, so some dependencies are required.

The minimal set of dependencies should be sudo apt-get install libjpeg8-dev zlib1g-dev.

>>> from PIL import Image
>>> image = Image.open('/home/ubuntu/IMG_20031001_113300.jpg')
Hash: c817bfdb8a88f768b866df65f4796af4a6f7cf191798efbdec03fb77a846cf17

fp.tell(): 4 i: 0xFF 65504
Marker: APP0 Handler: <function APP at 0x7f341cfe7400>
APP 65504 s b'JFIF\x00\x01\x01\x00\x00\x01' fp.tell(): 20
fp.tell(): 22 i: 0xFF 65499
Marker: DQT Handler: <function DQT at 0x7f341cfe75b0>
fp.tell(): 91 i: 0xFF 65499
Marker: DQT Handler: <function DQT at 0x7f341cfe75b0>
fp.tell(): 160 i: 0xFF 65472
Marker: SOF0 Handler: <function SOF at 0x7f341cfe7520>
fp.tell(): 179 i: 0xFF 65476
Marker: DHT Handler: <function Skip at 0x7f341cf9d090>
fp.tell(): 212 i: 0xFF 65476
Marker: DHT Handler: <function Skip at 0x7f341cf9d090>
fp.tell(): 395 i: 0xFF 65476
Marker: DHT Handler: <function Skip at 0x7f341cf9d090>
fp.tell(): 428 i: 0xFF 65476
Marker: DHT Handler: <function Skip at 0x7f341cf9d090>
fp.tell(): 611 i: 0xFF 65498
Marker: SOS Handler: <function Skip at 0x7f341cf9d090>
>>> exif = image._getexif()
>>> print(exif)
None

@radarhere
Copy link
Member

Thanks.

When I run it with https://user-images.githubusercontent.com/25956878/209842976-c0dca8ee-81d2-4353-8987-a53e97fd89db.jpg, I get a hash of 3ffa732571be0f3a76e96093c2499870b71bfbce745270fced2ec12aa5e1c9ff. That is a different hash to what you have shown me. That means that the file you're testing and the file that I'm downloading from GitHub are actually different.

So I can see two possible actions at this point.

  1. You could download https://user-images.githubusercontent.com/25956878/209842976-c0dca8ee-81d2-4353-8987-a53e97fd89db.jpg, and test it with Pillow. I expect the EXIF data to show then.
  2. You could upload the image that you are testing. I would encourage you to first check the image with whatever site that showed you the DatetimeOriginal though, as if Pillow isn't finding anything, I suspect there isn't actually EXIF data in the image.

@nsosnsos
Copy link
Author

Great, thanks. I found the mistake that I made. I overwrite the image with new generated thumbnail, no wonder I failed to get the exif data.
I am closing the issue.

@radarhere
Copy link
Member

When the next version of Pillow is released in a few days with #6748, you can replace exif_info.get_ifd(34665) with the clearer exif_info.get_ifd(ExifTags.IFD.Exif).

Pillow 9.4.0 has now been released.

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

No branches or pull requests

2 participants