Create image with PIL in Zope
Hi, I have a small script that generate a image with some text, in python with Python Imagin Library (PIL). The problem is, i want a security image in an html page in zope, so i started to put the code in a External module, and now i have all kind of problems with that script. When i use the "ImageFont.load('pathToFont.pil')", that causes an Error Type: IOError Error Value: cannot find glyph data file i don' understand, because i have the ".pbm", and it works in command line. So, then i try to do "ImageFont.load_path('font.pil')", and that causes a Error Type: IOError Error Value: cannot find font file once again i couldn't understand because it works on command line, because the font file is in "/usr/lib/python2.3/site-packages/pilfonts/" At this time, i start to dig in the sourcecode of ImageFont.py, and i found that when it raises the "cannot find glyph data file", is when it try's to open the ".pbm" file with Image.open(), i tried to do this to get more information, and it gives a Error Type: AttributeError Error Value: 'file' object has no attribute 'startswith' I don't know if this makes any sense to you, .... but are there someone to help me in this? Do i have other alternatives to generate images with text on the fly in zope? thanks in advance, --Bruno Silva
Bruno Miguel Silva wrote:
When i use the "ImageFont.load('pathToFont.pil')", that causes an Error Type: IOError Error Value: cannot find glyph data file i don' understand, because i have the ".pbm", and it works in command line.
I have problems with using PIL under Zope as Zope has a module called Image, and PIL has a module called Image, and the Zope one is the one that gets imported instead of the PIL one when running under Zope (but not when running on the command line). I end up munging my PIL installation, so that modules that have: import Image, ImageFile get changed to read instead: from PIL import Image, ImageFile This usually works for me. I usually only have to munge the few "plugin" modules for the types of images I am creating, like "JpegImagPlugin.py" and "PngImagePlugin.py". You may have to munge other modules. HTH, John Z
Bruno Miguel Silva wrote:
Hi, I have a small script that generate a image with some text, in python with Python Imagin Library (PIL). The problem is, i want a security image in an html page in zope, so i started to put the code in a External module, and now i have all kind of problems with that script.
for opening an image with PIL in an ExternalMethod (assuming that the image to load is in the ZODB), i use: from PIL.Image import * from cStringIO import StringIO def processImage(): img_to_open = ImageFolder.restrictedTraverse(ImageID) img = open(StringIO(str(img_to_open._data))) [... img processing stuff come here ...] final_img = StringIO() img.save(final_img, final_img.format) final_img.seek(0) return finalmap.getvalue() HTH
When i use the "ImageFont.load('pathToFont.pil')", that causes an Error Type: IOError Error Value: cannot find glyph data file i don' understand, because i have the ".pbm", and it works in command line.
is it '.pbm' ? I oftenly use '.bmp'
So, then i try to do "ImageFont.load_path('font.pil')", and that causes a Error Type: IOError Error Value: cannot find font file once again i couldn't understand because it works on command line, because the font file is in "/usr/lib/python2.3/site-packages/pilfonts/"
Sorry, I never worked with fonts
At this time, i start to dig in the sourcecode of ImageFont.py, and i found that when it raises the "cannot find glyph data file", is when it try's to open the ".pbm" file with Image.open(), i tried to do this to get more information, and it gives a Error Type: AttributeError Error Value: 'file' object has no attribute 'startswith'
I don't know if this makes any sense to you, .... but are there someone to help me in this? Do i have other alternatives to generate images with text on the fly in zope?
thanks in advance, --Bruno Silva _______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
.
-- David Convent
From: "Bruno Miguel Silva" <bruno@techwave.org>
I have a small script that generate a image with some text, in python
with
Python Imagin Library (PIL). The problem is, i want a security image in an html page in zope, so i started to put the code in a External module, and now i have all kind of problems with that script. When i use the "ImageFont.load('pathToFont.pil')", that causes an Error Type: IOError Error Value: cannot find glyph data file i don' understand, because i have the ".pbm", and it works in command line.
So, then i try to do "ImageFont.load_path('font.pil')", and that causes a Error Type: IOError Error Value: cannot find font file once again i couldn't understand because it works on command line, because the font file is in "/usr/lib/python2.3/site-packages/pilfonts/"
At this time, i start to dig in the sourcecode of ImageFont.py, and i found that when it raises the "cannot find glyph data file", is when it try's to open the ".pbm" file with Image.open(), i tried to do this to get more information, and it gives a Error Type: AttributeError Error Value: 'file' object has no attribute 'startswith'
I don't know if this makes any sense to you, .... but are there someone to help me in this? Do i have other alternatives to generate images with text on the fly in zope?
I don't know what the cause of your PIL problem is, but here are some code chunks from routines that we use to generate images on the fly that may give you some clues: 1) Code from dtml method that gathers info from a form and db then calls an external method to generate the images, then displays the images: <dtml-comment> build graph for daily qty & percentage chart </dtml-comment> <dtml-call "REQUEST.set('ts', ZopeTime())"> <dtml-with TmpImages> <dtml-call "REQUEST.set('graphtype', 'day_qty_percent')"> <dtml-let tt="graphics(REQUEST)"> <dtml-call "REQUEST.set('day_qty_percent_id',_.str(_.int(ts)))"> <dtml-call "this().manage_addImage(day_qty_percent_id, tt)"> </dtml-let> </dtml-with> <dtml-comment> display graph (previously created/store in TmpImages folder) for daily qty & percentages </dtml-omment> <center> <br> <img src="TmpImages/&dtml-day_qty_percent_id;"> </center> 2) Code from the external method which generates the images from piddlePIL import * class RamFile: def __init__(self): self.contents = [] def write(self,s): self.contents.append(s) def read(self): return string.join(self.contents,'') class Colour: dark = HexColor("D0DDAD") light = HexColor("EBFFD9") def graphics(REQUEST): datalist = REQUEST.get('datalist') graphtype = REQUEST.get('graphtype') if graphtype == 'day_qty_percent': return day_qty_percent(datalist) def day_qty_percent(datalist): import fpformat canvas = PILCanvas() canvas.drawRect( 0,0,300,300, fillColor=Colour.light ) canvas.drawRect( 2,2,296,297, edgeColor=Colour.dark, edgeWidth=5 ) totalcount = 0 biggestday = 0 for today in datalist: totalcount = totalcount + today if today > biggestday: biggestday = today scale = 150.0/((float(biggestday)/float(totalcount))*100.0) i = 0 for x in datalist: xpercent = (float(x)/float(totalcount)) * 100.0 x1 = int(xpercent * scale) if x > 0: canvas.drawRect(23, 40+(i*35), x1+23, 60+(i*35),fillColor=Colour.dark) xstr = str(int(x)) canvas.drawString(xstr, x1+28, 53+(i*35), Font(face="times",size=14,bold=0), color=black, angle=0) canvas.drawString("("+str(fpformat.fix(xpercent,1))+"%)", x1+38+(len(xstr)*6), 53+(i*35) ,Font(face="times",size=12,bold=0), $ i = i + 1 canvas.drawString("Parking Events (Total "+str(int(totalcount))+")", 50, 19, Font(face="times",size=12,bold=1), color=black, angle=0) canvas.drawLine(22, 25, 22, 293, color=slategray) canvas.drawString("M", 7,53, Font(face="times",size=14,bold=1), color=black, angle=0) canvas.drawString("T", 7,88, Font(face="times",size=14,bold=1), color=black, angle=0) canvas.drawString("W", 7,123, Font(face="times",size=14,bold=1), color=black, angle=0) canvas.drawString("T", 7,158, Font(face="times",size=14,bold=1), color=black, angle=0) canvas.drawString("F", 7,193, Font(face="times",size=14,bold=1), color=black, angle=0) canvas.drawString("S", 7,228, Font(face="times",size=14,bold=1), color=black, angle=0) canvas.drawString("S", 7,263, Font(face="times",size=14,bold=1), color=black, angle=0) canvas.flush() rfile = RamFile() canvas.save(file=rfile,format="jpeg") return rfile.read() HTH Jonathan
participants (4)
-
Bruno Miguel Silva -
David Convent -
John Ziniti -
Jonathan Hobbs