Help with PIL

Sean True seant at iname.com
Fri May 28 19:40:32 EDT 1999


mrfusion at bigfoot.com wrote:

>HI,
>
>	I've just installed the PIL library on my win98 machine.  I
>can open a file and see it's size, but when I try to show it
>(im.show()) I get a blank TK window and an error on the command line
>that says : Bad command or File name.
>
>Any idea what's wrong?  I can import Image and ImageTk without any
>errors.
>
>Thanks for any help.
>
>Tom

For those of us on Windows who still like to keep programming in
console mode, and who therefore avoid TK, the following example
shows one way of getting an image up on the screen. It's neither
idiomatic nor elegant, but I do use it a lot. The kludge about the
temp files keeps them from being deleted too soon, at the expense
of not deleting them at all if the program fails to exit cleanly.


# Hacked version of show in image.py from pil
# Uses irfan's view, and makes the image small enough to fit on my
# screen
    def show(self, title = None):
        "Display image (for debug purposes only)"

	try:
	    import ImageTk
	    ImageTk._show(self, title)
	    # note: caller must enter mainloop
	except:
	    #_showxv(self, title)
            showirfan(self, title)

import os,sys
tempfiles = []
def showcleanuponexit():
    raw_input("Hit enter to continue and delete image temp files")
    for file in tempfiles:
        if os.path.exists(file):
            command = "del "+file
            os.system(command)

sys.exitfunc = showcleanuponexit

def showirfan(im,label = None):
    import tempfile
    maxdim = 1024
    if label <> None:
        xsize, ysize = im.size
        print "Showing %s (%ix%i)" % (label, xsize, ysize)
    filename = tempfile.mktemp() + ".bmp"
    tempfiles.append(filename)
    width, height = im.size
    if width > height:
        if width > maxdim:
            height = int((height * maxdim) / width)
            width = maxdim
    else:
        if height > maxdim:
            width = int ((width *maxdim) / height)
            height = maxdim
    im = im.resize((width, height))
    im.load()
    if im.mode == "P":
	file = im.convert("RGB").save(filename)
    else:
	file = im.save(filename)
    command = "i_view32 "+filename
    os.system(command)


Sean True
seant at iname.com
http://striper.ne.highway1.com




More information about the Python-list mailing list