Convert image loaded as binary string into numpy array

孤者浪人 提交于 2020-02-24 16:56:27

问题


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!


回答1:


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)


来源:https://stackoverflow.com/questions/50821145/convert-image-loaded-as-binary-string-into-numpy-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!