python PIL/cv2/base64相互轉(zhuǎn)換實(shí)例
PIL和cv2是python中兩個(gè)常用的圖像處理庫(kù),PIL一般是anaconda自帶的,cv2是opencv的python版本。base64在網(wǎng)絡(luò)傳輸圖片的時(shí)候經(jīng)常用到。
##PIL讀取、保存圖片方法
from PIL import Image
img = Image.open(img_path)
img.save(img_path2)
##cv2讀取、保存圖片方法
import cv2
img = cv2.imread(img_path)
cv2.imwrite(img_path2, img)
##圖片文件打開為base64
import base64
def img_base64(img_path):
with open(img_path,"rb") as f:
base64_str = base64.b64encode(f.read())
return base64_str
1、PIL和cv2轉(zhuǎn)換
##PIL轉(zhuǎn)cv2 import cv2 from PIL import Image import numpy as np def pil_cv2(img_path): image = Image.open(img_path) img = cv2.cvtColor(np.asarray(image),cv2.COLOR_RGB2BGR) return img ##cv2轉(zhuǎn)PIL import cv2 from PIL import Image def cv2_pil(img_path): image = cv2.imread(img_path) image = Image.fromarray(cv2.cvtColor(image,cv2.COLOR_BGR2RGB)) return image
2、PIL和base64轉(zhuǎn)換
##PIL轉(zhuǎn)base64 import base64 from io import BytesIO def pil_base64(image): img_buffer = BytesIO() image.save(img_buffer, format='JPEG') byte_data = img_buffer.getvalue() base64_str = base64.b64encode(byte_data) return base64_str ##base64轉(zhuǎn)PIL import base64 from io import BytesIO from PIL import Image def base64_pil(base64_str): image = base64.b64decode(base64_str) image = BytesIO(image) image = Image.open(image) return image
3、cv2和base64轉(zhuǎn)換
##cv2轉(zhuǎn)base64
import cv2
def cv2_base64(image):
base64_str = cv2.imencode('.jpg',image)[1].tostring()
base64_str = base64.b64encode(base64_str)
return base64_str
##base64轉(zhuǎn)cv2
import base64
import numpy as np
import cv2
def base64_cv2(base64_str):
imgString = base64.b64decode(base64_str)
nparr = np.fromstring(imgString,np.uint8)
image = cv2.imdecode(nparr,cv2.IMREAD_COLOR)
return image
以上這篇python PIL/cv2/base64相互轉(zhuǎn)換實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Python常用base64 md5 aes des crc32加密解密方法匯總
- python將圖片轉(zhuǎn)base64,實(shí)現(xiàn)前端顯示
- Python 解碼Base64 得到碼流格式文本實(shí)例
- Python 實(shí)現(xiàn)opencv所使用的圖片格式與 base64 轉(zhuǎn)換
- python base64庫(kù)給用戶名或密碼加密的流程
- Python中base64與xml取值結(jié)合問(wèn)題
- python3 常見(jiàn)解密加密算法實(shí)例分析【base64、MD5等】
- Python3內(nèi)置模塊之base64編解碼方法詳解
- Python 利用base64庫(kù) 解碼本地txt文本字符串
相關(guān)文章
Python實(shí)現(xiàn)統(tǒng)計(jì)給定字符串中重復(fù)模式最高子串功能示例
這篇文章主要介紹了Python實(shí)現(xiàn)統(tǒng)計(jì)給定字符串中重復(fù)模式最高子串功能,涉及Python針對(duì)字符串的遍歷、排序、切片、運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
K最近鄰算法(KNN)---sklearn+python實(shí)現(xiàn)方式
今天小編就為大家分享一篇K最近鄰算法(KNN)---sklearn+python實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
Python實(shí)現(xiàn)簡(jiǎn)單猜數(shù)字游戲
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-02-02
Python中實(shí)現(xiàn)地圖可視化的方法小結(jié)
Python提供了多個(gè)強(qiáng)大的庫(kù),如Folium、Matplotlib、Geopandas等,使得創(chuàng)建漂亮而具有信息量的地圖變得簡(jiǎn)單而靈活,本文將詳細(xì)介紹如何使用這些庫(kù)繪制漂亮的地圖,感興趣的可以了解下2023-12-12
Django實(shí)現(xiàn)學(xué)員管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Django實(shí)現(xiàn)學(xué)員管理系統(tǒng),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
如何在windows下安裝Pycham2020軟件(方法步驟詳解)
這篇文章主要介紹了在windows下安裝Pycham2020軟件方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05

