Can't find what you need? Try here:
Loading

Tuesday, April 14, 2009

This example is an application for displaying an image with the ability to stretch the window size arbitrarily (thus enabling to zoom in on the image):


from Tkinter import *
import Image, ImageTk
import sys

#
# an image viewer

class UI(Canvas):

    def __init__(self, master, im):

      x, y, orig_w, orig_h = im.getbbox()
      Canvas.__init__(self, master,width=orig_w, height=orig_h)
      tmpim = ImageTk.PhotoImage(im)
      self.origim = im
      self.image = tmpim
      self.create_image(orig_w/2, orig_h/2, image=tmpim)

      self.bind('',self.resizeImage)

      top=self.winfo_toplevel()
      top.rowconfigure(0, weight=1)
      top.columnconfigure(0, weight=1)
      self.rowconfigure(0, weight=1)
      self.columnconfigure(0, weight=1)
      self.grid(sticky=N+S+E+W)

    def resizeImage(self,event):

      im = self.origim.resize((event.width, event.height),Image.ANTIALIAS)
      tmpim = ImageTk.PhotoImage(im)
      self.image = tmpim
      self.create_image(event.width/2, event.height/2, image=tmpim)


if not sys.argv[1:]:

    print 'need an image name!'
    exit()

else:

    filename = sys.argv[1]


root = Tk()
root.title(filename)
im = Image.open(filename)

UI(root, im)
root.mainloop()


No comments:

Post a Comment