In order to facilitate the display of the complete picture and the original scale picture on the interface of tkinter, I found a lot of posts, found a useful example, and finally encapsulated it into a function (easy to use)
Code that displays a local image and resizes with the window (non-dynamic)
Code reference→https://blog.csdn.net/yangdashi888/article/details/73321919
Change the image path below to see the effect
import me from PIL import Image, ImageTk import tkinter as tk def resize(w, h, w_box, h_box, pil_image): ''' resize a pil_image object so it will fit into a box of size w_box times h_box, but retain aspect ratio Scale a pil_image object so that it fits within a rectangle and maintains proportions ''' f1 = 1.0*w_box/w # 1.0 forces float division in Python2 f2 = 1.0*h_box/h factor = min([f1, f2]) #print(f1, f2, factor) # test # use best down-sizing filter width = int(w*factor) height = int(h*factor) return pil_image.resize((width, height), Image.ANTIALIAS) root = tk.Tk() # size of image display box you want # Expect the size of the image to display w_box = 800 h_box = 800 # open as a PIL image object #Open as a PIL image object pil_image = Image.open(r'C:\Users\23216\Desktop\1.jpg') # get the size of the image #Get the original size of the image w, h = pil_image.size # resize the image so it retains its aspect ration # but fits into the specified display box #Scale the image to keep it proportioned while constrained to a rectangle pil_image_resized = resize(w, h, w_box, h_box, pil_image) # convert PIL image object to Tkinter PhotoImage object # Convert the PIL image object to a Tkinter PhotoImage object tk_image = ImageTk.PhotoImage (pil_image_resized) # put the image on a widget the size of the specified display box # Label: This widget is a display box, a small window, which displays the image size to the specified display box label = tk.Label(root, image=tk_image, width=w_box, height=h_box) #padx, pady is the distance between the image and the edge of the window label.pack(padx=5, pady=5) root.mainloop ()
encapsulated function
ps: Inside the equal sign dividing line is the usage process
import me from PIL import Image, ImageTk import tkinter as tk #Scale a pil_image object to keep it in a rectangular box and keep the scale def resize( w_box, h_box, pil_image): #The parameters are: the width and height of the window to be adapted, the image after Image.open w, h = pil_image.size #Get the original size of the image f1 = 1.0*w_box/w f2 = 1.0*h_box/h factor = min([f1, f2]) width = int(w*factor) height = int(h*factor) return pil_image.resize((width, height), Image.ANTIALIAS) root = tk.Tk() #Create a window, it must be before ImageTk.PhotoImage()! #resize function usage process: #================================================================== w_box = 700 # Expected image display size (window size) h_box = 100 pil_image = Image.open(r'C:\Users\23216\Desktop\1.jpg') #Open with a PIL image object [Adjust the image format to be transferred] pil_image_resized = resize( w_box, h_box, pil_image) #Scale the image to keep it proportional, while limiting it to a rectangular box [call the function, return the rectified image] tk_image = ImageTk.PhotoImage(pil_image_resized) # Convert the PIL image object to Tkinter's PhotoImage object [conversion format, easy to display in the window] #==================================================================== # Label: This widget is a display box, a small window, which displays the image size to the specified display box label = tk.Label(root, image=tk_image, width=w_box, height=h_box) #padx, pady is the distance between the image and the edge of the window label.pack(padx=5, pady=5) root.mainloop ()
Effect:
300*600 window
700*100 window: