The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Tkinter- resize images
Discuss Tkinter- resize images in the Python Programming forum on Dev Shed. Tkinter- resize images Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 16th, 2013, 05:49 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 7
Time spent in forums: 2 h 35 m 19 sec
Reputation 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
|

February 16th, 2013, 10:41 AM
|
 |
Contributing User
|
|
|
|
|
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!
|

February 17th, 2013, 08:40 AM
|
 |
Contributing User
|
|
|
|
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
|

February 17th, 2013, 12:37 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 7
Time spent in forums: 2 h 35 m 19 sec
Reputation 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?
|

February 17th, 2013, 05:13 PM
|
 |
Contributing User
|
|
|
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|