Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)? 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!

like image 848
Thommy257 Avatar asked Jan 28 '23 22:01

Thommy257


1 Answers

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)
like image 158
Andriy Makukha Avatar answered Jan 30 '23 13:01

Andriy Makukha