Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python PIL load throwing AttributeError: 'NoneType' object has no attribute 'read'

I've been struggling with this error for several days with little headway. Basically, I'm trying to read in an image file and then use PIL to preform a specific operation on it. (my end goal is to preform a PIL paste operation).

However, whenever I load my image in, and then invoke the load() method on it (operations like show(), paste(), resize(), etc. all invoke the load() method), I get a weird NoneType has no attribute read error.

I'm using PIL 1.1.7 and have reproduced this error on both OSX 10.6 and Ubuntu 10.04. Below is the most basic ipython code that I can enter to produce the error.

Has anyone see this type of situation before?

Any help is much appreciated.

In [1]: import os
In [2]: try:
   ...:     from PIL import Image
   ...: except ImportError:
   ...:     import Image
   ...: 
In [3]: from django.conf import settings
In [4]: bgImageFileHash = "d41d8cd98f00b204e9800998ecf8427e"
In [5]: bgImageFilePath = os.path.join(settings.MEDIA_ROOT,'uploads',"%s.jpg" % (bgImageFileHash)) 
In [6]: print bgImageFilePath 
------> print(bgImageFilePath )
/Users/test/Sites/env/mysite/proj/mysite/../mysite/media/uploads/d41d8cd98f00b204e9800998ecf8427e.jpg
In [7]: bgImageImage=Image.open(bgImageFilePath)
In [8]: bgImageImage.verify()
In [9]: bgImageImage.load()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/Users/test/Sites/env/mysite/proj/mysite/<ipython console> in <module>()
/Users/test/Sites/env/mysite/lib/python2.6/site-packages/PIL/ImageFile.pyc in load(self)
    168             read = self.load_read
    169         except AttributeError:
--> 170             read = self.fp.read
    171 
    172         try:
AttributeError: 'NoneType' object has no attribute 'read'
like image 895
Joe J Avatar asked Oct 15 '25 13:10

Joe J


2 Answers

Maybe remove the call to verify(), or put a second call to open() between verify() and load()?

The documention on verify() here says:

...if you need to load the image after using this method, you must reopen the image file.

like image 101
Saxon Druce Avatar answered Oct 17 '25 03:10

Saxon Druce


This is a silly thing, but I ran into this same error and spent at least an hour trying to find a solution. However the problem was actually mine, the filename I was passing was blank and caused it to throw this error........ Ugh.. Would have been nice if the library said "oh, you are missing the filename" instead of 'NoneType' object has no attribute 'read'. This was on a raspberry pi and python 2.7 just FYI.

like image 20
Brandon Bates Avatar answered Oct 17 '25 02:10

Brandon Bates