I want to create a jpg image containg some graphics which I draw using the Tkinter Canvas widget. This draws the image to screen and then I use the PIL library ImageGrab() function to grab it off the screen into a file. It would be much neater to by-pass drawing it on screen by drawing it in memory and then capturing this in the desired format in a file - is this possible ?

Here is one way to do it ...

# with the Tkinter canvas, drawings can only be saved as .ps files 
# use PIL to draw simultaneuosly to memory and then save to file
# PIL allows .png .jpg .gif or .bmp file formats

import Tkinter as tk
from PIL import Image, ImageDraw

root = tk.Tk()
root.title("drawing lines")

# some color constants for PIL
white = (255, 255, 255)
black = (0, 0, 0)
blue = (0, 0, 255)
red = (255, 0, 0)
green = (0,128,0)

width = 450
height = 450
# create the drawing canvas
cv = tk.Canvas(root, width=width, height=height, bg='white')
cv.pack()

# create empty PIL image and draw objects to draw on
# PIL draws in memory only, but the image can be saved
image = Image.new("RGB", (width, height), white)
draw = ImageDraw.Draw(image)

# draw horizontal lines
x1 = 0
x2 = 450
for k in range(0, 500, 50):
    y1 = k
    y2 = k
    # Tkinter (visible)
    cv.create_line(x1, y1, x2, y2)
    # PIL (to memory for saving to file)
    draw.line((x1, y1, x2, y2), black)    

# draw vertical lines
y1 = 0
y2 = 450
for k in range(0, 500, 50):
    x1 = k
    x2 = k
    # Tkinter
    cv.create_line(x1, y1, x2, y2)
    # PIL
    draw.line((x1, y1, x2, y2), black)

# Tkinter canvas object can only be saved as a postscipt file
# which is actually a postscript printer language text file
cv.postscript(file="mylines.ps", colormode='color')

# PIL image can be saved as .png .jpg .gif or .bmp file
filename = "mylines.jpg"
image.save(filename)

root.mainloop()

Very neat - thank you so much - it is just what I was looking for (I have tested it here and it works!).
I am new to Python (ex-Visual Basic) and am excited by the power and elegance of the language. Also I value the simple and transparent build process. In this case I need to understand the 2 lines

image = Image.new("RGB", (width, height), white)
draw = ImageDraw.Draw(image)[icode]which are the key that I was missing.  

which are the key. I am working on it ...!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.