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

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

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

问题

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

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

  1. from tkinter import *
  2. from PIL import Image, ImageTk
  3. import cv2
  4. root = Tk()
  5. root.title("Video!")
  6. # vid = cv2.VideoCapture("vid1.mp4")
  7. vid = cv2.VideoCapture(0)
  8. width = vid.get(cv2.CAP_PROP_FRAME_WIDTH)
  9. height = vid.get(cv2.CAP_PROP_FRAME_HEIGHT)
  10. def get_frame():
  11. ret, frame = vid.read()
  12. if ret:
  13. return (ret, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
  14. else:
  15. return (ret, None)
  16. def update():
  17. ret, frame = get_frame()
  18. if ret:
  19. img = Image.fromarray(frame)
  20. photo = ImageTk.PhotoImage(image=img)
  21. canvas.create_image(0, 0, image=photo, anchor=NW)
  22. canvas.image = photo
  23. root.after(delay, update)
  24. canvas = Canvas(root, width=width, height=height)
  25. canvas.pack()
  26. delay = 15
  27. update()
  28. root.mainloop()
  29. vid.release()
  30. 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框架中显示多个图像副本和源图像。

  1. from tkinter import *
  2. from PIL import Image, ImageTk
  3. import cv2
  4. root = Tk()
  5. root.title("Video!")
  6. # vid = cv2.VideoCapture("vid1.mp4")
  7. vid = cv2.VideoCapture(0)
  8. width = vid.get(cv2.CAP_PROP_FRAME_WIDTH)
  9. height = vid.get(cv2.CAP_PROP_FRAME_HEIGHT)
  10. def get_frame():
  11. ret,frame = vid.read()
  12. if ret :
  13. return(ret,cv2.cvtColor(frame,cv2.COLOR_BGR2RGB))
  14. else :
  15. return(ret,None)
  16. def update():
  17. ret,frame = get_frame()
  18. if ret :
  19. img = Image.fromarray(frame)
  20. photo = ImageTk.PhotoImage(image=img)
  21. canvas.create_image(0, 0, image = photo, anchor = NW)
  22. canvas.image=photo
  23. root.after(delay,update)
  24. canvas = Canvas(root, width = width, height = height)
  25. canvas.pack()
  26. delay = 15
  27. update()
  28. root.mainloop()
  29. vid.release()

thank you

答案1

得分: 1

您可以使用cv2.vconcatcv2.hconcat来合并您的图像并一次显示它们。

例如,如果您修改您的更新函数如下:

  1. def update():
  2. ret, frame = get_frame()
  3. im1 = frame #您的第一张期望的图像
  4. im2 = frame.copy() #第二张图像
  5. im3 = frame.copy() #第三张图像
  6. im4 = frame.copy() #第四张图像
  7. im5 = frame.copy() #第五张图像
  8. im6 = frame.copy() #最后一张图像
  9. upper_part = cv2.hconcat([im1, im2, im3])
  10. lower_part = cv2.hconcat([im4, im5, im6])
  11. final_image = cv2.vconcat([upper_part, lower_part])
  12. if ret:
  13. img = Image.fromarray(final_image)
  14. photo = ImageTk.PhotoImage(image=img)
  15. canvas.create_image(0, 0, image=photo, anchor=NW)
  16. canvas.image = photo
  17. 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:

  1. def update():
  2. ret,frame = get_frame()
  3. im1 = frame #your first desired image
  4. im2 = frame.copy() #second image
  5. im3 = frame.copy() #third image
  6. im4 = frame.copy() #fourth image
  7. im5 = frame.copy() #fifth image
  8. im6 = frame.copy() #last image
  9. upper_part = cv2.hconcat([im1,im2,im3])
  10. lower_part= cv2.hconcat([im4,im5,im6])
  11. final_image = cv2.vconcat([upper_part,lower_part])
  12. if ret :
  13. img = Image.fromarray(final_image)
  14. photo = ImageTk.PhotoImage(image=img)
  15. canvas.create_image(0, 0, image = photo, anchor = NW)
  16. canvas.image=photo
  17. 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:

确定