"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

June 13, 2020

Day #336 - Image Encoding / Decoding Python


import base64
import cv2
from io import BytesIO
from PIL import Image
import numpy as np
def encodeimage(inputfilename):
with open(inputfilename, "rb") as original_file:
encoded_string = base64.b64encode(original_file.read())
print(encoded_string)
return encoded_string
def decodeimage(encoded_string):
image = Image.open(BytesIO(base64.b64decode(encoded_string)))
image.show()
#color image
cv2colorimg = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR)
cv2.imshow('color image',cv2colorimg)
cv2.waitKey(0)
#grey image
cv2greyimg = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2GRAY)
cv2.imshow('color image',cv2greyimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
inputfilename = r'15.jpg'
encoded_string = encodeimage(inputfilename)
decodeimage(encoded_string)
#Good Reference bookmark
#https://jdhao.github.io/2020/04/12/build_webapi_with_flask_s2/
Happy Learning!!!

No comments: