I'm receiving an image via an http POST that is base64 encoded. It can be a JPG or a BMP. Now that I have the image, I can get it in memory. I found how to write it to disk and re-read it into a numpy array (Which I need to actually put into a torch.tensor but numpy will suffice for now).
HEre's what works for me but appears HIGHLY inefficient :
import torch
import numpy as np
from PIL import Image
import base64
base64_decoded = base64.b64decode(test_image_base64_encoded)
with open("out.jpg", "wb") as out_file:
out_file.write(base64_decoded)
image = Image.open("out.jpg")
image_np = np.array(image)
image_torch = torch.tensor(np.array(image))
It feels extremely useless to have to write the array to out.jpg to reread it right after into an array. There must be a better way. I've tried some things where it ends up in a 1D array... and my image is a 2D array in my case (BW image).
nparr = np.fromstring(base64.b64decode(test_image), np.uint8)
would yield when nparr.shape = (694463,) when image_np.shape = (2048, 3072)
Any idea how I could represent to np.array something like Image.frombase64 :) ? I know it doesn't exists per say but that would be great if it could somehow interpret the "file" without having to save it to disk first.
Assuming you're using PIL, but you don't know the image type or dimensions:
from PIL import Image
import base64
import io
import numpy as np
import torch
base64_decoded = base64.b64decode(test_image_base64_encoded)
image = Image.open(io.BytesIO(base64_decoded))
image_np = np.array(image)
image_torch = torch.tensor(np.array(image))
io.BytesIO is the key thing you're missing, I think.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With