How to solve IOError:broken data stream when reading image file? - python

Im working under Ubuntu 11.04. I'm trying to work with PIL in my Django project. Sadly PIL cannot load my image.
Here is the PIL setup summary:
PIL 1.1.7 SETUP SUMMARY
--------------------------------------------------------------------
version 1.1.7
platform linux2 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
[GCC 4.5.2]
--------------------------------------------------------------------
*** TKINTER support not available
--- JPEG support available
--- ZLIB (PNG/ZIP) support available
--- FREETYPE2 support available
--- LITTLECMS support available
--------------------------------------------------------------------
I tried coding in python shell and it returns the error when I called load() function:
>>> import PIL
>>> import Image
>>> import _imaging
>>> i = Image.open("someimage.jpg")
>>> i
<JpegImagePlugin.JpegImageFile image mode=RGB size=600x599 at 0x9646C0C>
>>> i.load()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/home/joseph/Desktop/thesis/env/lib/python2.7/site-packages/PIL/ImageFile.py", line 215, in load
raise_ioerror(e)
File "/home/joseph/Desktop/thesis/env/lib/python2.7/site-packages/PIL/ImageFile.py", line 52, in raise_ioerror
raise IOError(message + " when reading image file")
IOError: broken data stream when reading image file
I am also working in a Virtual Environment, I am having problems with uploading jpeg image on my Django project. PNG works fine but it rejects JPEG

sudo apt-get install libjpeg8-dev
rebuild PIL and install again.

I had the same problem, but unfortunately we were using RHEL which only had libjpeg 6b.
Digging into this problem further, this error seems to occur when you are using a JPEG feature that isn't supported in the version of libjpeg you're using. In my case it was arithmetic encoding (introduced in version 7).
I found the wikipedia page on libjpeg to be helpful in determining which types of JPEG files could be opened by which versions. so...
TL;DR
Upgrade libjpeg if you can or re-save your images as baseline JPEG's.

Use Pillow and report issues as they occur and the Pillow team will fix them. Bonus points for submitting a pull request along with your issue report. Also see: https://stackoverflow.com/a/7770547/185820

Related

Reading ETOPO tiff images into numpy array (Python)

I'm trying to read one of these images in Python https://www.ngdc.noaa.gov/mgg/global/relief/ETOPO2022/data/15s/15s_surface_elev_gtif/
They are greyscale tiff images. I managed to read them with the Pillow (PIL) library:
im = Image.open(filepath)
and all is well. I get something with dimensions 3600 x 3600, which seems reasonable.
Now I want to look at the pixel values so I try to create a numpy array:
imarray = numpy.array(im)
with various arguments, but I always end up with nothing (size = 1). Has anybody tried to read these images and successfully accessed the pixel values? Is the problem related to having 32 bit greyscale values?
I have also tried other ways, using imageio and cv2 libraries, but so far no luck. I'm relatively new to Python and these libraries, so maybe I'm heading in the wrong direction here. Has anyone tried and succeeded with these particular ones?
The images are compressed with "Adobe Deflate" compression. You can tell that with exiftool like this:
exiftool YOURIMAGE.TIF
...
...
Compression : Adobe Deflate
...
As that is the case, you need to have libtiff installed to decompress the images. You can tell that like this:
python3 -m PIL
--------------------------------------------------------------------
Pillow 9.1.1
Python 3.10.0 (v3.10.0:b494f5935c, Oct 4 2021, 14:59:19) [Clang 12.0.5 (clang-1205.0.22.11)]
--------------------------------------------------------------------
Python modules loaded from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/PIL
Binary modules loaded from /Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/PIL
--------------------------------------------------------------------
--- PIL CORE support ok, compiled for 9.1.1
--- TKINTER support ok, loaded 8.6
--- FREETYPE2 support ok, loaded 2.12.1
--- LITTLECMS2 support ok, loaded 2.14
--- WEBP support ok, loaded 1.2.4
--- WEBP Transparency support ok
--- WEBPMUX support ok
--- WEBP Animation support ok
--- JPEG support ok, compiled for libjpeg-turbo 2.1.3
--- OPENJPEG (JPEG2000) support ok, loaded 2.5.0
--- ZLIB (PNG/ZIP) support ok, loaded 1.2.12
--- LIBTIFF support ok, loaded 4.4.0
--- RAQM (Bidirectional Text) support ok, loaded 0.9.0, fribidi 1.0.12, harfbuzz 5.3.1
*** LIBIMAGEQUANT (Quantization method) support not installed
--- XCB (X protocol) support ok
--------------------------------------------------------------------
BLP
Extensions: .blp
Features: open, save, encode
--------------------------------------------------------------------
...
...
...
So, I imagine you need to install libtiff. How you do that depends on your OS.

Pillow was built without XCB support error

I am coding in an Android device using an app called Pydroid3.
I want to use ImageGrab from pillow to take screenshots,I created a python file which takes screenshots using ImageGrab,but when I import it to other python file it shows error "Pillow was built without XCB support".
I imported everything from that file.
How could I resolve it

No Resolution to Pygame Error

I am using anaconda [python 2.7] on ubuntu machine [15.04].
I need opencv, pygame, python 2.7 for my code to run.
I get the error on running my code:
Traceback (most recent call last):
File "deep_q_network.py", line 8, in <module>
import wrapped_flappy_bird as game
File "game/wrapped_flappy_bird.py", line 19, in <module>
IMAGES, SOUNDS, HITMASKS = flappy_bird_utils.load()
File "game/flappy_bird_utils.py", line 21, in load
pygame.image.load('assets/sprites/0.png').convert_alpha(),
pygame.error: File is not a Windows BMP file
Hours spent on this making the fix. TRied SO solutions. PLease help. Thanks.
Read the error: File is not a Windows BMP file. The image you're trying to load seems to be a .png: assets/sprites/0.png. To fix, save the image as a .bmp. Your pygame is missing support for other formats. Documentation quote:
The image module is a required dependency of Pygame, but it only
optionally supports any extended file formats. By default it can only
load uncompressed BMP images. When built with full image support, the
pygame.image.load() function can support the following formats.
To fix it, you need to install python imaging libraries.

UnsupportedOperation: fileno - How to fix this Python dependency mess?

I'm building quite an extensive Python backend and things were working quite good on server A. I then installed the system on a new (development) server B on which I simply installed all pip packages again from scratch. Things seemed to work fine, so I did a pip freeze. I then took that list and upgraded the packages on server A.
But, as you might expect I should have known better. I didn't test things on machine B wel enough, so I ran into a problem with Pillow version 3.0.0. So I downgraded to version 1.7.8. That solves that single problem, bug gives me another one:
File "/home/kramer65/theproject/app/models/FilterResult.py", line 26, in to_json
self.image.save(b, 'JPEG')
File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 1437, in save
save_handler(self, fp, filename)
File "/usr/local/lib/python2.7/dist-packages/PIL/JpegImagePlugin.py", line 471, in _save
ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)])
File "/usr/local/lib/python2.7/dist-packages/PIL/ImageFile.py", line 476, in _save
fh = fp.fileno()
UnsupportedOperation: fileno
And here I'm kinda lost. As far as I know this is a problem in Pillow itself, so I wouldn't know why it used to work and why it doesn't work anymore.
I searched around on the internet, but I couldn't find any solution.
Does anybody know what I could do to solve this?
ps. PIL is not installed, so it's not a collision between PIL and Pillow
[EDIT]
I just tested an import Image in Python (which would suggest that PIL is still installed). To my surprise that succeeds, even though pip tells me that it is not installed:
$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import Image
>>> exit()
$ sudo pip uninstall PIL
The directory '/home/hielke/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Cannot uninstall requirement PIL, not installed
BytesIO objects raise UnsupportedOperation (rather than AttributeError which StringIO does) when their fileno method is called that exception wasn't handled as it should be.
This was fixed in Pillow 3.0.0 by this commit https://github.com/python-pillow/Pillow/commit/197885164b22f82653af514e66c76f4b778c0b1b by catching the exception. The following is the fix. The rest of that commit are changes to the test suite.
In PIL/ImageFile.py:
## -29,6 +29,7 ##
import Image
import traceback, os
+import io
MAXBLOCK = 65536
## -475,7 +476,7 ## def _save(im, fp, tile):
try:
fh = fp.fileno()
fp.flush()
- except AttributeError:
+ except (AttributeError, io.UnsupportedOperation):
# compress to Python file-compatible object
for e, b, o, a in tile:
e = Image._getencoder(im.mode, e, a, im.encoderconfig)
You could simply patch 1.7.8 to handle the exception.
I finally managed to fix things. The reason I downgraded pillow from 3.0.0 to 1.7.8, is because those where the only two versions I saw listed on the Pillow Pypi package index. I finally remembered that I had one more server on which I once tested this code and there it was still working. A quick pip freeze told me that it had Pillow version 2.3.0 installed. So after installing that on my dev server things worked beautifully again.
So what have I learned from this? Use pip freeze!

GdkPixbuf returns "gi._glib.GError: Couldn't recognize the image file format"

I have a problem loading PNG images in Gtk3.
I have broken image symbols in my ToolButtons and after some investigation it appears that it comes from GdkPixbuf not being able to read the PNG files.
I have reproduced the problem with the python console
>>> from gi.repository import GdkPixbuf
>>> print(GdkPixbuf)
<gi.module.DynamicModule 'GdkPixbuf' from '/home/user1/ctcils/dusserm/applications/gobject-introspection/1.40.0/lib/girepository-1.0/GdkPixbuf-2.0.typelib'>
>>> GdkPixbuf.Pixbuf.new_from_file("/home/user1/ctcils/dusserm/applications/pycharm-community-3.4.1/bin/pycharm.png")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
gi._glib.GError: Couldn't recognize the image file format for file '/home/user1/ctcils/dusserm/applications/pycharm-community-3.4.1/bin/pycharm.png'
Many of the problems I faced in the last days were due to the fact we had to compile Gtk3, pygobject and all their dependencies and to install them in non-standard directories.
I suspect a problem with the gobject introspection (the last from a long list).
Not a full answer but some debugging tips: Check what image loaders gdk-pixbuf is providing (see also What image formats are supported by Gdk-Pixbuf (Gtk-Image?) by Default?). A Python snippet to check this:
from gi.repository import GdkPixbuf
for fmt in GdkPixbuf.Pixbuf.get_formats():
print(fmt.get_extensions())
If nothing shows up, gdk-pixbuf is not finding any loaders which probably has something to do with the install location (--prefix and/or --libdir configure options). Verify you have loaders installed into the location gdk-pixbuf expects to find them (especially the png loader). This should be something like: <prefix>/lib[64]/gdk-pixbuf-2.0/<version>/loaders
See also: https://developer.gnome.org/gdk-pixbuf/stable/gdk-pixbuf-query-loaders.html
I got a similar error with unusual prefix, and solved it by setting XDG_DATA_DIRS, which I didn't have set.
export XDG_DATA_DIRS=.../usr/share
Source: https://bugs.gentoo.org/644136
It seems that the problem was due to the PNG library itself.
I am quite surprised considering that it comes from a regular CentOS rpm libpng-1.2.49-1.el6_2.x86_64 and that PNG is a stable standard for ages. In addition we did not have the problem with GTK2 that was using the same lib I suppose...
Anyway, I compiled a bright new libpng 1.6.2 from the sources, I re-configured and re-installed gdk-pixbuf and now it works.

Categories