What's wrong with the following snippet?
It's not related to the image format, I tried both with jpg and png.
import Image
from cStringIO import StringIO
with open('/path/to/file/image.png') as f:
data = f.read()
img = Image.open(StringIO(data))
img.load()
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
File "/usr/lib64/python2.7/site-packages/PIL/Image.py", line 2030, in open
raise IOError("cannot identify image file")
IOError: cannot identify image file
EDIT:
This does happen with a randomly downloaded picture from the internet and the following most basic snippet:
import Image
im = Image.open('WicZW.jpg')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib64/python2.7/site-packages/PIL/Image.py", line 2030, in open
raise IOError("cannot identify image file")
IOError: cannot identify image file
The problem was in the mutual presence of the PIL and Pillow library on the machine:
# pip freeze | grep -E '(Pillow|PIL)'
PIL==1.1.7
Pillow==2.1.0
I solved this by using
from PIL import Image
instead of just doing
import Image
Related
I am setting up a computer vision project to detect and process GFP proteins. I keep getting errors about my file not being a Tiff Image and a Byte Error. I don't quite understand what they mean and haven't found anything about it online.
I have already made sure that the file path is correct and have tried changing the file into Tiff Format. Now on Finder, it says that it is a TIFF Image but still gives an error.
import tifffile
from colicoords import Data, Cell, CellPlot
import matplotlib.pyplot as plt
binary_img = tifffile.imread('organoid_images/gfp/cells1.tif')
data = Data()
data.add_data(binary_img, 'binary')
cell = Cell(data)
cell.optimize()
cp = CellPlot(cell)
plt.figure()
cp.imshow('flu_514', cmap='viridis', interpolation='nearest')
cp.plot_outline()
cp.plot_midline()
plt.show()
Error Message:
Traceback (most recent call last):
File "/Users/CosmoCrash/opencvblobs/lib/python3.7/site-packages/tifffile/tifffile.py", line 2236, in __init__
byteorder = {b'II': '<', b'MM': '>'}[header[:2]]
KeyError: b'\x89P'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "gfp.py", line 6, in <module>
binary_img = tifffile.imread('organoid_images/gfp/cells1.tif')
File "/Users/CosmoCrash/opencvblobs/lib/python3.7/site-packages/tifffile/tifffile.py", line 715, in imread
with TiffFile(files, **kwargs_file) as tif:
File "/Users/CosmoCrash/opencvblobs/lib/python3.7/site-packages/tifffile/tifffile.py", line 2238, in __init__
raise TiffFileError('not a TIFF file')
tifffile.tifffile.TiffFileError: not a TIFF file
Your file starting \x89P is a PNG file, not TIFF, as that is a PNG signature, whereas TIFF files start II if in Intel order, or MM if in Motorola order.
If on Linux/macOS, try running:
file cells1.tif
See Wikipedia for description of PNG signature, as suggested by Warren.
See Wikipedia for description of TIFF header.
I want to bake a .gif file into .exe with pyinstaller. From my research I have to encode the gif to a string and decode that string for this to be possible. Encoding the string and decoding the string is working. I get an error when trying to use it as a animation resource in pyglet. Something in the encoding-decoding has broken the gif? If there is another preferred way of doing this please let me know!
Step1
import base64
with open("test.gif", "rb") as imageFile:
image = base64.b64encode(imageFile.read())
print(image)
Step2
Saving this string in a .py file like this:
image = b'AWggsegsegs/....'
Step3
import pyglet
import base64
from gif_string import image
decodedgif = base64.b64decode(image) # Works this far
animation = pyglet.resource.animation(decodedgif) # Error here
sprite = pyglet.sprite.Sprite(decodedgif)
Error message reads
Traceback (most recent call last):
File "...pyglet\resource.py", line 583, in animation
identity = self._cached_animations[name]
File "...AppData\Local\Programs\Python\Python36\lib\weakref.py", line 131, in __getitem__
o = self.data[key]()
KeyError: b'GIF89a
...
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "...\pyglet\resource.py", line 435, in file
location = self._index[name]
KeyError: b'GIF89a\
...
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\..., line 7, in <module>
animation = pyglet.resource.animation(decodedgif) # Error here
File "...\pyglet\resource.py", line 585, in animation
animation = pyglet.image.load_animation(name, self.file(name))
File "...\pyglet\resource.py", line 438, in file
raise ResourceNotFoundException(name)
pyglet.resource.ResourceNotFoundException: Resource "b'GIF89a\x00\
...
...f9\x88\xc0\xbe\xa4O"o\xcf\xe5\x81\x00\x00;'" was not found on the path.
Ensure that the filename has the correct captialisation.
Instead of pyglet.resource.animation() which can only load files, you should use pyglet.image.load_animation(). Its optional File parameter supports file-like objects, so you should construct one using io.BytesIO() and pass it there.
I am trying to get some code working that has broken but was working before. I have a PNG file on my desktop and I simply want to open it using the Image module from PIL.
from PIL import Image
img_dir = r'C:\Users\DylanDB\Desktop\square.png'
img = Image.open(img_dir)
This is a remake of my more advanced code that it happens in as well. The error is:
Traceback (most recent call last):
File "C:/Users/DylanDB/Desktop/img_test.py", line 5, in <module>
img = Image.open(img_dir)
File "C:\Python34\lib\site-packages\PIL\Image.py", line 2317, in open
% (filename if filename else fp))
OSError: cannot identify image file 'C:\\Users\\DylanDB\\Desktop\\square.png'
I had the same error and it was due to the file was recently created and not closed properly before opening with the Image.open(). After closing the file f.close() it werked as expect
I found that the file was a corrupted image.
So I'm using Wand to try to convert the pdf to an image
from wand.image import Image
with Image(filename="test.pdf") as img:
img.save(filename="/temp.jpg")
with Image(filename="test.jpg") as img:
img.resize(200, 150)
img.save(filename="t.jpg")
but for some reason i get:
Traceback (most recent call last):
File "C:\Users\Rafael\Desktop\k\pdf to image.py", line 3, in <module>
with Image(filename="test.pdf") as img:
File "C:\Python27\lib\site-packages\wand\image.py", line 2534, in __init__
self.read(filename=filename, resolution=resolution)
File "C:\Python27\lib\site-packages\wand\image.py", line 2601, in read
self.raise_exception()
File "C:\Python27\lib\site-packages\wand\resource.py", line 222, in raise_exception
raise e
DelegateError: PDFDelegateFailed `The system cannot find the file specified.
' # error/pdf.c/ReadPDFImage/798
Can i do something or is there another way to convert a pdf to an image?
Installing Ghostscript and adding
C:\Program Files (x86)\gs\gs9.06\bin
to your systempath should help to resolve this issue as it helped me to overcome this error....
The reason may be a missing ghostscript installation. This is a similar question on SO
There is another way. Looks like you are using windows so you need download "https://github.com/oschwartz10612/poppler-windows/releases/"
Because you are using Windows,so you need to state the path to poppler/bin folder:
code:
from pdf2image import convert_from_path
poppler_path = "C:\path\to\poppler\bin"
images = convert_from_path('example.pdf')
for i in range(len(images)):
images[i].save('page'+ str(i) +'.jpg', 'JPEG')
reference : https://www.geeksforgeeks.org/convert-pdf-to-image-using-python/
I'm using Python2.7.6 and Pillow 2.3.0 on 32 bits Windows. And I do not have PIL installed on my machine.
My problem is when I do following I get "cannot identify image file" error.
>>> from PIL import Image
>>> file = open(r"C:\\a.jpg", 'r')
>>> image = Image.open(file)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\pillow-2.3.0-py2.7-win32.egg\PIL\Image.py", line 2025, in open
IOError: cannot identify image file
But this works if I don't "Open" the file before opening it with Image.Open:
>>> image2 = Image.open(r"C:\\a.jpg", 'r')
NOTE: I cannot omit the "Open" statement.
Does anyone know what may be causing this strange behavior?
Thanks, in advance!
Don't do image = Image.open(file) , you already opened the file.
Try image = Image.open("C:\\a.jpg")
Here is the Image module: http://effbot.org/imagingbook/image.htm
EDIT:
Use 'rb' instea of 'r' when opening the file