Mailing List Archive

PIL - Image.open() problem
Hi everybody.

It seems I don't know Python well enough yet. I have a problem with
Python Imaging Library which probably is simple. Only not for me!

Problem: I have a method in a classe which sometimes open a JPEG image,
and at other times do not.

When it doesn't work it is always in the same mode, which leads me to think
that I somehow don't initialize the PIL library.

The imports and method is shown below together with the stacktrace. Any
pointer as to where to look for the error is most welcome

Regards.
Per Jensen

--------------------------------------------

import os, sys, traceback
import string
from node import Node
from functions import printTraceback, trim, xmlString
from PIL import Image


def getImage(self):
"Return a PIL Image object of picture"
try:
if self.filename is None: raise NE_NoFilename("Filename is None")
if self.filename == "": raise NE_NoFilename("Filename is empty")
if self.picture is None:
print "Opening picture: \"" + self.filename + "\""
self.picture = Image.open(self.filename)
print "Opening succeeded!"
return self.picture
except:
printTraceback()
return None


Result:
---------------------------------------------
Opening picture: "1.jpg"
Traceback (most recent call last):
File "./picture.py", line 155, in getImage
self.picture = Image.open(self.filename)
File "/var/tmp/python-imaging-root//usr/lib/python2.0/site-packages/PIL/Image.py", line 871, in open
prefix = fp.read(16)
AttributeError: read
--------------------------------------------
PIL - Image.open() problem [ In reply to ]
Per Jensen wrote:

> Problem: I have a method in a classe which sometimes open a JPEG image,
> and at other times do not.

> ---------------------------------------------
> Opening picture: "1.jpg"
> Traceback (most recent call last):
> File "./picture.py", line 155, in getImage
> self.picture = Image.open(self.filename)
> File "/var/tmp/python-imaging-root//usr/lib/python2.0/site-packages/PIL/Image.py", line 871, in open
> prefix = fp.read(16)
> AttributeError: read
> --------------------------------------------

could it be that someone's passing you a unicode string?
PIL's file-or-string code doesn't deal properly with unicode
strings:

>>> import Image
>>> i = Image.open("lena.jpg")
>>> i = Image.open(u"lena.jpg")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "c:\py21\PIL\Image.py", line 936, in open
prefix = fp.read(16)
AttributeError: read
>>>

try changing your code to:

self.picture = Image.open(str(self.filename))

or, if you're on a platform that can handle unicode filenames:

self.picture = Image.open(open(self.filename, "rb"))

:::

note that 2.2 is a bit more helpful:

Python 2.2b1 (#25, Oct 19 2001, 21:26:51)
>>> import Image
>>> i = Image.open("lena.jpg")
>>> i = Image.open(u"lena.jpg")
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "c:\py22\PIL\Image.py", line 936, in open
prefix = fp.read(16)
AttributeError: 'unicode' object has no attribute 'read'

</F>
PIL - Image.open() problem [ In reply to ]
In article <fQaD7.3257$Z_1.557518@newsc.telia.net>, "Fredrik Lundh"
<fredrik@pythonware.com> wrote:

That was it !

When the routine failed, it was always when reading elements from a XML
file. Apparently it was this Unicode issue.

Thanks a lot, I have been struggling with this for a *long* time :-)

BTW: this solution wil clear the way for a new release of a little, yet
very useful, program. Check it out at http://www.net-es.dk/~pj/python

Thanx



> Per Jensen wrote:
>
>> Problem: I have a method in a classe which sometimes open a JPEG image,
>> and at other times do not.
>
>> --------------------------------------------- Opening picture: "1.jpg"
>> Traceback (most recent call last):
>> File "./picture.py", line 155, in getImage self.picture =
>> Image.open(self.filename) File
>> "/var/tmp/python-imaging-root//usr/lib/python2.0/site-packages/PIL/Image.py",
>> line 871, in open prefix = fp.read(16)
>> AttributeError: read
>> --------------------------------------------
>
> could it be that someone's passing you a unicode string? PIL's
> file-or-string code doesn't deal properly with unicode strings:
>
>>>> import Image
>>>> i = Image.open("lena.jpg")
>>>> i = Image.open(u"lena.jpg")
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "c:\py21\PIL\Image.py", line 936, in open
> prefix = fp.read(16)
> AttributeError: read
>>>>
>>>>
> try changing your code to:
>
> self.picture = Image.open(str(self.filename))
>
> or, if you're on a platform that can handle unicode filenames:
>
> self.picture = Image.open(open(self.filename, "rb"))
>
> :::
>
> note that 2.2 is a bit more helpful:
>
> Python 2.2b1 (#25, Oct 19 2001, 21:26:51)
>>>> import Image
>>>> i = Image.open("lena.jpg")
>>>> i = Image.open(u"lena.jpg")
> Traceback (most recent call last):
> File "<stdin>", line 1, in ?
> File "c:\py22\PIL\Image.py", line 936, in open
> prefix = fp.read(16)
> AttributeError: 'unicode' object has no attribute 'read'
>
> </F>