IOError: cannot identify image file - python-imaging-library

Can anyone tell why PIL can't open this PNG file?
https://b75094855c6274df1cf8559f089f485661ae1156.googledrive.com/host/0B56ak7W-HmqAX005c3g5eTlBakE/8.png
I get IOError: cannot identify image file, and by looking at the code, it seems it tries PIL.PngImagePlugin.PngImageFile and corresponding "accept" function, and it returns False
I'm using version 1.1.6

I don't know what the problem is with PIL 1.1.6 but I just tested it with the latest Pillow 2.4.0 and this worked:
>>> from PIL import Image
>>> im = Image.open("8.png")
>>> im.show()
PIL in unmaintained and Pillow is an actively maintained and developed fork. To use Pillow, first uninstall PIL, then install Pillow.
Further installation instructions here: http://pillow.readthedocs.org/en/latest/installation.html

try to import cv2
>>> from PIL import Image
>>> import cv2
>>> im = cv2.imread("8.png")
>>> im = cv2.imwrite("8.png")
i hope its working...

Related

pySerial installed, but still getting ImportError

I am new to Python, so I have likely done something obviously wrong, though despite my best efforts I cannot figure out what.
I am running windows 7 64bit.
I only have Python 3.5 (32 bit) installed.
I updated pip to the latest version succesfully and used it to install pySerial. I am working in eclipse oxygen with PyDev installed. My run configuration does show the appropriate path (as far as I can tell):
run configuration in eclipse/PyDev
I have confirmed pySerial is installed by doing the following in python interpreter:
>>>help()
>>>modules
serial shows up in the list of modules.
also:
>>> import serial; print(serial.__file__)
C:\Python35\lib\site-packages\serial\__init__.py
Additionally, trying to use serial in the terminal works fine, as below:
>>> import serial
>>> s=serial.Serial("COM4")
>>> s
Serial<id=0x383b750, open=True>(port='COM4', baudrate=9600, bytesize=8, parity='N', stop
bits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)
However, when I try to run the following code as a file:
import serial
print ("Test Script")
I get the following error:
Traceback (most recent call last):
File "C:\Users\H260643\Desktop\Projects\EclipseWorkspace\CMP_Thous_Hr\Base.py", line 1, in <module>
import serial
ImportError: No module named 'serial'
For what it's worth, the eclispe editor window has also flagged that line saying it is an "unresolved import: serial"
Right after posting this I realized that the install path for python/pySerial did not match what was in my eclispe/PyDev run configuration. I corrected my run configuration and all is well.

PIL thinks every image I give it is corrupted

I'm trying to get an array of RGB values for a jpeg. Using the code
from PIL import Image
im = Image.open('lion.jpg')
pix = list(im.getdata())
gives the error
IOError: broken data stream when reading image file
The picture in the code is this one, which I got from google for testing:
but the code returns the same error for every picture I've tried, from a variety of sources (google, phone, etc...). When I push forward and load the image anyway, all the pixel values are black.
I've tried doing
from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
but all that does is suppress the error; the pixel values are still black.
It seems other people have had this problem elsewhere, like here, but so far other solutions have failed me. Any suggestions?
I'm on Mac 10.10, with python 2.7.8. I first installed Pillow with pip, and then from the source. I have confirmed that libjpeg is installed.
I don't really have an answer, but I can give you the setup of mine for which this is working fine.
The following script:
from PIL import Image
if __name__ == '__main__':
im = Image.open('lion.jpg'))
pix = list(im.getdata())
print('The image has {} pixels'.format(len(pix)))
import PIL, sys
print('Using Python version {}'.format(sys.version))
print('Using PIL version {}'.format(PIL.VERSION))
print('Using Pillow version {}'.format(PIL.PILLOW_VERSION))
Gives as a result on my Macbook with OS X El Capitan (10.11.6):
The image has 1183000 pixels
Using Python version 3.5.1 |Continuum Analytics, Inc.| (default, Dec 7 2015, 11:24:55)
[GCC 4.2.1 (Apple Inc. build 5577)]
Using PIL version 1.1.7
Using Pillow version 3.1.0
Process finished with exit code 0

Google Cloud ML scipy.misc.imread returning <PIL.JpegImagePlugin.JpegImageFile>

I am running the following snippet:
import tensorflow as tf
import scipy.misc
from tensorflow.python.lib.io import file_io
file = file_io.FileIO('gs://BUCKET/data/celebA/000007.jpg', mode='r')
img = scipy.misc.imread(file)
If I run that snippet in Cloud Console, I get back a proper array. But when that same snippet runs in Cloud ML, the img object is
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=178x218 at 0x7F1F8F26DA10>
This stackoverflow answer suggests that libjpeg was not installed when PIL was installed. The Cloud ML Runtime Version list shows that for Tensorflow 0.12, libjpeg-dev is an installed debian package.
I was able to reproduce this issue on Cloud ML, and it seems to be an issue with the version of file_io in Tensorflow 0.12.1, and goes away if Tensorflow 1.0 is installed.
If you can, upgrade to the 1.0 build of TF.
If you need a 0.12 version, the Cloud ML "0.12" runtime uses the official 0.12.1 build of TF, but you can upload your own version to install if you like. I did not track down exactly when the issue was fixed but a Nightly Tensorflow build from Feb 2nd seemed to work.

py2app crashes with PIL errors on earlier versions of OS X

A similar question was asked before by someone else: https://stackoverflow.com/questions/23600028/py2app-app-crashes-on-earlier-mac-os-x-versions
I have an app that runs fine on my machine (OSX 10.9.4), but when I move it to another machine running 10.7.5, the app crashes on start up. I get the following messages in console:
Traceback (most recent call last):
File "/PATH_TO_APP/APP.app/Contents/Resources/__boot__.py", line 3
_recipes_pil_prescript(['Hdf5SubImagePlugin', 'FitsStubImagePlugin', 'SunImagePlugin', 'GbrImagePlugin', 'Jpeg2KImagePlugin', 'MicImagePlugin', 'FpxImagePlugin', ImImagePlugin', ...
File "/PATH_TO_APP/APP.app/Contents/Resources/__boot__.py, line 3
from PIL import Image
File "PIL/Image.pyc", line 62, in <module>
File "PIL/_imaging.pyc", line 14, in <module>
File "PIL/_imaging.pyc", line 10, in __load
ImportError: dlopen(/PATH_TO_APP/APP.app/Contents/Resources/lib/python2.7/lib-dynload/PIL/_imaging.so, 2): Symbol not found: ___sincos_stret
Referenced from /PATH_TO_APP/APP.app/Contents/Resources/lib/python2.7/lib-dynload/PIL/_imaging.so
Expected in: /usr/lib/libSystem.B.dylib
in /PATH_TO_APP/APP.app/Contents/Resources/lib/python2.7/lib-dynload/PIL/_imaging.so
Earlier, to get around this error, I just uninstalled pillow. I'm not sure why pillow libraries were being added to the app to begin with, since I didn't think I was using it. But now, when attempting this workaround, I get another error:
Traceback (most recent call last):
File "/PATH_TO_APP.app/APP.app/Contents/Resources/__boot__.py", line 384, in <module> _recipes_pil_prescript([])
File "/PATH_TO_APP.app/APP.app/Contents/Resources/__boot__.py", line 344, in _recipes_pil_prescript from PIL import Image
File "/usr/local/lib/python2.7/site-packages/PIL/Image.py", line 62, in <module>
ImportError cannot import name _imaging
Apparently I'm using PIL now somehow. My script imports the following:
from __future__ import division
import easygui as eg
import os, zlib, re, datetime, getpass, tkMessageBox, sys
import zipfile as z
import Tkinter as tk
from tkFileDialog import askdirectory
from optparse import OptionParser
Here's my setup.py:
from setuptools import setup
APP = ['zipperscript.py']
DATA_FILES = []
OPTIONS = {'argv_emulation': True, 'excludes': ['']}
setup(
app=APP,
name="Zipperscript_HI_50",
data_files=DATA_FILES,
options={'py2app': OPTIONS},
setup_requires=['py2app'],
version="Hawaii branch 4.0.50"
)
I'm not sure that the OS version is causing the problem, but that's my theory right now. I'm not sure how to proceed. Any help or suggestions are greatly appreciated.
I encountered a similar problem while using buildout on Mac OS 10.8.5. It seems that the pre-compiled packages available on Pypi have been compiled on newer versions of OS X and unfortunately insufficiently tested on older versions.
The solution is to install pillow from source instead. Download the source, and run pip install your_downloaded_file. You may need to install dependencies such as libjpeg and zlib beforehand.
After doing this I do not encounter the errors anymore and have pillow version 5.4.0 installed.

Error in images2gif.py with GlobalPalette

Getting an error when trying to convert sequence of jpgs to gifs. Can't seem to figure out how to add a palette, or if that's the actual problem. Was able to get gifs to load using the numpy arrays in the images2gif.py main.
import PIL
from PIL import Image
import StringIO
import images2gif
images = []
for frame in animation1.frames:
img_data = s3manager.get_file_as_string(frame.s3_filename)
image = Image.open(StringIO.StringIO(img_data))
images.append(image)
images2gif.writeGif('lala3.gif', images, duration=0.5, dither=0)
With this I get the following error:
"images2gif.py", line 436, in writeGifToFile
fp.write(globalPalette)
TypeError: must be string or buffer, not None
Not sure how to specify a palette for these jpgs. documentation unclear to me, and not even sure if that's the issue. help?
In images2gif.py change line 200:
for im in images:
palettes.append( getheader(im)[1] )
to
for im in images:
palettes.append(im.palette.getdata()[1])
images2gif author seems to be willing to drop support for pillow. See this thread :
https://code.google.com/p/visvis/issues/detail?id=81
From this thread too, i found a fixed version of that script, which works with me (with pillow 2.4). It is available here :
https://github.com/rec/echomesh/blob/master/code/python/external/images2gif.py and produces good quality gif with any kind of PNG (P mode too)
images2gif.py uses getheader function from PIL.GifImagePlugin to get the palettes.
For some reason, it doesn't work with image you read. Maybe the script doesn't really work if source images are not 'P' mode.
I installed PIL after Pillow and it started to work. Seems like both libraries are needed for some reason. Here is how to reinstall both:
pip uninstall PIL
pip uninstall Pillow
pip install Pillow
pip install PIL