Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 16th, 2013, 05:49 AM
mememe12937 mememe12937 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 mememe12937 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old February 16th, 2013, 10:41 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,372 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 11 h 41 m 46 sec
Reputation Power: 383
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!

Reply With Quote
  #3  
Old February 17th, 2013, 08:40 AM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 483 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 22 h 51 m 26 sec
Reputation Power: 63
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

Reply With Quote
  #4  
Old February 17th, 2013, 12:37 PM
mememe12937 mememe12937 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 7 mememe12937 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #5  
Old February 17th, 2013, 05:13 PM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,372 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 11 h 41 m 46 sec
Reputation Power: 383
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Tkinter- resize images

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap