Create Image w PIL and save to OFS.Image.Image subclass instance
want to create a grafik w PIL and store into my instance which is subclassed from OFS.Image.Image. Excerpt: def manage_draw_PraxisIndex ... sql_graph = [ (1, 8), (16, 8), (1, 9), (16, 9) ] import PIL.Image # name clash PIL.Image % OFS.Image import PIL.ImageDraw # mode, size thePic = PIL.Image.new( "RGB", (17, 17) ) draw = PIL.ImageDraw.Draw(thePic) draw.line( sql_graph) from cStringIO import StringIO pic_File = StringIO('') 201>> thePic.save(pic_File) Error Type: KeyError Error Value: Traceback (innermost last): File C:\PROGRA~1\Zope232\lib\python\ZPublisher\Publish.py, line 223, in publish_module File C:\PROGRA~1\Zope232\lib\python\ZPublisher\Publish.py, line 187, in publish File C:\PROGRA~1\Zope232\lib\python\Zope\__init__.py, line 221, in zpublisher_exception_hook (Object: rki) File C:\PROGRA~1\Zope232\lib\python\ZPublisher\Publish.py, line 171, in publish File C:\PROGRA~1\Zope232\lib\python\ZPublisher\mapply.py, line 160, in mapply (Object: manage_draw_PraxisIndex) File C:\PROGRA~1\Zope232\lib\python\ZPublisher\Publish.py, line 112, in call_object (Object: manage_draw_PraxisIndex) File C:\Programme\Zope232\lib\python\Products\PraxInd\RKI.py, line 201, in manage_draw_PraxisIndex (Object: rki) File C:\Programme\Zope232\bin\PIL\Image.py, line 681, in save KeyError: (see above) what did i miss? Also: How does an image get displayed, when <img src="myImageURL">, what is the browser request and to which call (of Image) will it get mapped? thanks a lot, cheers hans -- ------------------------------------------------------------- Who's got only a hammer sees the world as a nail hans augustin (software developer) hans@beehive.de beehive elektronische medien GmbH http://www.beehive.de phone: +49 30 847-82 0 fax: +49 30 847-82 299
Hans wrote:
want to create a grafik w PIL and store into my instance which is subclassed from OFS.Image.Image.
Here is an example that works. The only caveat I found was that you must use the same id for the images id in the objectmanager and as the id in the image. Well this is perhaps logical, but it bit me once. so that is like: imageID = 'myImage' self._setObject(imageID, OFS.Image.Image(imageID,'Laerings graf','')) Then it can be called like: <dtml-var myImage> regards Max M def _drawTriangle(self, a, b, c): im = PIL.Image.open("C:/root/pythonScripts/PILTest/laeringstilGraf_2.gif") """ Coordinate system: d | a -------- c | b """ # a is a sequence of 4 values. maxValue = max(a) # what value is it biggestIndex = a.index(maxValue) # Where is it? if biggestIndex == 0: # Then draw in a dirX, dirY = 1,1 bX, bY = 200, 300 cX, cY = 300, 200 elif biggestIndex == 1: # Then draw in b dirX, dirY = 1,0 bX, bY = 200, 100 cX, cY = 300, 200 elif biggestIndex == 2: # Then draw in c dirX, dirY = 0,0 bX, bY = 200, 100 cX, cY = 100, 200 elif biggestIndex == 3: # then draw in d dirX, dirY = 0,1 bX, bY = 200, 300 cX, cY = 100, 200 tr1 = transformer(200, 200, 4, dirX, dirY) sideLength = sqrt(pow(maxValue,2)/2.0) # Pythagoras cord1 = tr1.transform(sideLength, sideLength) tr2 = transformer(bX, bY, 6, dirX, dirY) coord2 = tr2.transform(0,b) tr3 = transformer(cX, cY, 1, dirX, dirY) coord3 = tr3.transform(c,0) draw = PIL.ImageDraw.Draw(im) darkBlue = 2 draw.polygon([cord1, coord2, coord3], outline=darkBlue, fill=darkBlue) del draw outf = StringIO() im.save(outf, 'GIF') # Save it to a temp file del im data = outf.getvalue() outf.close() return data def updateGraph(self): "Updates the graph if it is changed since last view" #self.imageLastChanged = 0 imageID = 'laeringsGraf' if self.imageLastChanged == self.lastChanged: # Image is in sync pass else: # doesn't exist, so render it # all stuff happens in the next line #data = self._drawTriangle([60, 0, 0, 0], 11, 60) data = self._drawTriangle(self._getQ1Result(), self._getQ2Result(), self._getQ3Result()) if not hasattr(self, imageID): # add to objectmanager self._setObject(imageID, OFS.Image.Image(imageID,'Laerings graf','')) self._getOb(imageID).update_data(data) self.imageLastChanged = self.lastChanged
From: "hans" <hans@beehive.de>
want to create a grafik w PIL and store into my instance which is subclassed from OFS.Image.Image.
You can look at how it's done with the EasyImage product. It uses PIL for making convertions, resizing and previews. http://www.zope.org/Members/regebro/
participants (3)
-
hans -
Lennart Regebro -
Max M