函数cv2.imread读取的数据格式是什么?使用 tkinter 和 python [英] What is the data format read by the function cv2.imread? Working with tkinter and python

查看:213
本文介绍了函数cv2.imread读取的数据格式是什么?使用 tkinter 和 python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

美好的一天,我对 Python 编程还很陌生,我的任务是在我的 GUI 中使用图像来制作我自己的 GUI.我一直在取得一些不错的进展,但是当我想从我的网络摄像头将图像插入到我的 GUI 中时我被卡住了.但是,我确实设法从网络摄像头获取图像,但它必须是与 GUI 窗口不同的窗口.

Good day, I am quite new to Python programming and I was tasked to do my own GUI with image inside my GUI. I have been doing some good progress but i was stuck when I want to insert an image into my GUI from my webcam. However, I did manage to get an image from the webcam but it has to be a different window with the GUI window.

在我的 GUI 代码中,它包含一个像这样的简单代码:

In my GUI codes, it includes a simple code like this:

(我使用范围 i<25 因为我的网络摄像头需要预热)

(I use range i<25 because my webcam needs warming up)

对于范围内的 i (25):

for i in range (25):

     _ , frame = cap.read()
     frame = cv2.flip(frame, 1)
     cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)

     i+=1

     cv2.imshow("Latex Truck", cv2image)
     img = cv2image
     label = Label(root, image = img)
     label.place(x = 300, y = 300)

现在,问题是这个.由于 cv2.imshow,我成功获得了我需要的框架并且能够显示,但是当我尝试使用与 tkinter 中的cv2image"相同的源时,它显示了这个错误.

Now, the problem is this. I successfully obtain the frame that I need and was able to show thanks to cv2.imshow but when I try to use the same source which is the "cv2image" in tkinter, it shows this error.

Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1487, in __call__
    return self.func(*args)
  File "C:\Users\FF7_C\OneDrive\Desktop\Logo.py", line 82, in Capture
    label = Label(root, image = img)
  File "C:\Python34\lib\tkinter\__init__.py", line 2573, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)
  File "C:\Python34\lib\tkinter\__init__.py", line 2091, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "[[[ 49  32  22 255]

现在,从逻辑上讲,我认为我做了我需要做的事情,即从我所做的网络摄像头中提取图像,现在唯一的问题是我需要了解为什么 tkinter 无法读取由 cv2.imshow 读取的相同信息.

Now, logically I think I did what I needed to do which is the extract an image from the webcam which I did, the only problem now is I need to understand why tkinter cannot read the same information read by cv2.imshow.

有人可以指导我吗?非常感谢!:)

Can someone guide me on this? Thank you very much! :)

推荐答案

cv2.cvtColor(...) 返回的格式是 numpy.ndarray 类型.您需要使用 Pillow 模块将其转换为 tkinter 识别的格式:

The format returned by cv2.cvtColor(...) is of type numpy.ndarray. You need to convert it to format recognized by tkinter by using Pillow module:

from tkinter import *
from PIL import Image, ImageTk
import cv2

root = Tk()

cap = cv2.VideoCapture(0)
ret, frame = cap.read()

img = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
# convert to image format recognized by tkinter
img = Image.fromarray(img)
tkimg = ImageTk.PhotoImage(image=img)

Label(root, image=tkimg).pack()

root.mainloop()

这篇关于函数cv2.imread读取的数据格式是什么?使用 tkinter 和 python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆