Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot resize Image with tkinter

I am trying to create a tkinter project in python 2.7, where the user can resize the window, and everything inside the window will scale with it. This means the canvas, the shapes in the canvas and most importantly the PhotoImages will scale with the window. My problem, is for the life of me I cannot get my images to resize properly. I know that subsample and zoom exist for this purpose, but first of all

plantImage = PhotoImage(file="images/Arable_Cell.gif")
plantImage.subsample(2, 2)
canvas.create_image(0, 0, anchor=NW, image=plantImage)

Makes no noticeable change in a 50x50 pixel image and same for zoom(2, 2). It is important to note I know PIL exists but for the purposes of this project I cannot download any extra libraries. So what am I doing wrong?

like image 477
EasilyBaffled Avatar asked Jan 12 '23 14:01

EasilyBaffled


1 Answers

According to the docs,

subsample(self, x, y='')

Return a new PhotoImage based on the same image as this widget but use only every Xth or Yth pixel.

I.e. subsample doesn't modify the image, it creates a new one, so try this instead:

originalPlantImage = PhotoImage(file="images/Arable_Cell.gif")
displayPlantImage = originalPlantImage.subsample(2, 2)
canvas.create_image(0, 0, anchor=NW, image=displayPlantImage)
like image 165
Brionius Avatar answered Jan 16 '23 01:01

Brionius