在Python中将openCV图像转换为PIL图像(与Zbar库一起使用) [英] convert openCV image into PIL Image in Python (for use with Zbar library)

查看:98
本文介绍了在Python中将openCV图像转换为PIL图像(与Zbar库一起使用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对使用OpenCV的摄像头方法提取的图像使用Zbar库的QR码检测方法.通常,QR码检测方法适用于计算机上的图像(jpg,png等),但是我猜想OpenCV捕获的帧是不同的.
有没有办法将捕获的帧变成PIL图像?

I'm trying to use the Zbar library's QR code detection methods on images I extract with OpenCV's camera methods. Normally the QR code detection methods work with images (jpg, png, etc.) on my computer, but I guess the captured frames of OpenCV are different.
Is there a way of making the captured frame into a PIL Image?

谢谢.

from PIL import Image
import zbar
import cv2.cv as cv

capture = cv.CaptureFromCAM(1)
imgSize = cv.GetSize(cv.QueryFrame(capture))
img = cv.QueryFrame(capture)

#SOMETHING GOES HERE TO TURN FRAME INTO IMAGE
img = img.convert('L')
width, height = img.size

scanner = zbar.ImageScanner()
scanner.parse_config('enable')
zbar_img = zbar.Image(width, height, 'Y800', img.tostring())

# scan the image for barcodes
scanner.scan(zbar_img)

for symbol in zbar_img:
    print symbol.data

推荐答案

使用python CV2,您还可以执行以下操作:

With the python CV2, you can also do this:

import Image, cv2

cap = cv2.VideoCapture(0) # says we capture an image from a webcam
_,cv2_im = cap.read()
cv2_im = cv2.cvtColor(cv2_im,cv2.COLOR_BGR2RGB)
pil_im = Image.fromarray(cv2_im)
pil_im.show()

这篇关于在Python中将openCV图像转换为PIL图像(与Zbar库一起使用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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