Python 对图像进行base64编码及解码读取为numpy、opencv、matplot需要的格式
生活随笔
收集整理的這篇文章主要介紹了
Python 对图像进行base64编码及解码读取为numpy、opencv、matplot需要的格式
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Python 對(duì)圖像進(jìn)行base64編碼及解碼讀取為numpy、opencv、matplot需要的格式
- 1. 效果圖
- 2. 源碼
- 參考
這篇博客將介紹Python如何對(duì)圖像進(jìn)行base64編解碼及讀取為numpy、opencv、matplot需要的格式。
1. 效果圖
原始圖如下:
base64轉(zhuǎn)換為numpy數(shù)組效果圖如下(一維):
base64轉(zhuǎn)換為opencv BGR VS 灰度效果圖如下:
base64轉(zhuǎn)換為 matplot RGB VS 灰度效果圖如下:
2. 源碼
- base64編碼圖片為string
- base64解碼string為圖片
- base64解碼string為numpy、opencv、matplot圖片
# 對(duì)圖片進(jìn)行base64編碼,解碼,解碼為numpy,opencv,matplot照片
# USAGE
# python base64_2_jpg.pyimport base64import cv2
import numpy as np
from matplotlib import pyplot as plt# 將字符串寫入文字
# name 圖片名
# base64_data 圖片二進(jìn)制編碼后string流
def write2txt(name, base64_data):# 寫入_base64.txtprint(name)print(name,len(base64_data))basef = open(name + '_base64.txt', 'w')data = 'data:image/jpg;base64,%s' % base64_data# print(data)basef.write(base64_data)basef.close()# 編碼圖像為base64字符串
def encode_base64(file):with open(file, 'rb') as f:img_data = f.read()base64_data = base64.b64encode(img_data)print(type(base64_data))# print(base64_data)# 如果想要在瀏覽器上訪問(wèn)base64格式圖片,需要在前面加上:data:image/jpeg;base64,base64_str = str(base64_data, 'utf-8')# print(base64_str)print(len(base64_data))write2txt(file.replace(".jpg", ""), base64_str)return base64_data# 解碼base64字符串為圖像,并保存
def decode_base64(base64_data):with open('./images/base64.jpg', 'wb') as file:img = base64.b64decode(base64_data)file.write(img)# 解碼base64字符串為numpy圖像、opencv、matplot圖像# 解碼base64字符串為numpy圖像
def decode_base64_np_img(base64_data):img = base64.b64decode(base64_data)img_array = np.fromstring(img, np.uint8) # 轉(zhuǎn)換np序列print('numpy: ', img_array.shape)cv2.imshow("img", img_array)cv2.waitKey(0)# 解碼base64字符串為opencv圖像
def decode_base64_cv_img(base64_data):img = base64.b64decode(base64_data)img_array = np.fromstring(img, np.uint8) # 轉(zhuǎn)換np序列img_raw = cv2.imdecode(img_array, cv2.IMREAD_COLOR) # 轉(zhuǎn)換Opencv格式BGRimg_gray = cv2.imdecode(img_array, cv2.IMREAD_GRAYSCALE) # 轉(zhuǎn)換灰度圖print('opencv bgr: ', img_raw.shape)print('opencv gray: ', img_gray.shape)cv2.imshow("img bgr", img_raw)cv2.imshow("img gray", img_gray)cv2.waitKey(0)# 解碼base64字符串為matplot圖像
def decode_base64_matplot_img(base64_data):img = base64.b64decode(base64_data)img_array = np.fromstring(img, np.uint8) # 轉(zhuǎn)換np序列img_raw = cv2.imdecode(img_array, cv2.IMREAD_COLOR) # 轉(zhuǎn)換Opencv格式BGRimg_matplot = cv2.cvtColor(img_raw, cv2.COLOR_BGR2RGB) # BGR轉(zhuǎn)RGBimg_gray = cv2.imdecode(img_array, cv2.IMREAD_GRAYSCALE) # 轉(zhuǎn)換灰度圖imggray_matplot = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2RGB) # 灰度圖轉(zhuǎn)RGBplt.figure()plt.title("Matplot RGB Origin Image")plt.axis("off")plt.imshow(img_matplot)plt.figure()plt.title("Matplot Gray Origin Image")plt.axis("off")plt.imshow(imggray_matplot)plt.show()if __name__ == '__main__':img_path = './images/1622175322109_0.025711.jpg'base64_data = encode_base64(img_path)decode_base64(base64_data)decode_base64_np_img(base64_data)decode_base64_cv_img(base64_data)decode_base64_matplot_img(base64_data)
參考
- https://blog.csdn.net/sinat_37967865/article/details/94554568
- https://blog.csdn.net/flyfish1986/article/details/79734711
總結(jié)
以上是生活随笔為你收集整理的Python 对图像进行base64编码及解码读取为numpy、opencv、matplot需要的格式的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 求一个好听的读书社团名字
- 下一篇: 开个面馆投资多少钱?