简体   繁体   English

将以二进制字符串形式加载的图像转换为numpy数组

[英]Convert image loaded as binary string into numpy array

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)? 是否有一种方法可以将以二进制字符串形式加载的图像转换为大小为(im_height,im_width,3)的numpy数组? 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM