5

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!

Thomas
  • 705
  • 6
  • 21
  • 2
    Have you seen https://stackoverflow.com/questions/31883432/converting-jpeg-string-to-pil-image-object – jedwards Jun 12 '18 at 15:50
  • no, I didn't find that on my own, I'll check it out, seem very close to my problem. Thanks a lot! – Thomas Jun 12 '18 at 15:55

1 Answers1

10
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)
Andriy Makukha
  • 7,580
  • 1
  • 38
  • 49