将以二进制字符串形式加载的图像转换为numpy数组 [英] Convert image loaded as binary string into numpy array

查看:353
本文介绍了将以二进制字符串形式加载的图像转换为numpy数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以将以二进制字符串形式加载的图像转换为大小为(im_height,im_width,3)的numpy数组?像这样:

Is there a method to convert an image, that is loaded as a binary string, into a numpy array of size (im_height, im_width, 3)? Something like this:

# read image as binary string
with open(img_path, "rb") as image_file:
  image_string = image_file.read()

# convert image string to numpy
image_np = convert_binary_string_to_numpy(image_string)

该转换函数的外观如何?我正在使用解密,因此我需要使用二进制字符串. 谢谢!

How would that conversion function look like? I'm working with decryption, thus I need to work with binary strings. Thanks!

推荐答案

import io
import numpy as np    
from PIL import Image

image_string = open(IMG_PATH, 'rb').read()
img = Image.open(io.BytesIO(image_string))
arr = np.asarray(img)

这篇关于将以二进制字符串形式加载的图像转换为numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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