網站首頁 編程語言 正文
函數使用
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.圖像轉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.圖像轉二進制編碼
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)# 二進制數據
3.圖像保存成二進制文件并讀取二進制
# python+OpenCV讀取圖像并轉換為二進制格式文件的代碼 # coding=utf-8 ''' Created on 2016年3月24日 使用Opencv讀取圖像將其保存為二進制格式文件,再讀取該二進制文件,轉換為圖像進行顯示 @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] # 把圖像轉換為二進制文件 # python寫二進制文件,f = open('name','wb') # 只有wb才是寫二進制文件 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() # 把二進制轉換為圖像并顯示 # python讀取二進制文件,用rb # f.read(n)中n是需要讀取的字節數,讀取后需要進行解碼,使用struct.unpack("B",fileReader.read(1))函數 # 其中“B”為無符號整數,占一個字節,“b”為有符號整數,占1個字節 # “c”為char類型,占一個字節 # “i”為int類型,占四個字節,I為有符號整形,占4個字節 # “h”、“H”為short類型,占四個字節,分別對應有符號、無符號 # “l”、“L”為long類型,占四個字節,分別對應有符號、無符號 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.二進制轉圖像
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轉圖像
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.互轉
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.二進制轉base64
def binary_base64(binary): img_stream = base64.b64encode(binary) bs64 = img_stream.decode('utf-8') print(bs64)
8.base64轉二進制
import base64 bs64 = "" img_data = base64.b64decode(bs64) print(img_data)
原文鏈接:https://blog.csdn.net/Vertira/article/details/123844551
相關推薦
- 2022-01-17 自動化實戰之Cypress(一):環境搭建
- 2023-04-24 React組件與事件的創建使用教程_React
- 2022-07-24 C語言超詳細i講解雙向鏈表_C 語言
- 2022-10-31 React中使用antd組件的方法_React
- 2022-04-11 android?ViewPager實現一個無限輪播圖_Android
- 2023-02-12 一文帶你了解Golang中reflect反射的常見錯誤_Golang
- 2022-01-27 關于httpclient中多次執行execute阻塞問題,卡住不動了解決方式。
- 2023-07-07 Aop基于xml和注解應用
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支