python base64 編解碼,轉換成Opencv,PIL.Image圖片格式

-牧野-發表於2018-06-01

二進位制開啟圖片檔案,base64編解碼,轉成Opencv格式:

# coding: utf-8
import base64
import numpy as np
import cv2

img_file = open(r'00.JPG','rb')   # 二進位制開啟圖片檔案
img_b64encode = base64.b64encode(img_file.read())  # base64編碼
img_file.close()  # 檔案關閉
img_b64decode = base64.b64decode(img_b64encode)  # base64解碼

img_array = np.fromstring(img_b64decode,np.uint8) # 轉換np序列
img=cv2.imdecode(img_array,cv2.COLOR_BGR2RGB)  # 轉換Opencv格式

cv2.imshow("img",img)
cv2.waitKey()


二進位制開啟圖片檔案,base64編解碼,轉成PIL.Image格式:

# coding: utf-8
# python base64 編解碼,轉換成Opencv,PIL.Image圖片格式
import base64
import io
from PIL import Image

img_file = open(r'/home/dcrmg/work/medi_ocr_v1.2/img/00.JPG','rb')   # 二進位制開啟圖片檔案
img_b64encode = base64.b64encode(img_file.read())  # base64編碼
img_file.close()  # 檔案關閉
img_b64decode = base64.b64decode(img_b64encode)  # base64解碼

image = io.BytesIO(img_b64decode)
img = Image.open(image)
img.show()

相關文章