Created
August 16, 2018 02:26
-
-
Save gachiemchiep/52f3255a81c907461c2c7ced6ede367a to your computer and use it in GitHub Desktop.
Write/Read image data on Redis using opencv or PIL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import redis | |
import cv2 | |
import numpy as np | |
import time | |
import io | |
from PIL import Image | |
r = redis.StrictRedis.from_url('redis://:[email protected]:6379/1') | |
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 = fid.read() | |
# 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(Image.open(io.BytesIO(img1_bytes))) | |
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(Image.open(io.BytesIO(img1_bytes_))) | |
cv2.imwrite("pil_redis.png", decoded) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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