How can I get Tkinter To display a very large image without having scrollbars? How can I get Tkinter To display a very large image without having scrollbars? tkinter tkinter

How can I get Tkinter To display a very large image without having scrollbars?


Use size attribute and resize(w, h) method to do what you want. Here is an example, which halves the image size:

from tkinter import *import ioimport urllib.requestfrom PIL import ImageTk, Imagecanvas = Tk()page = urllib.request.urlopen("https://web2.hirez.com/smite-media//wp-content/uploads/2017/07/Fafnir.jpg")background = Image.open(io.BytesIO(page.read()))width, height = background.sizebackground = background.resize((width // 2, height // 2))tkimage = ImageTk.PhotoImage(background)panel1 = Label(canvas, image=tkimage)panel1.grid(ipadx=0, ipady=0, sticky=E)mainloop()

Note: don't use width / 2, use width // 2 instead - otherwise it will cause TypeError if you get a float as a division result.

Note 1: if the quality of the output image is more important for you, than the speed, use background.resize((width // 2, height // 2), Image.ANTIALIAS).


background = background.resize((w, h), Image.ANTIALIAS)