Python OpenCV从字节字符串加载图像

时间:2023-01-24 19:18:11

I'm trying to load image from string like as PHP function imagecreatefromstring

我正在尝试从字符串加载图像,如PHP函数imagecreatefromstring

How can I do that?

我怎样才能做到这一点?

I have MySQL blob field image. I'm using MySQLdb and don't want create temporary file for working with images in PyOpenCV.

我有MySQL blob字段图像。我正在使用MySQLdb,并且不希望创建临时文件来处理PyOpenCV中的图像。

NOTE: need cv (not cv2) wrapper function

注意:需要cv(不是cv2)包装函数

2 个解决方案

#1


60  

This is what I normally use to convert images stored in database to OpenCV images in Python.

这是我通常用于将存储在数据库中的图像转换为Python中的OpenCV图像。

import numpy as np
import cv2
from cv2 import cv

# Load image as string from file/database
fd = open('foo.jpg')
img_str = fd.read()
fd.close()

# CV2
nparr = np.fromstring(img_str, np.uint8)
img_np = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR) # cv2.IMREAD_COLOR in OpenCV 3.1

# CV
img_ipl = cv.CreateImageHeader((img_np.shape[1], img_np.shape[0]), cv.IPL_DEPTH_8U, 3)
cv.SetData(img_ipl, img_np.tostring(), img_np.dtype.itemsize * 3 * img_np.shape[1])

# check types
print type(img_str)
print type(img_np)
print type(img_ipl)

I have added the conversion from numpy.ndarray to cv2.cv.iplimage, so the script above will print:

我已经添加了从numpy.ndarray到cv2.cv.iplimage的转换,因此上面的脚本将打印:

<type 'str'>
<type 'numpy.ndarray'>
<type 'cv2.cv.iplimage'>

#2


4  

I've try to use this code to create an opencv from a string containing a raw buffer (plain pixel data) and it doesn't work in that peculiar case.

我尝试使用此代码从包含原始缓冲区(纯像素数据)的字符串创建opencv,并且它不适用于那种特殊情况。

So here's how to do that for this kind of data:

所以这里是如何为这种数据做到这一点:

image = np.fromstring(im_str, np.uint8).reshape( h, w, nb_planes )

(but yes you need to know your image properties)

(但是,你需要知道你的图像属性)

if your B and G channel is permuted, here's how to fix it:

如果您的B和G频道被置换,以下是如何解决它:

image = cv2.cvtColor(image, cv2.cv.CV_BGR2RGB)

#1


60  

This is what I normally use to convert images stored in database to OpenCV images in Python.

这是我通常用于将存储在数据库中的图像转换为Python中的OpenCV图像。

import numpy as np
import cv2
from cv2 import cv

# Load image as string from file/database
fd = open('foo.jpg')
img_str = fd.read()
fd.close()

# CV2
nparr = np.fromstring(img_str, np.uint8)
img_np = cv2.imdecode(nparr, cv2.CV_LOAD_IMAGE_COLOR) # cv2.IMREAD_COLOR in OpenCV 3.1

# CV
img_ipl = cv.CreateImageHeader((img_np.shape[1], img_np.shape[0]), cv.IPL_DEPTH_8U, 3)
cv.SetData(img_ipl, img_np.tostring(), img_np.dtype.itemsize * 3 * img_np.shape[1])

# check types
print type(img_str)
print type(img_np)
print type(img_ipl)

I have added the conversion from numpy.ndarray to cv2.cv.iplimage, so the script above will print:

我已经添加了从numpy.ndarray到cv2.cv.iplimage的转换,因此上面的脚本将打印:

<type 'str'>
<type 'numpy.ndarray'>
<type 'cv2.cv.iplimage'>

#2


4  

I've try to use this code to create an opencv from a string containing a raw buffer (plain pixel data) and it doesn't work in that peculiar case.

我尝试使用此代码从包含原始缓冲区(纯像素数据)的字符串创建opencv,并且它不适用于那种特殊情况。

So here's how to do that for this kind of data:

所以这里是如何为这种数据做到这一点:

image = np.fromstring(im_str, np.uint8).reshape( h, w, nb_planes )

(but yes you need to know your image properties)

(但是,你需要知道你的图像属性)

if your B and G channel is permuted, here's how to fix it:

如果您的B和G频道被置换,以下是如何解决它:

image = cv2.cvtColor(image, cv2.cv.CV_BGR2RGB)