[Zope] Create Image w PIL and save to OFS.Image.Image subclass
instance
Max M
maxm@mxm.dk
Mon, 18 Mar 2002 21:23:35 +0000
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