#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Tkinter- resize images


    Hi, I'm looking for a way to change images size when the window size is changed (like sticky).
    Do you know a code or a way to do that? maybe in Python Imaging Library (I've never used it)?
    If you don't know, I'll try to write one myself. Could you tell me which PIL functions or methods I should use in the code? (I'm guessing I should use PIL) because there are a lot of functions that change image size in PIL, and I don't understand the difference.
    TNX
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,901
    Rep Power
    481
    http://www.pythonware.com/library/pil/handbook/image.htm

    presents two methods for resizing, resize and transform. Transform gives more control.

    PhotoImage has subsample() and zoom() methods. If you wanted to zoom by a factor of 1.5 I'm quite sure you'd need a two step procedure, zoom by factor of 3 then resample with spacing 2. You shouldn't give too many rational number choices because you don't want to zoom an already large image by a large number.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    611
    Rep Power
    65
    Here is an example of PIL and Tkinter together ...
    Code:
    ''' tk_PIL_resize1.py
    load and display an image with Tkinter
    
    Tkinter only reads gif and ppm images
    Use the Python Image Library (PIL) for other image formats
    and for manipulating the image
    Give Tkinter a namespace to avoid conflicts with PIL 
    (they both have class Image) 
    PIL is free from:
    http://www.pythonware.com/products/pil/index.htm
    I used Windows installer
    PIL-1.1.7.win32-py2.7.exe
    from:
    http://effbot.org/downloads/#pil
    '''
    
    from PIL import Image, ImageTk
    try:
        # Python2
        import Tkinter as tk
    except ImportError:
        # Python3
        import tkinter as tk
    
    root = tk.Tk()
    w = 640
    h = 500
    cv = tk.Canvas(root, width=w, height=h)
    cv.pack(fill='both', expand='yes')
    
    # pick an image file you have .bmp  .jpg  .gif.  .png
    # if not in the working directory, give full path
    img_file = "flowers.jpg"
    # open as a PIL image object
    pil_image = Image.open(img_file)
    
    # get the size of the original image
    width_org, height_org = pil_image.size
    
    # set the resizing factor so the aspect ratio is retained
    # factor > 1.0 increases size
    # factor < 1.0 decreases size
    factor = 1.45
    width = int(width_org * factor)
    height = int(height_org * factor)
    # use one of these filter options to resize the image:
    # Image.NEAREST     use nearest neighbour
    # Image.BILINEAR    linear interpolation in a 2x2 environment
    # Image.BICUBIC     cubic spline interpolation in a 4x4 environment
    # Image.ANTIALIAS   best down-sizing filter
    pil_image2 = pil_image.resize((width, height), Image.ANTIALIAS)   
    
    # optional title ...
    sf = "{} ({}x{})".format(img_file, width, height)
    root.title(sf)
    
    # convert PIL image object to Tkinter PhotoImage object
    tk_image = ImageTk.PhotoImage(pil_image2)
    
    # 'nw' uses upper left corner of the image as anchor
    # image corner positions at canvas coordinates x=30, y=20
    #cv.create_image(30, 20, image=tk_image, anchor='nw')
    
    # 'center' is default --> uses center of picture
    # at canvas coordinates x=250, y=250 (center of canvas)
    cv.create_image(w//2, h//2, image=tk_image, anchor='center')
    
    root.mainloop()
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    7
    Rep Power
    0

    Tnx


    Thanks for the help.
    I've written a code, using the event <Configure> to adjust the image size when the window size changes.
    It works fine, but I read that on some platforms <Configure> is triggered when the widget changed location.
    does that mean the code won't work on some platforms?
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,901
    Rep Power
    481
    There are a whole bunch of events you can respond to.
    http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm
    The particular event object containes relevant information. On configure notify you could verify that the size changed before deciding to rescale your image.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo