Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize an image using tkinter?

Is it possible to resize an image using tkinter only? If so, how can that be done?

like image 893
drakide Avatar asked Jul 05 '10 08:07

drakide


1 Answers

You can resize a PhotoImage using the zoom and subsample methods. Both methods return a new PhotoImage object.

from tkinter import *
root = Tk()     #you must create an instance of Tk() first

image = PhotoImage(file='path/to/image.gif')
larger_image = image.zoom(2, 2)         #create a new image twice as large as the original
smaller_image = image.subsample(2, 2)   #create a new image half as large as the original

However, both of these methods can only take integer values as arguments, so the functionality is limited.

It is possible to scale by decimal values but it is slow and loses quality. The below code demonstrates scaling by 1.5x:

new_image = image.zoom(3, 3)            #this new image is 3x the original
new_image = new_image.subsample(2, 2)   #halve the size, it is now 1.5x the original
like image 148
Alex Boxall Avatar answered Oct 13 '22 02:10

Alex Boxall