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
🌐
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 - In the above code, we first convert binary image to Numpy array, then decode the array with cv2.imdecode(). The final img is an OpenCV image in Numpy ndarray format. import base64 from io import BytesIO from PIL import Image img = Image.open('test.jpg') im_file = BytesIO() img.save(im_file, format="JPEG") im_bytes = im_file.getvalue() # im_bytes: image in binary format.
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

🌐
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
🌐
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 - Specifically, for a JPG file, the base64 encoding process preserves the original image's content in its entirety, ensuring a lossless conversion due to the absence of compression.
🌐
GitHub
gist.github.com › HoweChen › 7cdd09b08147133d8e1fbe9b52c24768
[opencv_from_base64]read image from opencv with base64 encoding #OpenCV · GitHub
Save HoweChen/7cdd09b08147133d8e1fbe9b52c24768 to your computer and use it in GitHub Desktop. Download ZIP · [opencv_from_base64]read image from opencv with base64 encoding #OpenCV · Raw · opencv_from_base64.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
🌐
OpenCV Q&A Forum
answers.opencv.org › question › 174328 › base64-to-mat-and-mat-to-base64
base64 to Mat and Mat to base64 - OpenCV Q&A Forum
Hi, I have very poor processing time with base64 image using C++. I don't use third library as boost... After couple of hour to google search, I use this code to encode and decode image into base 64: http://renenyffenegger.ch/notes/devel... My problem is my processing time is 40ms to decode ...
Find elsewhere
🌐
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)
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()
🌐
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 - import cv2 · import base64 · import json · import numpy as np · ###################################################### #read image · img = cv2.imread('./code_backup/test_img.jpg') #cv2 to string · image_string = cv2.imencode('.jpg', img)[1] image_string = base64.b64encode(image_string).decode() #make string image dict ·
🌐
GeeksforGeeks
geeksforgeeks.org › python-opencv-imencode-function
Python OpenCV - imencode() Function - GeeksforGeeks
January 3, 2023 - cv2.imdecode() function is used to read image data from a memory cache and convert it into image format.
🌐
GitHub
github.com › justadudewhohacks › opencv-electron › issues › 7
Converting matrix to base64 string · Issue #7 · justadudewhohacks/opencv-electron
January 21, 2018 - Hey, I have problems converting Mat to a base64 string. I have this stream function that basically creates opencv mat and sends it to another window: export const stream = (video, canvas) => { /...
Author   justadudewhohacks
🌐
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() ...
🌐
GitHub
github.com › RonnyldoSilva › OpenCV_Mat_to_Base64
GitHub - RonnyldoSilva/OpenCV_Mat_to_Base64: What does this algorithm do? Convert Mat (OPENCV) to base64, and vise-versa. · GitHub
What does this algorithm do? Convert Mat (OPENCV) to base64, and vise-versa. - RonnyldoSilva/OpenCV_Mat_to_Base64
Starred by 69 users
Forked by 23 users
Languages   C++
🌐
CSDN
devpress.csdn.net › python › 630463bb7e6682346619aec9.html
Convert base64 String to an Image that's compatible with OpenCV_python_Mangs-Python
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)
🌐
Stack Overflow
stackoverflow.com › questions › 64245597 › python-opencv-base64-issue-with-converting-frame-to-base64
Python + OpenCV + Base64: Issue with converting frame to base64 - Stack Overflow
Also it is a opencv image (numpy array) which is not something you can use "with" on. def frame_to_base64(frame): return base64.b64encode(frame) If you want to read all frames of video, you should do something like: import cv2 import base64 def footage_to_frame(video): vidcap = cv2.VideoCapture(video) frames = [] # read until no more frames exist in the video while True: success, frame = vidcap.read() if (success): frames.append(frame) else: # unable to read a frame break return frames def frames_to_base64(frames): frames_b64 = [] # iterate frames and convert each of them to base64 for frame in frames: frames_b64.append(base64.b64encode(frame)) return frames_b64