PIL image.open() working for some images but not others
Asked Answered
S

4

22

I use PIL to open AREA files from NOAA on a regular basis. In the last batch of images I received, the image.open() command simply does not work. Here is a simple code I wrote which yields the same results. It will open, rotate, and perform normal tasks with a file from a month ago, and not with a file from today.

from PIL import Image
im = Image.open("path/to/file")
im.show()

Here's the error:

File "image_goes.py", line 2, in <module>
im = Image.open("path/to/file")
File "/usr/lib/python2.6/dist-packages/PIL/Image.py", line 1980, in open
raise IOError("cannot identify image file")
IOError: cannot identify image file

Here's what I have tried:

  1. Opening the image on two separate machines.
  2. Changing the folder of the file in case there was a permission problem
  3. Redownloading the image, as well as two other batches, both using FTP manually AND our automatic script.

My hypothesis was that there was a problem with our downloading script and that it was not fully downloading the file, but that hypothesis is rejected by the fact that the new files are the correct size and that I manually downloaded them using an FTP client and got the same results.

My only other theory is that there is a problem with the NOAA files today or that they have been changed in such a way that PIL can no longer handle them, but I find that unlikely.

Any help greatly appreciated, Thanks

Spousal answered 30/7, 2012 at 18:41 Comment(3)
Could you give a link to one of the images, assuming that it's open?Clemons
do you know if the image format is the same? do your PIL installations have support for JPG/PNG/TIFF formats?Tawnatawney
The images are local, downloaded from the NOAA CLASS website. I have no reason to believe that the file format is different, as we've been using the same method for years.Spousal
W
20

Maybe be the content is not actually synced to the disk. try Image.open(open("path/to/file", 'rb'))

Wheedle answered 10/8, 2012 at 5:21 Comment(1)
I met the same problem with django-ckeditor. Immediate call of Image.open(filename) after filename was new created result in: "IOError: cannot identify image file". But later I call Image.openImage.open(filename) manually, no error occurred! So I tried to change Image.open(filename) to Image.open(open(filename, 'rb')) and then it is OK.Wheedle
C
4

For

im = Image.open("path/to/file")

Try

im = Image.open(r"path/to/file")

That is, put a r before the path string.

Celina answered 29/9, 2018 at 2:27 Comment(0)
S
1

You can use cv2 to read the image.

im = cv2.imread(path)

And, for opening/displaying, you can use:

cv2.imshow()
Serotonin answered 17/3, 2020 at 13:50 Comment(0)
H
0
from PIL import Image

# Load the image from the provided path
image_path = "/mnt/data/figure_instructions.jpg"

# Open the image file
image = Image.open(image_path)
image
Horizontal answered 6/6 at 22:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.