Python Tkinter OpenCV PIL 图像调整大小以适合标签
Python Tkinter OpenCV PIL image resize to fit label
我正在尝试调整我的网络摄像头输出的大小以适应标签,但我无法真正让它发挥作用。如果您决定尝试测试代码,我在标签中放置了一个蓝色背景,作为您需要调整网络摄像头输出大小的指南。
密码是:
from Tkinter import *
import cv2
from PIL import Image, ImageTk
isrunning = 0
def start():
global isrunning
if isrunning == 0:
global cap
cap = cv2.VideoCapture(0)
isrunning = 1
lmain.grid(row = 1,column = 1)
def show_frame():
_, frame = cap.read()
frame = cv2.flip(frame, 1)
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
lmain.imgtk = imgtk
lmain.configure(image=imgtk)
if isrunning == 1:
lmain.after(10, show_frame)
show_frame()
def Stop():
global isrunning
isrunning = 0
lmain.grid_forget()
def main():
Stop()
Reset.invoke()
stopFunc.invoke()
root = Tk()
lmain = Label(root, width = 800, height = 600, bg = "blue")
stopFunc = Button(root, text = "stop", command = Stop)
Reset = Button(root, text = "Reset", command = start)
Main = Button(root, text = "Stop", command = main)
Start = Button(root, text = "Start", command = start)
Start.grid(row = 0, column = 0)
Main.grid(row = 0, column = 1)
root.mainloop()
我试过你的代码,我在这里修改了一些代码:
from Tkinter import *
import cv2
from PIL import Image, ImageTk
isrunning = 0
def start():
global isrunning
if isrunning == 0:
global cap
cap = cv2.VideoCapture(0)
isrunning = 1
lmain.grid(row = 1,column = 1)
def show_frame():
_, frame = cap.read()
frame = cv2.flip(frame, 1)
frame = cv2.resize(frame, (800,600))
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
lmain.imgtk = imgtk
lmain.configure(image=imgtk)
if isrunning == 1:
lmain.after(10, show_frame)
else:
cap.release()
show_frame()
def Stop():
global isrunning
isrunning = 0
lmain.grid_forget()
def main():
Stop()
#Reset.invoke()
#stopFunc.invoke()
root = Tk()
lmain = Label(root, width = 800, height = 600, bg = "blue")
stopFunc = Button(root, text = "stop", command = Stop)
Reset = Button(root, text = "Reset", command = start)
Main = Button(root, text = "Stop", command = main)
Start = Button(root, text = "Start", command = start)
Start.grid(row = 0, column = 0)
Main.grid(row = 0, column = 1)
root.mainloop()
如您所见,我添加了一行:frame = cv2.resize(frame, (800,600))
来调整图像的大小。
而且我发现在我添加这一行之后,当我按下"Stop"按钮时出现了错误。所以我尝试在每次按下 "Stop" 按钮时使用 cap.release()
释放相机。但是我现在没有网络摄像头来测试这段代码,我不能确保当你重新启动流时它会工作。希望对你有帮助。
我正在尝试调整我的网络摄像头输出的大小以适应标签,但我无法真正让它发挥作用。如果您决定尝试测试代码,我在标签中放置了一个蓝色背景,作为您需要调整网络摄像头输出大小的指南。
密码是:
from Tkinter import *
import cv2
from PIL import Image, ImageTk
isrunning = 0
def start():
global isrunning
if isrunning == 0:
global cap
cap = cv2.VideoCapture(0)
isrunning = 1
lmain.grid(row = 1,column = 1)
def show_frame():
_, frame = cap.read()
frame = cv2.flip(frame, 1)
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
lmain.imgtk = imgtk
lmain.configure(image=imgtk)
if isrunning == 1:
lmain.after(10, show_frame)
show_frame()
def Stop():
global isrunning
isrunning = 0
lmain.grid_forget()
def main():
Stop()
Reset.invoke()
stopFunc.invoke()
root = Tk()
lmain = Label(root, width = 800, height = 600, bg = "blue")
stopFunc = Button(root, text = "stop", command = Stop)
Reset = Button(root, text = "Reset", command = start)
Main = Button(root, text = "Stop", command = main)
Start = Button(root, text = "Start", command = start)
Start.grid(row = 0, column = 0)
Main.grid(row = 0, column = 1)
root.mainloop()
我试过你的代码,我在这里修改了一些代码:
from Tkinter import *
import cv2
from PIL import Image, ImageTk
isrunning = 0
def start():
global isrunning
if isrunning == 0:
global cap
cap = cv2.VideoCapture(0)
isrunning = 1
lmain.grid(row = 1,column = 1)
def show_frame():
_, frame = cap.read()
frame = cv2.flip(frame, 1)
frame = cv2.resize(frame, (800,600))
cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
img = Image.fromarray(cv2image)
imgtk = ImageTk.PhotoImage(image=img)
lmain.imgtk = imgtk
lmain.configure(image=imgtk)
if isrunning == 1:
lmain.after(10, show_frame)
else:
cap.release()
show_frame()
def Stop():
global isrunning
isrunning = 0
lmain.grid_forget()
def main():
Stop()
#Reset.invoke()
#stopFunc.invoke()
root = Tk()
lmain = Label(root, width = 800, height = 600, bg = "blue")
stopFunc = Button(root, text = "stop", command = Stop)
Reset = Button(root, text = "Reset", command = start)
Main = Button(root, text = "Stop", command = main)
Start = Button(root, text = "Start", command = start)
Start.grid(row = 0, column = 0)
Main.grid(row = 0, column = 1)
root.mainloop()
如您所见,我添加了一行:frame = cv2.resize(frame, (800,600))
来调整图像的大小。
而且我发现在我添加这一行之后,当我按下"Stop"按钮时出现了错误。所以我尝试在每次按下 "Stop" 按钮时使用 cap.release()
释放相机。但是我现在没有网络摄像头来测试这段代码,我不能确保当你重新启动流时它会工作。希望对你有帮助。