我正在从系统中获取图像,并且在单击“加载图像”按钮时需要显示一个新标签。我需要在一个定义了大小的名为“ canvas”的标签中显示完整的图像,但是我只能得到图像的一部分。
我的代码工作得很好,只显示了一部分图像。
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 images", ".png, .jpg"), ("all files", "*.*")))
if not filename:
return
image = ImageTk.PhotoImage(Image.open(filename)) #need to resize this image
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()
答案 0 :(得分:0)
唯一的解决方案是在显示图像之前使用其他一些库(例如Pillow)调整图像大小。 tkinter唯一需要调整图像大小的功能是将其缩放为2倍。