PIL thinks every image I give it is corrupted - python-imaging-library

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

Related

RED lines under libraries names

I just finished downloading pillow library and when i came to pycharm and type " import PIL " still underlined by red, saying " no module named PIL ". i've checked on youtube how i bring pillow's functions i am correct its " import PIL " i've checked even on the site it makes me think im wrong i lost my self confidence cause i can't fix it im in the same position for 2 weeks help please
i beleived that maybe 3.10 vesrion of python doesn't match with pillow library so i deleted it and i've downloaded 3.9 and i don't know im confused im lost

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.

IOError: cannot identify image file

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...

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

compiling pdflatex using knitr error

I have installed Lyx 2.0.5 and have Miktex 2.9 installed running in Win 7.0 as well as R Ver 2.15
I have installed knitr in R with simple code
<<>>=
1+1
#
and can use View DVI to produce a file in Yak
When I try and use "eyes" on the bar to produce a PDF (pdflatex) I get the following message.
Can anyone give me some idea what the problem might be
Thanks