日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

Python 对图像进行base64编码及解码读取为numpy、opencv、matplot需要的格式

發(fā)布時(shí)間:2023/11/27 生活经验 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python 对图像进行base64编码及解码读取为numpy、opencv、matplot需要的格式 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Python 對圖像進(jìn)行base64編碼及解碼讀取為numpy、opencv、matplot需要的格式

    • 1. 效果圖
    • 2. 源碼
    • 參考

這篇博客將介紹Python如何對圖像進(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圖片
# 對圖片進(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)# 如果想要在瀏覽器上訪問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需要的格式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。