Skip to content

Instantly share code, notes, and snippets.

@gachiemchiep gachiemchiep/
Created Aug 16, 2018

What would you like to do?
Write/Read image data on Redis using opencv or PIL
import redis
import cv2
import numpy as np
import time
import io
from PIL import Image
r = redis.StrictRedis.from_url('redis://:password@')
img_path ="image"
img1 = cv2.imread(img_path, 1)
retval, buffer = cv2.imencode('.png', img1)
img1_bytes = np.array(buffer).tostring()
# with open(img_path, "rb") as fid:
# img1_bytes =
# Write into redis server
r.set(img_path, img1_bytes)
# Decoding CV2
decoded = cv2.imdecode(np.frombuffer(img1_bytes, np.uint8), 1)
cv2.imwrite("cv2.png", decoded)
# Decoding PIL
image = np.array(
cv2.imwrite("PIL.png", decoded)
# Reading Redis
img1_bytes_ = r.get(img_path)
# Decoding CV2+Redis
decoded = cv2.imdecode(np.frombuffer(img1_bytes_, np.uint8), 1)
cv2.imwrite("cv2_redis.png", decoded)
# Decoding PIL+Redis
img1_bytes_ = r.get(img_path)
decoded = np.array(
cv2.imwrite("pil_redis.png", decoded)
Execution time is as follow (Does not include write time)
Encoding 0.00508904457092 seconds
Decoding CV2 0.191784858704 seconds
Decoding PIL 0.206390142441 seconds
Reading Redis 0.00943112373352 seconds
Decoding CV2+Redis 0.196936130524 seconds
Reading Redis 0.0152509212494 seconds
Decoding PIL+Redis 0.228559017181 seconds
CV2+Redis was a little better than PIL+Redis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.