英文:
how to display multiple image copies and source image in one tkinter frame
问题
我能在一个帧中显示保存的mp4文件,并想尝试复制源文件并在一个帧中显示源mp4,如下图所示:
from tkinter import *
from PIL import Image, ImageTk
import cv2
root = Tk()
root.title("Video!")
# vid = cv2.VideoCapture("vid1.mp4")
vid = cv2.VideoCapture(0)
width = vid.get(cv2.CAP_PROP_FRAME_WIDTH)
height = vid.get(cv2.CAP_PROP_FRAME_HEIGHT)
def get_frame():
ret, frame = vid.read()
if ret:
return (ret, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
else:
return (ret, None)
def update():
ret, frame = get_frame()
if ret:
img = Image.fromarray(frame)
photo = ImageTk.PhotoImage(image=img)
canvas.create_image(0, 0, image=photo, anchor=NW)
canvas.image = photo
root.after(delay, update)
canvas = Canvas(root, width=width, height=height)
canvas.pack()
delay = 15
update()
root.mainloop()
vid.release()
thank you
希望这对你有帮助。
英文:
i am able to display saved mp4 file in 1 frame and want to try copy the source and display it in one frame with the source mp4.
as following image
from tkinter import *
from PIL import Image, ImageTk
import cv2
root = Tk()
root.title("Video!")
# vid = cv2.VideoCapture("vid1.mp4")
vid = cv2.VideoCapture(0)
width = vid.get(cv2.CAP_PROP_FRAME_WIDTH)
height = vid.get(cv2.CAP_PROP_FRAME_HEIGHT)
def get_frame():
ret,frame = vid.read()
if ret :
return(ret,cv2.cvtColor(frame,cv2.COLOR_BGR2RGB))
else :
return(ret,None)
def update():
ret,frame = get_frame()
if ret :
img = Image.fromarray(frame)
photo = ImageTk.PhotoImage(image=img)
canvas.create_image(0, 0, image = photo, anchor = NW)
canvas.image=photo
root.after(delay,update)
canvas = Canvas(root, width = width, height = height)
canvas.pack()
delay = 15
update()
root.mainloop()
vid.release()
thank you
答案1
得分: 1
您可以使用cv2.vconcat
和cv2.hconcat
来合并您的图像并一次显示它们。
例如,如果您修改您的更新函数如下:
def update():
ret, frame = get_frame()
im1 = frame #您的第一张期望的图像
im2 = frame.copy() #第二张图像
im3 = frame.copy() #第三张图像
im4 = frame.copy() #第四张图像
im5 = frame.copy() #第五张图像
im6 = frame.copy() #最后一张图像
upper_part = cv2.hconcat([im1, im2, im3])
lower_part = cv2.hconcat([im4, im5, im6])
final_image = cv2.vconcat([upper_part, lower_part])
if ret:
img = Image.fromarray(final_image)
photo = ImageTk.PhotoImage(image=img)
canvas.create_image(0, 0, image=photo, anchor=NW)
canvas.image = photo
root.after(delay, update)
您将获得六个图像连接在一起并放置在您的tkinter框架上。有关在OpenCV中进行连接的更多信息,请查看这里:https://stackoverflow.com/questions/7589012/combining-two-images-with-opencv
英文:
You can use cv2.vconcat
and cv2.hconcat
to unite your images and show them at once.
For example if you modify your update function like this:
def update():
ret,frame = get_frame()
im1 = frame #your first desired image
im2 = frame.copy() #second image
im3 = frame.copy() #third image
im4 = frame.copy() #fourth image
im5 = frame.copy() #fifth image
im6 = frame.copy() #last image
upper_part = cv2.hconcat([im1,im2,im3])
lower_part= cv2.hconcat([im4,im5,im6])
final_image = cv2.vconcat([upper_part,lower_part])
if ret :
img = Image.fromarray(final_image)
photo = ImageTk.PhotoImage(image=img)
canvas.create_image(0, 0, image = photo, anchor = NW)
canvas.image=photo
root.after(delay,update)
You will get the six image concatenated and placed on your tkinter frame. Check here for more info on concatenation in opencv: https://stackoverflow.com/questions/7589012/combining-two-images-with-opencv
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论