[Image-SIG] create image-instances from data strings
Torsten Gallmeister
galle@rostock.zgdv.de
Wed, 18 Oct 2000 15:46:52 +0200
I need to change only data: I get the data from LDAP and I want to change the
size and palette and then create an ZOPE - image object from that data.Now,
after reading the reply from Joerg Baumann I used two StringIO objects, one for
reading LDAP data and one for saving changes.
The result is now better. But only resizing is o.k. . The palette-manipulations
I get only in a saved file with Image.save("img.jpg").
I use Python 1.5.2 and PIL 1.1 too.
my code is :
# photo are dats from LDAP
readfile=StringIO.StringIO(photo)
witefile=StringIO.StringIO()
im=Image.open(readfile)
b,h=im.size
im=im.convert("L").convert("RGB")
im=im.resize((b*height/h,height))
im=im.convert("L").convert("P")
# put a new palette area
im.putpalette(palette)
# this imagedata are only resized
im.convert("RGB").save(writefile,"JPEG")
# and here is all ok
im.convert("RGB").save("img.jpg")
dats=writefile.getvalue()
# ZOPE -> create the Zope-Image obj
ph=OFS.Image.Image('photo.jpg','Photo',dats)
self.local_images._setObject('photo.jpg',ph)
> Torsten wrote:
> > Cool, that works but only for the open method. Image.save(file) and getting
> > values with file.getvalue() gives me the initial data of der StringIO
> > object.
>
> what initial data? (a new StringIO object has no content).
>
> here's an example cut and pasted from a terminal window
> (using Python 1.5.2, PIL 1.1):
>
> >>> import Image
> >>> im = Image.open("/images/lenna.im")
> >>> import StringIO
> >>> file = StringIO.StringIO()
> >>> im.save(file, "JPEG")
> >>> data = file.getvalue()
> >>> len(data)
> 29921
> >>> repr(data[:10])
> "'\\377\\330\\377\\340\\000\\020JFIF'"
>
> </F>