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)
Answer from Martin Evans on Stack Overflow
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

Discussions

Read a base 64 encoded image from memory using OpenCv python library - Stack Overflow
I'm working on an app that to do some facial recognition from a webcam stream. I get base64 encoded data uri's of the canvas and want to use it to do something like this: cv2.imshow('image',img) The More on stackoverflow.com
🌐 stackoverflow.com
python - Sending OpenCV image in JSON - Stack Overflow
I'm trying to send an OpenCV image in a json and receive it on the other end but I'm running into endless problems encoding and decoding the image I send it in JSON in the following way: dumps({"... More on stackoverflow.com
🌐 stackoverflow.com
Decode base64 image in JSON with Python - Stack Overflow
I receive from front-end an image in base64 into JSON file, and need take image and decode with OpenCV, the JSON there is: { 'photo': "b'/9j/4AAQSkZJR...(continue)" } and code is ob... More on stackoverflow.com
🌐 stackoverflow.com
March 22, 2021
Encode Image to Base64 - Python
Haciendo referencia un poco a lo que se hace en este script: https://github.com/franyack/CIEngine/blob/master/test/base64/example.py La idea sería levantar la imagen desde archuvo con alguna librer... More on github.com
🌐 github.com
1
August 18, 2017
🌐
CopyProgramming
copyprogramming.com › howto › python-opencv-base64-issue-with-converting-frame-to-base64
Read Base64 Encoded Images from Memory Using OpenCV Python: Complete 2026 Guide - Encoded images from memory using opencv python complete
December 9, 2025 - This guide covers the complete ... making it suitable for transmission over text-only protocols. When images are Base64-encoded, they can be transmitted via HTTP, JSON, or stored as strings in databases....
🌐
CodoRaven
codoraven.com › home › image & base64 | opencv vs pillow | python
Image & Base64 | OpenCV vs Pillow | Python - CodoRaven
March 16, 2023 - import base64 from io import BytesIO from PIL import Image # Pillow to base64 def pil_to_base64(pil_img): img_buffer = BytesIO() pil_img.save(img_buffer, format='JPEG') byte_data = img_buffer.getvalue() base64_str = base64.b64encode(byte_data) return base64_str # base64 to Pillow def base64_to_pil(base64_str): pil_img = base64.b64decode(base64_str) pil_img = BytesIO(pil_img) pil_img = Image.open(pil_img) return pil_img · 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
Top answer
1 of 1
5

Hopefully, this should get you started. I think that what you tried, by sending the unadorned bytes from the Numpy array probably won't work because the receiver will not know the width, height and number of channels in the image, so I used pickle to store that.

#!/usr/bin/env python3

import cv2
import numpy as np
import base64
import json
import pickle
from PIL import Image

def im2json(im):
    """Convert a Numpy array to JSON string"""
    imdata = pickle.dumps(im)
    jstr = json.dumps({"image": base64.b64encode(imdata).decode('ascii')})
    return jstr

def json2im(jstr):
    """Convert a JSON string back to a Numpy array"""
    load = json.loads(jstr)
    imdata = base64.b64decode(load['image'])
    im = pickle.loads(imdata)
    return im

# Create solid red image 
red = np.full((480, 640, 3), [0, 0, 255], dtype=np.uint8)  

# Make image into JSON string
jstr = im2json(red)

# Extract image from JSON string, and convert from OpenCV to PIL reversing BGR to RGB on the way
OpenCVim = json2im(jstr)
PILimage = Image.fromarray(OpenCVim[...,::-1])
PILimage.show()

As you haven't answered my question in the comments about why you want do things this way, it may not be optimal - sending uncompressed, base64-encoded images across a network (presumably) is not very efficient. You might consider JPEG, or PNG encoded data to save network bandwidth, for example.

You could also use cPickle instead.


Note that some folks disapprove of pickle and also the method above uses a lot of network bandwidth. An alternative might be to JPEG compress the image before sending and decompress on the receiving end straight into a PIL Image. Note that this is lossy.

Or change the .JPG extension in the code to .PNG which is loss-less but may be slower and will not work for images with floating point data or 16-bit data (although the latter could be accommodated).

You could also look at TIFF, but again, it depends on the nature of your data, the network bandwidth, the flexibility you need, your CPU's encoding/decoding performance...

#!/usr/bin/env python3

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

def im2json(im):
    _, imdata = cv2.imencode('.JPG',im)
    jstr = json.dumps({"image": base64.b64encode(imdata).decode('ascii')})
    return jstr

def json2im(jstr):
    load = json.loads(jstr)
    imdata = base64.b64decode(load['image'])
    im = Image.open(BytesIO(imdata))
    return im

# Create solid red image 
red = np.full((480, 640, 3), [0, 0, 255], dtype=np.uint8)  

# Make image into JSON string
jstr = im2json(red)

# Extract image from JSON string into PIL Image
PILimage = json2im(jstr)
PILimage.show()
🌐
CodeSpeedy
codespeedy.com › home › convert image to base64 string in python
Convert Image to Base64 String in Python - CodeSpeedy
September 19, 2023 - Then we read the image file and encoded it with the following line: base64.b64encode(img_file.read()) – b64encode() is a method to encode the data into base64 · You must read the image file before you encode it.
Find elsewhere
🌐
Reddit
reddit.com › r/learnpython › opencv frame to json
r/learnpython on Reddit: openCV frame to json
April 26, 2019 - Hey, I've been trying to convert an openCV frame to base64 and send it using json but JSON can't send byte arrays. What do I do? And on the client…
🌐
Medium
medium.com › analytics-vidhya › faceapi-rest-api-to-detect-face-s-in-image-using-dlib-openvc-flask-8a6cdfb0571f
FaceApi: Rest Api to detect face(s) in image using dlib, openCV & Flask. | by Sambhav Choradia | Analytics Vidhya | Medium
December 18, 2019 - We have used dlib to detect the face(s) in an image and opencv to create a bounding box on face(s). detect() returns number of faces detected and the image in base64 string to return as json response for the api.
🌐
Stack Overflow
stackoverflow.com › questions › 66739086 › decode-base64-image-in-json-with-python
Decode base64 image in JSON with Python - Stack Overflow
March 22, 2021 - I receive from front-end an image in base64 into JSON file, and need take image and decode with OpenCV, the JSON there is: ... obj = json.loads(json.dumps(event)) obj = obj['foto'] obj = bytes(obj,'utf-8') obj_d = base64.decodebytes(obj) print(type(obj_d)) img_buffer = np.frombuffer(obj_d, dtype=np.uint8) print(img_buffer) img = cv2.imdecode(img_buffer, flags=cv2.IMREAD_COLOR) print(img.shape) And received error is, AttributeError: 'NoneType' object has no attribute 'shape'. When I code and decode in base64 the image in the Python, I not have problem.
🌐
Marearts
study.marearts.com › 2020 › 04 › python-opencv-image-to-byte-string-for.html
MareArts Computer Vision Study.: Python OpenCV Image to byte string for json transfer
April 16, 2020 - PCA (2) ROI (2) RTSP (2) ReSize (2) String and char* (2) Thread (2) VideoWriter (2) Window (2) Window 2008 (2) YUV (2) YUV422 (2) ai (2) ai model (2) alpr (2) anaconda (2) and (2) api (2) ascii (2) augmentation (2) barcode (2) base64 (2) bitwise_and (2) bitwise_or (2) bitwise_xor (2) blockIdx (2) boto3 (2) boundingRect (2) bucket (2) camera calibration (2) canny (2) centos (2) circle (2) clear (2) compare (2) concept (2) conda (2) condensation (2) copyTo (2) create folder (2) crop (2) custom node (2) cvFindContours (2) data access (2) datetime (2) deep learnining (2) dict shuffle (2) docker st
🌐
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 - This format is selected due to its lossless compression capability, ensuring no degradation of the original my_opencv_image quality. Additional configuration options can be adjusted using various flags, details of which can be found at the specified link. Caution: Usingencode_params=[int(cv2.IMWRITE_JPEG_QUALITY), 100]would result in loss, and the original image could not be restored. Even using 100 as the compression parameter, loss is inevitable. Therefore, we do not recommend this approach. 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 › franyack › CIEngine › issues › 1
Encode Image to Base64 - Python · Issue #1 · franyack/CIEngine
August 18, 2017 - import io import base64 from PIL import Image path_to_img = '/path/to/test/image.png' # TODO: modify this! img = Image.open(path_to_img) # Show #img.show() # Encode in_mem_file = io.BytesIO() img.save(in_mem_file, format =img.format) # reset ...
Author   franyack
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()
🌐
Javaer101
javaer101.com › en › article › 1752995.html
Python OpenCV Image to byte string for json transfer - Javaer101
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 ...
🌐
Medium
medium.com › @maggi.giuseppe › python-embedding-images-in-json-objects-with-base64-8579059d80d7
Python: embedding images in JSON objects with base64 | by Giuseppe Maggi | Medium
March 12, 2024 - In fact, we get the value of content of the first element in the images list: from io import BytesIO import base64, json from PIL import Image with open('embedded.json', 'r') as f: data_received=json.load(f) im = Image.open(BytesIO(base64.b...
🌐
GitHub
github.com › justadudewhohacks › opencv-electron › issues › 7
Converting matrix to base64 string · Issue #7 · justadudewhohacks/opencv-electron
January 21, 2018 - export const stream = (video, canvas) => { // Draw video frame to canvas canvas.getContext('2d').drawImage(video, 0, 0, GAME_WINDOW_WIDTH, GAME_WINDOW_HEIGHT) // Convert canvas to blob canvas.toBlob((blob) => { // Convert canvas to buffer toBuffer(blob, (err, buffer) => { // Convert buffer to opencv mat const mat = cv.imdecode(buffer) mat.getDataAsync((error, buffer) => { if (error) { throw error } // Send buffer to another window send(SEND_VIDEO_SCREEN, buffer) }) setTimeout(() => { stream(video, canvas) }, 1000 / FPS) }) }) } In another window I have this code (for simplicty I decided to pass the base64 string directly to image source)
Author   justadudewhohacks
🌐
CSDN
devpress.csdn.net › python › 630455f1c67703293080b5f7.html
Read a base 64 encoded image from memory using OpenCv python library_python_Mangs-Python
<img src="data:image/gif;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7">