How to convert torch.tensor to base64 image? - pytorch

Here is my tensor:
import torch
from torchvision import transforms
content:
tensor([[[[0.8939, 0.8700, 0.8458, ..., 0.7610, 0.7093, 0.6909],
.
.
.
[0.4880, 0.5192, 0.5957, ..., 0.8569, 0.9148, 0.9186]]]])
I want to convert this torch.Tensor to base64. I've tried this but I can not convert PILImage to base64 too.
img = transforms.ToPILImage(content)
How can I do this?

You should first convert your image to bytes to encode afterward your image to base64 image
import torch
from torchvision import transforms
import base64
tensor = torch.randn(3, 512, 512)
transform = transforms.ToPILImage()
pil_image = transform(tensor)
# Convert the PIL image to bytes
image_bytes = pil_image.tobytes()
# Encode the bytes to base64
base64_string = base64.b64encode(image_bytes).decode()
print(base64_string)
Output:
00Hbhc4yZEZnw/VlIyHF6OH2VRAXeRdYiH82tBgSBoocDAB+cycknk3c5G9ZmdbHNu90whgDK49cjELZ6uJCzqYxVclXbJRc0Nb2AgN9XWULFOK+ubWJAyNbAgNZRvmwocwJN4603

Related

How to decode a byte arrray in python using cv2

Hi, I'm new to opencv and I'm trying to decode a byte array
From one side I'm sending I need to send a message in bytes format, and I'm using this code:
image_bytes = cv2.imencode('.jpg', imageRGB)[1].tobytes()
And from the receiving side, I'm am receiving a message with the following type: <class 'str'>
with this content: /9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoK ...
I tried the following: (x['other']['contentBytes'] is where the bytes are)
nparr = np.fromstring(x['other']['contentBytes'], np.uint8)
This returns a ( <class 'numpy.ndarray'> ) with the following shape: (40672,)
And when I try to
newFrame = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
I get a <class 'NoneType'> type.
Your image is base64-encoded so you need to decode it first:
from base64 import b64decode
import numpy as np
import cv2
# Extract JPEG-encoded image from base64-encoded string
JPEG = b64decode(YOURDATA)
# Decode JPEG back into Numpy array
na = cv2.imdecode(np.frombuffer(JPEG,dtype=np.uint8), cv2.IMREAD_COLOR)

how to pass an array instead of path in keras preprocessor

I want to convert an image that was loaded by cv2 into an specific format with keras preprocessor.
The keras.preprocessing.image.load_image() function takes input as path but i have only the numpy array.
I need help in converting the image into specfic format
The code i want to do is:
def convert(img):
img = load_img(imge, grayscale=True, target_size=(28, 28))
img = img_to_array(imge)
img = img.reshape(1, 28, 28, 1)
img = img.astype('float32')
img = img / 255.0
return img
Is there any other way to get the above format with numpy array and not with image path.
i got the output by the following method
import numpy as np
import cv2
img=cv2.imread('check.jpg')
grey=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
resized=cv2.resize(grey,(28,28))
np_arr=np.array(resized)
np_arr=np_arr.astype('float32')/255
print(np_arr.size)

how to show binary image data in python?

i can show the image using image.open, but how do i display from the binary data?
trying to use plot gets: ValueError: x and y can be no greater than 2-D, but have shapes (64,) and (64, 64, 3). this makes sense as that is what the result is supposed to be, but how do i display it?
import pathlib
import glob
from os.path import join
import matplotlib.pyplot as plt
from PIL import Image
import tensorflow as tf
def parse(image): # my like ings, but with .png instead of .jpeg.
image_string = tf.io.read_file(image)
image = tf.image.decode_png(image_string, channels=3)
image = tf.image.convert_image_dtype(image, tf.float32)
image = tf.image.resize(image, [64, 64])
return image
root = "in/flower_photos/tulips"
path = join(root,"*.jpg")
files = sorted(glob.glob(path))
file=files[0]
image = Image.open(file)
image.show()
binary=parse(file)
print(type(binary))
# how do i see this?
#plt.plot(binary) # does not seem to work
#plt.show() # does not seem to work
found a nice pillow tutorial.
from matplotlib import image
from matplotlib import pyplot
from PIL import Image
# load the image
filename='Sydney-Opera-House.jpg'
im = Image.open(filename)
# summarize some details about the image
print(im.format)
print(im.mode)
print(im.size)
# show the image
#image.show()
# load image as pixel array
data = image.imread(filename)
# summarize shape of the pixel array
print(data.dtype)
print(data.shape)
# display the array of pixels as an image
pyplot.imshow(data)
pyplot.show()

cv2 imdecode returns none with base64 string

When i try to convert base64 string into image i am getting none in CV2.
Following is my code
import cv2
import numpy as np
def bsae62toimage(imgvalue):
imge = "R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7"
nparr = np.fromstring(imge,np.uint8)
print(nparr.shape)
img1 = cv2.imdecode(nparr,cv2.IMREAD_UNCHANGED)
print(img1.shape)
It is printing the numpy array but cv2.imdecode is returning None. Can you help me to find the problem in my code?
My Findings: np.fromstring it returns 1D array. I assume it should it should return 3D array. But i may be wrong.
Since it is unclear as from where have you obtained the imge = "R0lGODlhEA..." variable. I would present an ideal flow for converting OpenCV image to base64 string and converting it back to OpenCV img as:
import cv2
import base64
import numpy as np
def to_base64(img):
_, buf = cv2.imencode(".png", img)
return base64.b64encode(buf)
def from_base64(buf):
buf_decode = base64.b64decode(buf)
buf_arr = np.fromstring(buf_decode, dtype=np.uint8)
return cv2.imdecode(buf_arr, cv2.IMREAD_UNCHANGED)
img = cv2.imread("/path/to/img.png")
img_base64 = to_base64(img)
img_decoded = from_base64(img_base64)
print img_decoded.shape
Also the documentation states that:
If the buffer is too short or contains invalid data, the empty
matrix/image is returned.
It seems to me that imge = "R0lGODlhEA..." is invalid.

How to convert binary image to RGB with PIL?

I have PIL Image in binary and I need to convert it in RGB. I did this diskew image
binary image
I need this way:
I already tried this which is not working
from PIL import Image as im
img = im.fromarray((255 * Image).astype("uint8")).convert("RGB")
I still don't understand why you convert to RGBA if you want RGB, but this code converts your image to RGB as you ask:
#!/usr/local/bin/python3
import numpy as np
from PIL import Image
# Open input image
im = Image.open('text.png').convert('RGB')
# Invert
npim = 255 - np.array(im)
# Save
Image.fromarray(npim).save('result.png')

Resources