我需要根据背景图片的宽度和高度调整窗口大小(tkinter
)。
我的代码
from tkinter import *
from PIL import ImageTk
import cv2
image=cv2.imread("New_refImg.png")
width_1, height_1,channels = image.shape
canvas = Canvas(width = width_1, height = height_1, bg = 'blue')
canvas.pack(expand = YES, fill = BOTH)
img = ImageTk.PhotoImage(file = "New_refImg.png")
canvas.create_image(10, 10, image = img, anchor = NW)
mainloop()
我正在使用一种简单的方法,我将相同的图像调用两次:image=cv2.imread("New_refImg.png")
和img = ImageTk.PhotoImage(file = "New_refImg.png")
但是有一种方法可以将此行img = ImageTk.PhotoImage(file = "New_refImg.png")
更改为img = ImageTk.PhotoImage(image)
之类的内容}(图片已经在代码的第3行调用)
谢谢
答案 0 :(得分:0)
我不了解PIL,但我可以告诉你如何在tkinter中做到这一点:
from tkinter import *
root = Tk() # Create Tk before you can create an image
img = PhotoImage(file='pilner.png')
w, h = img.width(), img.height()
canvas = Canvas(root, width=w, height=h, bg='blue', highlightthickness=0)
canvas.pack(expand = YES, fill = BOTH)
canvas.create_image(0, 0, image=img, anchor=NW)
root.mainloop()
highlightthickness=0
移动画布上的高亮边框。我将它定位在0,0,以便不显示bg。