I have written a pretty convoluted script for creating graphics – via the Tkinter module.
It works as I expect, and saves the canvas as a PostScript file.
Only, I can’t render the PS file in anything. At all.
I was pretty confident when I decided to use Tkinter that there would be something I could do with the PS files to get them into some more standard format.
I have seen some tutorials that suggest porting the drawings to PIL, which might work, but will be quite a task to port all the dynamic objects from tk to PIL.
I wondered if anyone had a quicker/dirtier way of getting the pixels off the widget window into an image file.
Or any windows methods of viewing/rasterising the PS file? I can place an example PS file somewhere if there is interest? (the python code is quite involved, and requires 3 MySQL tables to pull the data together)
I am trying to use the screengrab method from here: http://mail.python.org/pipermail/image-sig/2003-May/002292.html
and and struggle to get things in the right order.
tkinter code:
def drawCircles(MasterList,buildlist):
master = Tk()
w = Canvas(master, width=1000, height=1000)
w.config(bg='white')
coordsMain = MasterList[6:]
textMain = MasterList[0:2]
w.pack()
w.create_oval(*coordsMain, width=3, fill = "ivory3")
masterLabel = "Source PUID\n" + str(MasterList[3]) + "\nFiles = " + str(MasterList[4])
w.create_text(*textMain, text=masterLabel, justify = "center", font=("Helvetica", 16))
for i in buildlist:
coordsSet = i[6:10]
w.create_oval(*coordsSet, width=3, fill = i[5])
set_label = i[3] + "\n" + str(i[4]) + "%"
l=w.create_text(4,4, text=set_label, justify = "center", fill="white", font=("Helvetica", 16))
a,b,c,d= (w.bbox(l))
bboxArea =(c-a)*(d-b)
a,b,c,d = i[6:10]
circleArea = (c-a)*(d-b)
if bboxArea>circleArea:
textSet = i[10:]
j=w.create_text(*textSet, text=set_label, justify = "center", font=("Helvetica", 16))
r=w.create_rectangle(w.bbox(j),fill="white", width=0)
else:
textSet = i[:2]
j=w.create_text(*textSet, text=set_label, justify = "center", font=("Helvetica", 16))
r=w.create_rectangle(w.bbox(j),fill=i[5], width=0)
w.tag_lower(r,j)
PUID = str(MasterList[3])
PUID = PUID.replace('/', '-')
filename = "\images\\" + PUID + ".jpg"
mainloop()
Screen grab code:
x0 =w.winfo_rootx()
y0 =w.winfo_rooty()
x1 =x0 + w.winfo_width()
y1 =y0 + w.winfo_height()
im =ImageGrab.grab((x0, y0, x1, y1))
im.save(filename)
I can make a jpg this way, but can’t seem to get the contents of the widget in the the jgp (the file that gets created declares as a jpg, but has no image payload)
If I place the screen grab code after the mainloop, it has says its destroyed the object, before mainloop, its not built the object yet….
It turns out the update() function is required prior to postscript call, as specified by tk canvas manpage.
pathName postscript ?option value option value …?
Note: by default Postscript is only generated for information that appears in the canvas’s window on the screen. If the canvas is freshly created it may still have its initial size of 1×1 pixel so nothing will appear in the Postscript. To get around this problem either invoke the “update” command to wait for the canvas window to reach its final size, or else use the -width and -height options to specify the area of the canvas to print.
I have marked this as the fix because the PS file is the preferred save format, and is of much higher quality than the screengrab method.
The working code now becomes: