Python實(shí)現(xiàn)圖像的二進(jìn)制與base64互轉(zhuǎn)
函數(shù)使用
def base64_to_image(base64_code): img_data = base64.b64decode(base64_code) img_array = numpy.fromstring(img_data, numpy.uint8) # img_array = np.frombuffer(image_bytes, dtype=np.uint8) #可選 image_base64_dec = cv2.imdecode(img_array, cv2.COLOR_RGB2BGR) return image_base64_dec def image_to_base64(full_path): with open(full_path, "rb") as f: data = f.read() image_base64_enc = base64.b64encode(data) image_base64_enc = str(image_base64_enc, 'utf-8') return image_base64_enc #傳base64 img_bytes = request.json["img_stream"] img_cv = base64_to_image(img_bytes) uuid_str = str(uuid.uuid1()) img_path = uuid_str +".jpg" cv2.imwrite(img_path,img_cv)
1.圖像轉(zhuǎn)base64編碼
import cv2 import base64 def cv2_base64(image): img = cv2.imread(image) binary_str = cv2.imencode('.jpg', img)[1].tostring()#編碼 base64_str = base64.b64encode(binary_str)#解碼 base64_str = base64_str.decode('utf-8') myjson={"bs64":cv2_base64("1.jpg")} print(myjson) return base64_str
2.圖像轉(zhuǎn)二進(jìn)制編碼
import cv2 import base64 def cv2_binary(image): img = cv2.imread(image) binary_str = cv2.imencode('.jpg', img)[1].tostring()#編碼 print(binary_str) # base64_str = base64.b64encode(binary_str)#解碼 # base64_str = base64_str.decode('utf-8') # print(base64_str) return binary_str cv2_binary("1.jpg") # 或者 image_file =r"1.jpg" image_bytes = open(image_file, "rb").read() print(image_bytes)# 二進(jìn)制數(shù)據(jù)
3.圖像保存成二進(jìn)制文件并讀取二進(jìn)制
# python+OpenCV讀取圖像并轉(zhuǎn)換為二進(jìn)制格式文件的代碼 # coding=utf-8 ''' Created on 2016年3月24日 使用Opencv讀取圖像將其保存為二進(jìn)制格式文件,再讀取該二進(jìn)制文件,轉(zhuǎn)換為圖像進(jìn)行顯示 @author: hanchao ''' import cv2 import numpy as np import struct image = cv2.imread("1.jpg") # imageClone = np.zeros((image.shape[0],image.shape[1],1),np.uint8) # image.shape[0]為rows # image.shape[1]為cols # image.shape[2]為channels # image.shape = (480,640,3) rows = image.shape[0] cols = image.shape[1] channels = image.shape[2] # 把圖像轉(zhuǎn)換為二進(jìn)制文件 # python寫二進(jìn)制文件,f = open('name','wb') # 只有wb才是寫二進(jìn)制文件 fileSave = open('patch.bin', 'wb') for step in range(0, rows): for step2 in range(0, cols): fileSave.write(image[step, step2, 2]) for step in range(0, rows): for step2 in range(0, cols): fileSave.write(image[step, step2, 1]) for step in range(0, rows): for step2 in range(0, cols): fileSave.write(image[step, step2, 0]) fileSave.close() # 把二進(jìn)制轉(zhuǎn)換為圖像并顯示 # python讀取二進(jìn)制文件,用rb # f.read(n)中n是需要讀取的字節(jié)數(shù),讀取后需要進(jìn)行解碼,使用struct.unpack("B",fileReader.read(1))函數(shù) # 其中“B”為無符號整數(shù),占一個(gè)字節(jié),“b”為有符號整數(shù),占1個(gè)字節(jié) # “c”為char類型,占一個(gè)字節(jié) # “i”為int類型,占四個(gè)字節(jié),I為有符號整形,占4個(gè)字節(jié) # “h”、“H”為short類型,占四個(gè)字節(jié),分別對應(yīng)有符號、無符號 # “l(fā)”、“L”為long類型,占四個(gè)字節(jié),分別對應(yīng)有符號、無符號 fileReader = open('patch.bin', 'rb') imageRead = np.zeros(image.shape, np.uint8) for step in range(0, rows): for step2 in range(0, cols): a = struct.unpack("B", fileReader.read(1)) imageRead[step, step2, 2] = a[0] for step in range(0, rows): for step2 in range(0, cols): a = struct.unpack("b", fileReader.read(1)) imageRead[step, step2, 1] = a[0] for step in range(0, rows): for step2 in range(0, cols): a = struct.unpack("b", fileReader.read(1)) imageRead[step, step2, 0] = a[0] fileReader.close() cv2.imshow("source", image) cv2.imshow("read", imageRead) cv2.imwrite("2.jpg",imageRead) cv2.waitKey(0)
4.二進(jìn)制轉(zhuǎn)圖像
def binary_cv2(bytes): file = open("4.jpg","wb") file.write(bytes) binary_cv2("bytes") #或者 from PIL import Image import io img = Image.open(io.BytesIO("bytes")) img.save("5.jpg")
5.base64轉(zhuǎn)圖像
def base64_cv2(base64code): img_data = base64.b64decode(base64code) file = open("2.jpg","wb") file.write(img_data) file.close() base64_cv2("base64code") ============================================ with open("1.txt","r") as f: img_data = base64.b64decode(f.read()) file = open("3.jpg","wb") file.write(img_data) file.close()
6.互轉(zhuǎn)
def base64_to_image(base64_code): img_data = base64.b64decode(base64_code) img_array = numpy.fromstring(img_data, numpy.uint8) image_base64_dec = cv2.imdecode(img_array, cv2.COLOR_RGB2BGR) return image_base64_dec #圖像矩陣,需要cv2.imwrite寫入cv2.imwrite("1.jpg",img) def image_to_base64(full_path): with open(full_path, "rb") as f: data = f.read() image_base64_enc = base64.b64encode(data) image_base64_enc = str(image_base64_enc, 'utf-8') return image_base64_enc
7.二進(jìn)制轉(zhuǎn)base64
def binary_base64(binary): img_stream = base64.b64encode(binary) bs64 = img_stream.decode('utf-8') print(bs64)
8.base64轉(zhuǎn)二進(jìn)制
import base64 bs64 = "" img_data = base64.b64decode(bs64) print(img_data)
以上就是Python實(shí)現(xiàn)圖像的二進(jìn)制與base64互轉(zhuǎn)的詳細(xì)內(nèi)容,更多關(guān)于Python圖像二進(jìn)制轉(zhuǎn)base64的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python使用pickle模塊實(shí)現(xiàn)序列化功能示例
這篇文章主要介紹了Python使用pickle模塊實(shí)現(xiàn)序列化功能,結(jié)合實(shí)例形式分析了基于pickle模塊的序列化操作相關(guān)操作技巧,需要的朋友可以參考下2018-07-07python破解WiFi教程代碼,Python蹭網(wǎng)原理講解
用Python生成一個(gè)簡單的密碼本,一般是有數(shù)字、字母和符號組成,這里用到的思路主要是窮舉法。通過使用pywifi?模塊,根據(jù)密碼本暴力破解WiFi。本文只是從技術(shù)的角度來闡述學(xué)習(xí)Pywifi庫!并不建議大家做任何破壞性的操作和任何不當(dāng)?shù)男袨椋?/div> 2023-01-01python網(wǎng)絡(luò)編程學(xué)習(xí)筆記(七):HTML和XHTML解析(HTMLParser、BeautifulSoup)
在python中能夠進(jìn)行html和xhtml的庫有很多,如HTMLParser、sgmllib、htmllib、BeautifulSoup、mxTidy、uTidylib等,這里介紹一下HTMLParser、BeautifulSoup等模塊2014-06-06python數(shù)據(jù)處理 根據(jù)顏色對圖片進(jìn)行分類的方法
今天小編就為大家分享一篇python數(shù)據(jù)處理 根據(jù)顏色對圖片進(jìn)行分類的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12Python Tensor FLow簡單使用方法實(shí)例詳解
這篇文章主要介紹了Python Tensor FLow簡單使用方法,結(jié)合實(shí)例形式詳細(xì)分析了Tensor FLow相關(guān)概念、原理、用法與操作注意事項(xiàng),需要的朋友可以參考下2020-01-01基于Python實(shí)現(xiàn)股票數(shù)據(jù)分析的可視化
在購買股票的時(shí)候,可以使用歷史數(shù)據(jù)來對當(dāng)前的股票的走勢進(jìn)行預(yù)測,這就需要對股票的數(shù)據(jù)進(jìn)行獲取并且進(jìn)行一定的分析。本文將介紹如何通過Python實(shí)現(xiàn)股票數(shù)據(jù)分析的可視化,需要的可以參考一下2021-12-12深入解析Python中BeautifulSoup4的基礎(chǔ)知識與實(shí)戰(zhàn)應(yīng)用
BeautifulSoup4正是一款功能強(qiáng)大的解析器,能夠輕松解析HTML和XML文檔,本文將介紹BeautifulSoup4的基礎(chǔ)知識,并通過實(shí)際代碼示例進(jìn)行演示,感興趣的可以了解下2024-02-02最新評論