tkinter image sizing with PIL -
from tkinter import * pil import imagetk, image import os root = tk() img = imagetk.photoimage(image.open("example_image.png")) panel = label(root, image = img) panel.pack(side = "bottom", fill = "both", expand = "yes") root.mainloop() root.minsize(width=250, height=275) root.maxsize(width=375, height=525)
i've tried multiple things work it, no matter try, image remain same size. simplicity's sake, image 400x800, , want image scaled 125x250, modify above snippet (or redo it) achieve not ambitious goal? have window has min/max size of 250x350/375x525. image remains same size cut out , whole of image cannot seen, portion of image in window size can seen. there way modify size of image without having change actual image directly in other software? in advance, , ask if confused typed.
here's code improved:
from tkinter import * pil import imagetk, image import os def change_image_size(event): # function resizes original_img every time panel size changes size. global original_img global img global first_run global wid_dif global hei_dif if first_run: # should size differences of img , panel because panel going little bigger. # resize origianl_img size of panel minus differences. wid_dif = event.width - img.width() hei_dif = event.height - img.height() # should define minsize, maxsize here if aren't # going define root.geometry , want # root fit size of panel. root.minsize(width=250, height=275) root.maxsize(width=375, height=525) first_run = false pimg = original_img.resize((event.width-wid_dif,event.height-hei_dif)) img = imagetk.photoimage(pimg) panel.configure(image=img) first_run = true # first time change_image_size runs wid_dif = 0 # width difference of panel , first unchanged img hei_dif = 0 # height difference of panel , first unchanged img root = tk() original_img = image.open("example_image.png") img = imagetk.photoimage(original_img) panel = label(root, image = img) panel.pack(side = "bottom", fill = "both", expand = "yes") # "<configure>" runs whenever panel changes size or place panel.bind("<configure>",change_image_size) root.mainloop()
Comments
Post a Comment