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

北慕城南 提交于 2020-01-11 09:52:12

问题


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'

回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/3385561/python-pil-load-throwing-attributeerror-nonetype-object-has-no-attribute-rea

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!