如何从内存中解码jpg图像? [英] How to decode jpg image from memory?

查看:316
本文介绍了如何从内存中解码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)

这篇关于如何从内存中解码jpg图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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