D
D
DmitryDmitrievich2022-04-08 00:53:20
Image processing
DmitryDmitrievich, 2022-04-08 00:53:20

Image resizing in Tkinter, Python?

I'm creating a simple Tkinter game, I have an image, but I need the image to span 1 horizontally and 0.9 vertically without cropping the image. By any means, the image is only trimmed, not reduced. It is necessary that the size of the image is not set manually, but the exact amount is 0.9 from the screen vertically (it occupies the entire white background)624f5d3fa4d5d026207694.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lz961, 2022-04-08
@DmitryDmitrievich

get canvas dimensions

cnv = tk.Cnavas(...) # создали канву
.....
w = cnv['width']
h = cnv['height']

fit the image to the canvas
from PIL import Image
......
image = Image.open(....) # представили изображение в виде объекта PIL.Image
scaled_image = image.resize((w,h), Image.ANTIALIAS) # растянули/сжали

draw an image on canvas
from PIL import ImageTk
image4canvas = ImageTk.PhotoImage(scaled_image) # представили изображение в виде объекта PIL,
# с которым умеет работать канва
cnv.create_image(0, 0, anchor=tk.NW, image=image4canvas) # нарисовали изображение на канве

Tkinter is, in fact, a terrible perversion, a kind of interface to functions for building a graphical user interface for some initially stillborn tcl / tk language. The best advice on Tkinter is universal: don't use it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question