Discussion:
Help! PNG file upload error...
(too old to reply)
higer
2009-04-09 05:45:46 UTC
Permalink
When I upload a PNG file and use PIL to handle it,there will be an
error occured:
'NoneType' object is unsubscriptable

I do not know why and other formats(BMP GIF JPG JPEG) are all ok.
The following is my code in my views.py:

f = request.FILES["imagefile"]
fileExt = (f.name).split(".")[-1].lower() #get the pic's extension
parser = ImageFile.Parser()
for chunk in f.chunks():
parser.feed(chunk)
img = parser.close()
img.save("test"+"."+fileExt)

The detail of the occured error is as follows:

Environment:

Request Method: POST
Request URL: http://localhost:8000/photos/album/2/upload/
Django Version: 1.0-final-SVN-unknown
Python Version: 2.5.2
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
]
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware')


Traceback:
File "F:\Python25\Lib\site-packages\django\core\handlers\base.py" in
get_response
86. response = callback(request, *callback_args,
**callback_kwargs)
File "F:\52trips\wawafish\..\wawafish\photos\views.py" in upload_photo
175. img.save("test"+"."+fileExt)
File "F:\Python25\Lib\site-packages\PIL\Image.py" in save
1372. self.load()
File "F:\Python25\Lib\site-packages\PIL\ImageFile.py" in load
189. s = read(self.decodermaxblock)
File "F:\Python25\Lib\site-packages\PIL\PngImagePlugin.py" in
load_read
365. return self.fp.read(bytes)
File "F:\Python25\Lib\site-packages\PIL\ImageFile.py" in read
300. data = self.data[pos:pos+bytes]

Exception Type: TypeError at /photos/album/2/upload/
Exception Value: 'NoneType' object is unsubscriptable

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Kushal Das
2009-04-09 06:41:53 UTC
Permalink
Post by higer
When I upload a PNG file and use PIL to handle it,there will be an
'NoneType' object is unsubscriptable
I do not know why and other formats(BMP GIF JPG JPEG) are all ok.
f = request.FILES["imagefile"]
fileExt = (f.name).split(".")[-1].lower() #get the pic's extension
parser = ImageFile.Parser()
      parser.feed(chunk)
img = parser.close()
img.save("test"+"."+fileExt)
I guess you can save the file uploaded directly, any special reason to use PIL ?

Kushal
--
http://fedoraproject.org
http://kushaldas.in

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
higer
2009-04-10 10:37:53 UTC
Permalink
I want to get a thumbnail version of the uploaded image,so I have to
use PIL to handle it.
But I do not know why that error occured.
We can know that the chafferer is either the Django or the PIL.
Then I wrote a .py script to handle images of different format ,all
goes well.The operations are the same as views.py.
Thus,I think there may be a problem in Django.
When I upload a PNG file and usePILto handle it,there will be an
'NoneType' object is unsubscriptable
I do not know why and other formats(BMP GIF JPG JPEG) are all ok.
f = request.FILES["imagefile"]
fileExt = (f.name).split(".")[-1].lower() #get the pic's extension
parser = ImageFile.Parser()
      parser.feed(chunk)
img = parser.close()
img.save("test"+"."+fileExt)
I guess you can save the file uploaded directly, any special reason to usePIL?
Kushal
--http://fedoraproject.orghttp://kushaldas.in
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
Brian Neal
2009-04-10 13:46:29 UTC
Permalink
Post by higer
When I upload a PNG file and use PIL to handle it,there will be an
'NoneType' object is unsubscriptable
I do not know why and other formats(BMP GIF JPG JPEG) are all ok.
I don't know if this is your problem, but in order to use certain file
formats in PIL you have to have some prequisites installed. Here is
the PIL doc page for PNG support:

http://www.pythonware.com/library/pil/handbook/format-png.htm

In particular, note:

"PIL identifies, reads, and writes PNG files containing "1", "L", "P",
"RGB", or "RGBA" data. Interlaced files are currently not supported."

And also:

"Note: To enable PNG support, you need to build and install the ZLIB
compression library before building the Python Imaging Library. See
the distribution README for details."

BN
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
higer
2009-04-12 06:13:46 UTC
Permalink
Thanks for your suggestion. But Python 2.5 already has zlib moudle.
To png file,I wrote lines of code to save it first on disk and then
handled the saved copy,then that works.
I do not why there is some differencies between png file and file of
other formats.The following is my code:

parser = ImageFile.Parser()
for chunk in f.chunks():
parser.feed(chunk)
img = parser.close()

if format == 'png':
des_origin_f = open("tmp.png","ab")
for chunk in f.chunks():
des_origin_f.write(chunk)
des_origin_f.close()
img = Image.open("tmp.png")

##other operations to handle the image
img.save("test"+"."+fileExt) #save this image

I can not make sure whether the problem is caused by Django. Because
what my code tells is that PIL can handle the png file from a disk,but
can not handle a png file from a Django response(you can see my code
in the first message and compare with the code above).
Post by Brian Neal
When I upload a PNG file and usePILto handle it,there will be an
'NoneType' object is unsubscriptable
I do not know why and other formats(BMP GIF JPG JPEG) are all ok.
I don't know if this is your problem, but in order to use certain file
formats inPILyou have to have some prequisites installed. Here is
http://www.pythonware.com/library/pil/handbook/format-png.htm
"PILidentifies, reads, and writes PNG files containing "1", "L", "P",
"RGB", or "RGBA" data. Interlaced files are currently not supported."
"Note: To enable PNG support, you need to build and install the ZLIB
compression library before building the Python Imaging Library. See
the distribution README for details."
BN
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---
higer
2009-04-17 01:30:42 UTC
Permalink
Can anyone tell me why does this problem occur?
Post by higer
Thanks for your suggestion. But Python 2.5 already has zlib moudle.
To png file,I wrote lines of code to save it first on disk and then
handled the saved copy,then that works.
I do not why there is some differencies between png file and file of
                parser = ImageFile.Parser()
                    parser.feed(chunk)
                img = parser.close()
                    des_origin_f = open("tmp.png","ab")
                        des_origin_f.write(chunk)
                    des_origin_f.close()
                    img = Image.open("tmp.png")
                ##other operations to handle the image
                img.save("test"+"."+fileExt) #save this image
I can not make sure whether the problem is caused by Django. Because
what my code tells is thatPILcan handle the png file from a disk,but
can not handle a png file from a Django response(you can see my code
in the first message and compare with the code above).
Post by Brian Neal
When I upload a PNG file and usePILto handle it,there will be an
'NoneType' object is unsubscriptable
I do not know why and other formats(BMP GIF JPG JPEG) are all ok.
I don't know if this is your problem, but in order to use certain file
formats inPILyou have to have some prequisites installed. Here is
http://www.pythonware.com/library/pil/handbook/format-png.htm
"PILidentifies, reads, and writes PNG files containing "1", "L", "P",
"RGB", or "RGBA" data. Interlaced files are currently not supported."
"Note: To enable PNG support, you need to build and install the ZLIB
compression library before building the Python Imaging Library. See
the distribution README for details."
BN
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-***@googlegroups.com
To unsubscribe from this group, send email to django-users+***@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Continue reading on narkive:
Loading...