You can get a numpy array from you decoded data using:

import numpy as np
...
img = base64.b64decode(msg.payload)
npimg = np.fromstring(img, dtype=np.uint8)

Then you need imdecode to read the image from a buffer in memory. imread is meant to load an image from a file.

So:

import numpy as np
...
def on_message(client, userdata, msg): # msg.payload is incoming data
    img = base64.b64decode(msg.payload); 
    npimg = np.fromstring(img, dtype=np.uint8); 
    source = cv2.imdecode(npimg, 1)
Answer from Miki on Stack Overflow
🌐
jdhao's digital space
jdhao.github.io › 2020 › 03 › 17 › base64_opencv_pil_image_conversion
Conversion between base64 and OpenCV or PIL Image · jdhao's digital space
May 7, 2021 - import base64 import numpy as np import cv2 img = cv2.imread('test.jpg') _, im_arr = cv2.imencode('.jpg', img) # im_arr: image in Numpy one-dim array format.
Top answer
1 of 5
20

I've been struggling with this issue for a while now and of course, once I post a question - I figure it out.

For my particular use case, I needed to convert the string into a PIL Image to use in another function before converting it to a numpy array to use in OpenCV. You may be thinking, "why convert to RGB?". I added this in because when converting from PIL Image -> Numpy array, OpenCV defaults to BGR for its images.

Anyways, here's my two helper functions which solved my own question:

import io
import cv2
import base64 
import numpy as np
from PIL import Image

# Take in base64 string and return PIL image
def stringToImage(base64_string):
    imgdata = base64.b64decode(base64_string)
    return Image.open(io.BytesIO(imgdata))

# convert PIL Image to an RGB image( technically a numpy array ) that's compatible with opencv
def toRGB(image):
    return cv2.cvtColor(np.array(image), cv2.COLOR_BGR2RGB)
2 of 5
19

Here an example for python 3.6 that uses imageio instead of PIL. It first loads an image and converts it to a b64_string. This string can then be sent around and the image reconstructed as follows:

import base64
import io
import cv2
from imageio import imread
import matplotlib.pyplot as plt

filename = "yourfile.jpg"
with open(filename, "rb") as fid:
    data = fid.read()

b64_bytes = base64.b64encode(data)
b64_string = b64_bytes.decode()

# reconstruct image as an numpy array
img = imread(io.BytesIO(base64.b64decode(b64_string)))

# show image
plt.figure()
plt.imshow(img, cmap="gray")

# finally convert RGB image to BGR for opencv
# and save result
cv2_img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.imwrite("reconstructed.jpg", cv2_img)
plt.show()
🌐
PyPI
pypi.org › project › a-cv-imwrite-imread-plus
a-cv-imwrite-imread-plus · PyPI
November 10, 2022 - $pip install a-cv-imwrite-imread-plus from a_cv_imwrite_imread_plus import add_imwrite_plus_imread_plus_to_cv2 add_imwrite_plus_imread_plus_to_cv2() cv2.imwrite_plus("f:\\ö\\ö\\ö\\öädssdzß.jpg", base64img2cv) #or: from a_cv_imwrite_imread_plus import save_cv_image save_cv_image("f:\\...
      » pip install a-cv-imwrite-imread-plus
    
Published   Aug 14, 2023
Version   0.13
🌐
CSDN
devpress.csdn.net › python › 630455f1c67703293080b5f7.html
Read a base 64 encoded image from memory using OpenCv python library_python_Mangs-Python
How do I get a handle on that image in memory so that I can do cv2.imshow('image',img) and all kinds of cool stuff thereafter. I hope I was able to make myself clear. ... import base64 from PIL import Image import cv2 from StringIO import StringIO import numpy as np def readb64(base64_string): sbuf = StringIO() sbuf.write(base64.b64decode(base64_string)) pimg = Image.open(sbuf) return cv2.cvtColor(np.array(pimg), cv2.COLOR_RGB2BGR) cvimg = readb64('R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7') cv2.imshow(cvimg)
🌐
GitHub
gist.github.com › patharanordev › e22f2fe1c2593c9e5d9f7fbd00d8da09
Convert image from cv2.imread to img.src in HTML · GitHub
import cv2 import base64 def encode_img(img, im_type): """Encodes an image as a png and encodes to base 64 for display.""" success, encoded_img = cv2.imencode('.{}'.format(im_type), img) if success: return base64.b64encode(encoded_img).decode() ...
🌐
Stack Overflow
stackoverflow.com › questions › 51257124 › cv2-imread-alters-the-base64-string-from-images-and-generates-different-results
python - cv2.imread alters the base64 string from images and generates different results between OSes - Stack Overflow
Using plain Python after using cv2.imwrite for what cv2.imread gets: import cv2 from base64 import b64encode cv2.imwrite("ioimage_file.jpg", cv2.imread("image_file.jpg")) with open("ioimage_file.jpg", "rb") as file: iobase64string = b64encode(file.read()).decode("UTF-8")
Find elsewhere
Top answer
1 of 2
87

You do not need to save the buffer to a file. The following script captures an image from a webcam, encodes it as a JPG image, and then converts that data into a printable base64 encoding which can be used with your JSON:

import cv2
import base64

cap = cv2.VideoCapture(0)
retval, image = cap.read()
retval, buffer = cv2.imencode('.jpg', image)
jpg_as_text = base64.b64encode(buffer)
print(jpg_as_text)
cap.release()

Giving you something starting like:

/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCg

This could be extended to show how to convert it back to binary and then write the data to a test file to show that the conversion was successful:

import cv2
import base64

cap = cv2.VideoCapture(0)
retval, image = cap.read()
cap.release()

# Convert captured image to JPG
retval, buffer = cv2.imencode('.jpg', image)

# Convert to base64 encoding and show start of data
jpg_as_text = base64.b64encode(buffer)
print(jpg_as_text[:80])

# Convert back to binary
jpg_original = base64.b64decode(jpg_as_text)

# Write to a file to show conversion worked
with open('test.jpg', 'wb') as f_output:
    f_output.write(jpg_original)

To get the image back as an image buffer (rather than JPG format) try:

jpg_as_np = np.frombuffer(jpg_original, dtype=np.uint8)
image_buffer = cv2.imdecode(jpg_as_np, flags=1)
2 of 2
31

Some how the above answer doesn't work for me, it needs some update. Here is the new answer to this:

To encode for JSON:

import base64
import json
import cv2

img = cv2.imread('./0.jpg')
string = base64.b64encode(cv2.imencode('.jpg', img)[1]).decode()
dict = {
    'img': string
}
with open('./0.json', 'w') as outfile:
    json.dump(dict, outfile, ensure_ascii=False, indent=4)

To decode back to np.array:

import base64
import json
import cv2
import numpy as np

response = json.loads(open('./0.json', 'r').read())
string = response['img']
jpg_original = base64.b64decode(string)
jpg_as_np = np.frombuffer(jpg_original, dtype=np.uint8)
img = cv2.imdecode(jpg_as_np, flags=1)
cv2.imwrite('./0.jpg', img)

Hope this could help someone :P

🌐
OpenCV Q&A Forum
answers.opencv.org › question › 215404 › reading-data-uri-with-opencvjs
reading data uri with opencv.js - OpenCV Q&A Forum
(in the same way, imread() does it, just look at helper.js / utils.js) ... Berak , thx for your note, I found a great site that explains datauri conversion options with opencv.js https://github.com/justadudewhohacks/... // load base64 encoded image const base64text='data:image/png;base64,R0lGO..';//Base64 encoded string const base64data =base64text.replace('data:image/jpeg;base64','') .replace('data:image/png;base64','');//Strip image type prefix const buffer = Buffer.from(base64data,'base64'); const image = cv.imdecode(buffer); //Image is now represented as Mat
🌐
CodoRaven
codoraven.com › home › image & base64 | opencv vs pillow | python
Image & Base64 | OpenCV vs Pillow | Python - CodoRaven
March 16, 2023 - import base64 import numpy as np import cv2 # OpenCV to base64 def cv2_base64(cv2_img): base64_str = cv2.imencode('.jpg', cv2_img)[1].tostring() base64_str = base64.b64encode(base64_str) return base64_str # base64 to OpenCV def base64_cv2(base64_str): imgString = base64.b64decode(base64_str) nparr = np.fromstring(imgString, np.uint8) cv2_img= cv2.imdecode(nparr, cv2.IMREAD_COLOR) return cv2_img ·
🌐
McNeel Forum
discourse.mcneel.com › grasshopper
B64 String to Image - Grasshopper - McNeel Forum
July 26, 2023 - I have this code I am running in python on vscode and I am trying to send that base64 string through to rhino compute which will basically get me an image and turn it into a mesh. img = cv2.imread('./tile_60293_39317.png') png_img = cv2.imencode('.png', img) b64_string = base64.b64encode(p...
🌐
Medium
annacsmedeiros.medium.com › efficient-image-processing-in-python-a-straightforward-guide-to-base64-and-numpy-conversions-e9e3aac13312
Efficient Image Processing in Python: A Straightforward Guide to Base64 and Numpy Conversions | by Anna C S Medeiros | Medium
March 30, 2024 - import numpy as np import base64 import cv2 decoded = base64.b64decode(b64) nparr = np.frombuffer(decoded, np.uint8) bgr_img = cv2.imdecode(nparr, flags=cv2.IMREAD_COLOR)
🌐
GitHub
github.com › justadudewhohacks › face-recognition.js › issues › 45
Load base64 image · Issue #45 · justadudewhohacks/face-recognition.js
February 21, 2018 - "imread": { "step": 276, "elemSize": 3, "sizes": [ 112, 92 ], "empty": false, "depth": 0, "dims": 2, "channels": 3, "type": 16, "cols": 92, "rows": 112 }, "imdecode": { "step": 92, "elemSize": 1, "sizes": [ 112, 92 ], "empty": false, "depth": 0, "dims": 2, "channels": 1, "type": 0, "cols": 92, "rows": 112 }
Author   justadudewhohacks
🌐
OpenCV
docs.opencv.org › 4.13.0 › d4 › da8 › group__imgcodecs.html
OpenCV: Image file reading and writing
The imread function loads an image from the specified file and returns OpenCV matrix.
🌐
Stack Overflow
stackoverflow.com › questions › 65910507 › turning-an-open-cv-frame-into-a-base64-encoded-jpeg
python - Turning an Open CV frame into a Base64 encoded JPEG - Stack Overflow
import base64 import numpy as np import cv2 img = cv2.imread('test.jpg') _, im_arr = cv2.imencode('.jpg', img) # im_arr: image in Numpy one-dim array format.
🌐
GitHub
github.com › justadudewhohacks › opencv4nodejs › issues › 142
imread question · Issue #142 · justadudewhohacks/opencv4nodejs
January 8, 2018 - Its not an issue its a question Hey dude :) I just have a question I am getting images in form of a base 64 encoded string how can i convert this string into Mat ? I would like to skip using fs.writefile and then reading it with imread.
Author   justadudewhohacks
🌐
Programmer Click
programmerclick.com › article › 4933790313
Imagen ---- código base64 ---- imagen en formato cv2 - programador clic
Etiquetas: opencv base64 · Enlace del blog de referencia:https://blog.csdn.net/qq_24502469/article/details/82495252 · Es un poco incómodo escribir una imagen y leerla después de transcodificarla y luego girarla. Es bueno encontrar un blog por accidente. Marcarlo. El código se muestra a continuación: image = cv2.imread ("Ruta de la imagen") #Transcode image = cv2.imencode('.jpg', image)[1] image_code = str(base64.b64encode(image))[2:-1] # decodificación base64 img_data = base64.b64decode(image_code) # Convertir a matriz np img_array = np.frombuffer(img_data, np.uint8) # Convertir a formato utilizable opencv img = cv2.imdecode(img_array, cv2.COLOR_RGB2BGR) cv2.imshow("pp",img) cv2.waitKey(0) Imagen a base64 Datos de formato Método 1: Métodos proporcionados a través de Canvas.js.