[Python-au] PIL Image drawing problem

Duane Hennessy duaneh at connexus.net.au
Sat Feb 28 22:51:35 CET 2004

I'm having problems with the ImageDraw moduel in the PIL. I'm building a 
graphics application to do my artwork with custom filters etc etc... To 
track sessions of work I keep a dictionary of images that are currently 
open and load them when they are chosen into a Tk Canvas object. Works 
fine. I can also invert, flip and add other filters fine, no problem. 
When I come to draw into the image from the canvas it then fails to show 
the modified image in the canvas and instead I am presented with a blank 
canvas. If I apply a filter or whatever afterwards the image appears 
again no problem. What is going on with the draw method of ImageDraw? I 
am basically trying to draw a selection rectangle on the user's mouse 
movements and have followed the example to the T. Relevant bits of code 
I am using below.

---- Code Start ----
    import tkMessageBox as msgbox
    import sys
    import types
    import ImageTk
    from  ImageDraw import *
    import PSDraw
    from string import *
    import pywin.debugger as debug
except ImportError, err:
    msgbox.showwarning("Module Import",err)
    errType,value,traceback = sys.exc_info()
    msgbox.showwarning("Module Import","%s\n%s" % (errType,value))

# rather than make a convoluted string to access the dictionary in a 
class I'll make the dictionary
# global for easy access from outside once this module is imported. I 
might aswell make all the functions
# that work with the dictionary global aswell instead of putting them 
into classes.

# dictionary that holds main 'real' image.
SessionImages = {}

CurrentImage = None # Image representation
CurrentName = None # Image name (SessionImages dictionary key) to the 
current image having the focus.
X = 0 # X Coordinates of image on Canvas
Y = 0 # YCoordinates of image on Canvas

def loadImage(filename):
    '''Load a file representation so that it can be picked up and put onto
    the canvas. This incorporates layers etc'''
    global CurrentImage
    global CurrentName
    global X
    global Y
        imageToLoad = SessionImages[filename]["1"]
##        debug.brk()

        if imageToLoad.has_key("selection"):
            # grabs the main image here
            im = imageToLoad["image"]
            Start,Stop = imageToLoad["selection"] #Start and Stop are 
both tuples of X and Y coords. No issues here.

             # this is where the problem is!!!
            draw = Draw(im)
            box = [Start,Stop]
            draw.rectangle(box,outline = 128)
            del draw
            im = imageToLoad["image"]
        im = im.resize((imageToLoad["W"],imageToLoad["H"]))
        CurrentImage =  ImageTk.PhotoImage(im)
        CurrentName = filename
        X = imageToLoad["X"]
        Y = imageToLoad["Y"]
                errType,value,traceback = sys.exc_info()
                msgbox.showwarning("loadImage","%s\n%s" % (errType,value))

---- Code End ----

Hope this makes sense, I can clarify if it doesn't.



-------------- next part --------------
A non-text attachment was scrubbed...
Name: duaneh.vcf
Type: text/x-vcard
Size: 220 bytes
Desc: not available
Url : http://starship.python.net/pipermail/python-au/attachments/20040229/9bf8dbbf/duaneh.vcf

More information about the python-au mailing list