You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Tkinter固定尺寸Label中完整显示加载的本地图片?

Fix: Image Not Showing Entirely in Fixed-Size Canvas

Hey there! The issue you're facing is super common—your original image is bigger than the 150x150 canvas label, so Tkinter just displays the top-left chunk by default. To fix this, we need to resize the image while preserving its aspect ratio so it fits perfectly inside your canvas without getting cut off.

Here's the modified code with the fix:

from tkinter import *
from tkinter import filedialog
from PIL import ImageTk, Image
import os

root = Tk()
root.title("Images")

def Clicked():
    filename = filedialog.askopenfilename(
        initialdir=os.getcwd(), 
        title="Select file", 
        filetypes=(("png/jpg images", ".png .jpg"), ("all files", "*.*"))
    )
    if not filename:
        return
    
    # --- Modified section starts here ---
    # Open the original image
    original_img = Image.open(filename)
    # Define your canvas dimensions
    canvas_width, canvas_height = 150, 150
    # Calculate the scaling ratio to fit the image inside the canvas
    original_width, original_height = original_img.size
    scale_ratio = min(canvas_width / original_width, canvas_height / original_height)
    
    # Compute new dimensions while maintaining aspect ratio
    new_width = int(original_width * scale_ratio)
    new_height = int(original_height * scale_ratio)
    
    # Resize the image using high-quality interpolation
    resized_img = original_img.resize((new_width, new_height), Image.Resampling.LANCZOS)
    image = ImageTk.PhotoImage(resized_img)
    # --- Modified section ends here ---
    
    loaded = Label(frame, text="\n\n\nLoaded Image\n", font=('Arial',10,'bold')).grid(row=5)
    canvas = Label(frame, width=150, height=150, relief="groove", borderwidth=2, image=image)
    canvas.image = image
    canvas.grid(row=6)

frame = Frame(root, relief="solid", borderwidth=1)
frame.pack()

label = Label(frame, text="Content Based Image Retrieval\n", font=('Arial', 15), padx=10, pady=10).grid(row=0)
content = Label(frame, text="Contents\n\n", font=('Arial',10), padx=20).grid(row=1, column=0, sticky=W)
loadImg = Button(frame, text="Load Image", relief="groove", padx=10, pady=5, command=Clicked).grid(row=2)
blank = Label(frame, text="").grid(row=3)
searchImg = Button(frame, text="Search Image", relief="groove", padx=6, pady=5).grid(row=4)
blank = Label(frame, text="").grid(row=7)

root.mainloop()

Key changes explained:

  • We first calculate the scaling ratio by comparing how much we need to shrink the image's width and height to fit the canvas. Using the smaller ratio ensures the entire image fits without overflowing.
  • We resize the image with Image.Resampling.LANCZOS (a high-quality downsampling method) to keep the image sharp instead of pixelated.
  • The resized image now fits perfectly within your 150x150 canvas, showing the entire image instead of just a portion.

Give this a try—your loaded images should now display fully inside the canvas label!

内容的提问来源于stack exchange,提问作者Garvit Maurya

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:16:17