如何从内存中解码jpg图像?
我可以通过PIL,Python OpenCV等通过一些内置函数(例如(对于OpenCV)arr= cv2.imread(filename)
)将jpg图像从磁盘读取为numpy数组.
I can read a jpg image from disk via PIL, Python OpenCV, etc. into a numpy array via some built-in functions such as (in the case of OpenCV) arr= cv2.imread(filename)
.
但是如何直接从内存中解码二进制格式的jpg?
But how do I decode a jpg in binary format directly from memory?
用例:我想将jpg图像以二进制格式放入数据库中,然后从db中读取到内存中,并将其解码为numpy数组.
Use case: I want to put a jpg image into a database in binary format and then read it from the db into memory and decode it to a numpy array.
这可能吗?
假定您将图像数据作为string
存储在数据库中,则首先需要从该字符串构造一个numpy
数组,以便稍后使用使用 cv2.imdecode
转换为图像.例如:
Assuming that you are storing the image data in your db as a string
, you first need to construct a numpy
array from that string that can later be converted to an image using cv2.imdecode
. For example:
img = cv2.imdecode(np.fromstring(img_data, dtype=np.uint8), -1)