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.UnidentifiedImageError: cannot identify image file (Custom installation in other directories) #7575

Closed
deliangyang opened this issue Nov 27, 2023 · 6 comments · Fixed by #7576

Comments

@deliangyang
Copy link
Contributor

deliangyang commented Nov 27, 2023

What did you do?

pip install -h |grep target
#  -t, --target <dir>        Install packages into <dir>. By default this
#                            multiple platforms supported by the target
#                            abis supported by the target interpreter.

mkdir -p packages
# remove the Pillow, which installed into system packages
pip uninstall Pillow
pip install -t packages Pillow

What did you expect to happen?

It imports plugins correctly

What actually happened?

It can't import plugins, when I didn't install Pillow into system packages directories

What are your OS, Python and Pillow versions?

  • OS: Debian
  • Python: 3.8.0
  • Pillow: All versions

The real format of the image is webp, you need to import PIL.WebPImagePlugin. The image is as follows:

3ae6c099102fba7a4773c3568edee0be

from packages.PIL import Image

with open('3ae6c099102fba7a4773c3568edee0be.jpg', 'rb') as fr:
    Image.open(fr)
Traceback (most recent call last):
  File "test2.py", line 4, in <module>
    Image.open(fr)
  File "/app/packages/PIL/Image.py", line 3023, in open
    raise UnidentifiedImageError(
packages.PIL.UnidentifiedImageError: cannot identify image file <_io.BufferedReader name='3ae6c099102fba7a4773c3568edee0be.jpg'>
@radarhere
Copy link
Member

I'm not convinced that from packages.PIL import Image is the correct thing to do after specifying a target directory, rather than continuing to use from PIL import Image.

Googling, https://stackoverflow.com/a/23435273/4093019 recognises that normal imports aren't going to work this way. https://stackoverflow.com/a/19404371/4093019 and https://stackoverflow.com/a/76161892/4093019 suggest that you add the new directory to your path.

@deliangyang
Copy link
Contributor Author

deliangyang commented Nov 27, 2023

In AWS s3 lambda, we can't run the pip command to install our packages, we have to package the dependencies with the code. As in the example above, with the dependencies in the same directory.

@homm
Copy link
Member

homm commented Nov 27, 2023

@deliangyang you still can configure sys.path

@deliangyang
Copy link
Contributor Author

deliangyang commented Nov 27, 2023

Thanks, it's work. Why don't we use relative path to fix it?

@homm
Copy link
Member

homm commented Nov 27, 2023

As of all other imports in PIL dir are relative, I consider absolute paths for plugins as a bug

@deliangyang
Copy link
Contributor Author

deliangyang commented Nov 27, 2023

Thank you for your time.

@radarhere radarhere changed the title PIL.UnidentifiedImageError: cannot identify image file(Custom installation in other directories) PIL.UnidentifiedImageError: cannot identify image file (Custom installation in other directories) Nov 28, 2023
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

Successfully merging a pull request may close this issue.

3 participants