0

my programs, put a img on the screen, is very simple because i'm trying to understand the behavior of the matter,i got stuck trying to find a way to make the img fit to my resolution and resize within my resolution range as any img editor does it, if anyone find a solution for that i would apreciatte it, because i've been looking on the internet for some time now ,and i'm stuck, my resolution of screen is 1366x 768 and the img is 1080x1513, is there an way to scale the img down or something like that, also if u find a way using label, isnted of canvas i would appreciate

from tkinter import*
from PIL import ImageTk, Image
root = Tk()
root.title('Calculator')
root.iconbitmap(r'C:\Users\username123\Desktop\calculator\icon.ico')
root.geometry("800x500")
myimg = Image.open(
    r"C:\Users\username123\Desktop\calculator\img/printer.jpg")
myimg.resize((450, 350), Image.ANTIALIAS)
my_img = ImageTk.PhotoImage(myimg)
canvas = Canvas(root, width=1000, height=1000)
canvas.pack()
canvas.create_image(0, 0, anchor=NW, image=my_img)


root.mainloop()
  • A label definitely can be used to create an image, see [this](https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/label.html) link to the label documentation. Simply use the keyword argument ```image``` instead of ```text```. – nikost Jan 19 '22 at 16:23
  • As for the resizing, it looks like you have resized the image. Could you be more specific in how you want to resize it. If you need to maintain aspect ratio, you can always calculate the aspect ratio of an image, then use that when resizing it. See [this](https://stackoverflow.com/questions/70725006/pillow-imagetk-displaying-only-a-part-of-an-image/70726055?noredirect=1#comment125037679_70726055) answer for maintaining aspect ratio when resizing an image. – nikost Jan 19 '22 at 16:26
  • You need to assign the result of resize to `myimg`: `myimg = myimg.resize((450, 350), Image.ANTIALIAS)`. – acw1668 Jan 20 '22 at 03:35

0 Answers0