python圖片和二進(jìn)制轉(zhuǎn)換的三種實(shí)現(xiàn)方式
PIL格式轉(zhuǎn)二進(jìn)制
先讀取為PIL格式,再轉(zhuǎn)為二進(jìn)制
import io
import base64
from PIL import Image
def image2byte(image):
'''
圖片轉(zhuǎn)byte
image: 必須是PIL格式
image_bytes: 二進(jìn)制
'''
# 創(chuàng)建一個字節(jié)流管道
img_bytes = io.BytesIO()
# 將圖片數(shù)據(jù)存入字節(jié)流管道, format可以按照具體文件的格式填寫
image.save(img_bytes, format="JPEG")
# 從字節(jié)流管道中獲取二進(jìn)制
image_bytes = img_bytes.getvalue()
return image_bytes
def byte2image(byte_data):
'''
byte轉(zhuǎn)為圖片
byte_data: 二進(jìn)制
'''
image = Image.open(io.BytesIO(byte_data))
return image
調(diào)用代碼
image_path = "img/3.jpg" image = Image.open(image_path) byte_data = image2byte(image) image2 = byte2image(byte_data)
數(shù)組轉(zhuǎn)二進(jìn)制
先用opencv讀取為數(shù)組格式,再轉(zhuǎn)為二進(jìn)制
def numpy2byte(image):
'''
數(shù)組轉(zhuǎn)二進(jìn)制
image : numpy矩陣/cv格式圖片
byte_data:二進(jìn)制數(shù)據(jù)
'''
#對數(shù)組的圖片格式進(jìn)行編碼
success,encoded_image = cv2.imencode(".jpg",image)
#將數(shù)組轉(zhuǎn)為bytes
byte_data = encoded_image.tobytes()
return byte_data
def byte2numpy(byte_data):
'''
byte轉(zhuǎn)numpy矩陣/cv格式
byte_data:二進(jìn)制數(shù)據(jù)
image : numpy矩陣/cv格式圖片
'''
image = np.asarray(bytearray(byte_data), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
return image
調(diào)用代碼
image_path = "img/3.jpg" image = cv2.imread(image_path) byte_data = numpy2byte(image) image2 = byte2numpy(byte_data)
圖片轉(zhuǎn)二進(jìn)制
直接讀取圖片為二進(jìn)制
import cv2
import numpy as np
def read2byte(path):
'''
圖片轉(zhuǎn)二進(jìn)制
path:圖片路徑
byte_data:二進(jìn)制數(shù)據(jù)
'''
with open(path,"rb") as f:
byte_data = f.read()
return byte_data
def byte2numpy(byte_data):
'''
byte轉(zhuǎn)numpy矩陣/cv格式
byte_data:二進(jìn)制數(shù)據(jù)
image : numpy矩陣/cv格式圖片
'''
image = np.asarray(bytearray(byte_data), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
return image
調(diào)用代碼
image_path = "img/3.jpg" byte_data = read2byte(image_path) image = byte2numpy(byte_data)
延伸
有時候數(shù)據(jù)傳輸規(guī)定轉(zhuǎn)換為base64格式,可以參考我的另一篇文章 圖片和base64轉(zhuǎn)換的三種方式
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
OpenCV機(jī)器學(xué)習(xí)MeanShift算法筆記分享
這篇文章主要介紹了OpenCV機(jī)器學(xué)習(xí)MeanShift算法筆記分享,有需要的朋友可以借鑒參考下,希望可以對各位讀者的OpenCV算法學(xué)習(xí)能夠有所幫助2021-09-09
python將秒數(shù)轉(zhuǎn)化為時間格式的實(shí)例
今天小編就為大家分享一篇python將秒數(shù)轉(zhuǎn)化為時間格式的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09
Visual Studio code 配置Python開發(fā)環(huán)境
這篇文章主要介紹了Visual Studio code 配置Python開發(fā)環(huán)境,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)將序列分解為單獨(dú)變量的方法
這篇文章主要介紹了Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)將序列分解為單獨(dú)變量的方法,結(jié)合實(shí)例形式分析了Python序列賦值實(shí)現(xiàn)的分解成單獨(dú)變量功能相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
Django ORM判斷查詢結(jié)果是否為空,判斷django中的orm為空實(shí)例
這篇文章主要介紹了Django ORM判斷查詢結(jié)果是否為空,判斷django中的orm為空實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
基于Python開發(fā)高效文件搜索與內(nèi)容匹配工具
在日常的開發(fā)和辦公中,查找和篩選特定文件或文件內(nèi)容的需求十分常見,本文將基于PyQt6開發(fā)一個文件搜索工具,有需要的小伙伴可以參考一下2025-03-03

