如何在一个tkinter框架中显示多个图像副本和源图像。

huangapple go评论91阅读模式
英文:

how to display multiple image copies and source image in one tkinter frame

问题

我能在一个帧中显示保存的mp4文件,并想尝试复制源文件并在一个帧中显示源mp4,如下图所示:

如何在一个tkinter框架中显示多个图像副本和源图像。

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

如何在一个tkinter框架中显示多个图像副本和源图像。

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.vconcatcv2.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

huangapple
  • 本文由 发表于 2023年7月18日 11:12:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709309.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定