最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python實現(xiàn)圖像的二進制與base64互轉(zhuǎn)

 更新時間:2022年03月30日 14:26:14   作者:Vertira  
這篇文章主要為大家介紹了如何在Python中使用OpenCV從而實現(xiàn)圖像轉(zhuǎn)base64編碼、圖像轉(zhuǎn)二進制編碼、二進制轉(zhuǎn)圖像等功能,感興趣的可以跟上小編一起學(xué)習(xí)一下

函數(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)二進制編碼

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)# 二進制數(shù)據(jù)

3.圖像保存成二進制文件并讀取二進制

#   python+OpenCV讀取圖像并轉(zhuǎn)換為二進制格式文件的代碼
 
# coding=utf-8
'''
Created on 2016年3月24日
使用Opencv讀取圖像將其保存為二進制格式文件,再讀取該二進制文件,轉(zhuǎ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)換為二進制文件
# 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()
 
# 把二進制轉(zhuǎn)換為圖像并顯示
# python讀取二進制文件,用rb
# f.read(n)中n是需要讀取的字節(jié)數(shù),讀取后需要進行解碼,使用struct.unpack("B",fileReader.read(1))函數(shù)
# 其中“B”為無符號整數(shù),占一個字節(jié),“b”為有符號整數(shù),占1個字節(jié)
# “c”為char類型,占一個字節(jié)
# “i”為int類型,占四個字節(jié),I為有符號整形,占4個字節(jié)
# “h”、“H”為short類型,占四個字節(jié),分別對應(yīng)有符號、無符號
# “l(fā)”、“L”為long類型,占四個字節(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.二進制轉(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.二進制轉(zhuǎn)base64

def binary_base64(binary):
    img_stream = base64.b64encode(binary)
    bs64 = img_stream.decode('utf-8')
    print(bs64)

8.base64轉(zhuǎn)二進制

import base64
 
bs64 = ""
img_data = base64.b64decode(bs64)
print(img_data)

以上就是Python實現(xiàn)圖像的二進制與base64互轉(zhuǎn)的詳細內(nèi)容,更多關(guān)于Python圖像二進制轉(zhuǎn)base64的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Django自定義用戶表+自定義admin后臺中的字段實例

    Django自定義用戶表+自定義admin后臺中的字段實例

    今天小編就為大家分享一篇Django自定義用戶表+自定義admin后臺中的字段實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • Python獲取單個程序CPU使用情況趨勢圖

    Python獲取單個程序CPU使用情況趨勢圖

    這篇文章主要介紹了Python獲取單個程序CPU使用情況趨勢圖,本文使用matplotlib將數(shù)據(jù)可視化,需要的朋友可以參考下
    2015-03-03
  • 一起解密Python中的*args和**kwargs無限可能的函數(shù)參數(shù)

    一起解密Python中的*args和**kwargs無限可能的函數(shù)參數(shù)

    這篇文章主要來跟大家一起解密Python中的*args和**kwargs無限可能的函數(shù)參數(shù)使用的靈活性,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06
  • Python實現(xiàn)文件內(nèi)容批量追加的方法示例

    Python實現(xiàn)文件內(nèi)容批量追加的方法示例

    這篇文章主要介紹了Python實現(xiàn)文件內(nèi)容批量追加的方法,結(jié)合實例形式分析了Python文件的讀寫相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • Python基礎(chǔ)教程之Turtle繪制圖形詳解

    Python基礎(chǔ)教程之Turtle繪制圖形詳解

    在Python中,繪圖是一個非常有趣的領(lǐng)域,其中比較流行的繪圖庫就有?Turtle,所以本文就來講講如何在Python中使用它來創(chuàng)建和修改圖形,需要的可以參考一下
    2023-06-06
  • 對python 判斷數(shù)字是否小于0的方法詳解

    對python 判斷數(shù)字是否小于0的方法詳解

    今天小編就為大家分享一篇對python 判斷數(shù)字是否小于0的方法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • 原理解析為什么pydantic可變對象沒有隨著修改而變化

    原理解析為什么pydantic可變對象沒有隨著修改而變化

    這篇文章主要介紹了為什么pydantic可變對象沒有隨著修改而變化的原因解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • python循環(huán)某一特定列的所有行數(shù)據(jù)(方法示例)

    python循環(huán)某一特定列的所有行數(shù)據(jù)(方法示例)

    在Python中,處理表格數(shù)據(jù)(比如CSV文件、Excel文件等)時,我們通常會使用pandas庫,因為它提供了豐富的數(shù)據(jù)結(jié)構(gòu)和數(shù)據(jù)分析工具,下面,我將以處理CSV文件中的某一特定列的所有行數(shù)據(jù)為例,給出詳細、完整的代碼示例,感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • python中使用多線程改進flask案例

    python中使用多線程改進flask案例

    這篇文章主要介紹了使用多線程改進flask案例,線程是指進程內(nèi)的一個執(zhí)行單元,也是進程內(nèi)的可調(diào)度實體.線程的劃分尺度小于進程,使得多線程程序的并發(fā)性高,更多具體內(nèi)容,需要的小伙伴可以參考下面文章相關(guān)資料,希望對你有所幫助
    2022-03-03
  • Python爬蟲實現(xiàn)使用beautifulSoup4爬取名言網(wǎng)功能案例

    Python爬蟲實現(xiàn)使用beautifulSoup4爬取名言網(wǎng)功能案例

    這篇文章主要介紹了Python爬蟲實現(xiàn)使用beautifulSoup4爬取名言網(wǎng)功能,結(jié)合實例形式分析了Python基于beautifulSoup4模塊爬取名言網(wǎng)并存入MySQL數(shù)據(jù)庫相關(guān)操作技巧,需要的朋友可以參考下
    2019-09-09

最新評論

安宁市| 合江县| 枝江市| 萝北县| 洞头县| 武威市| 青岛市| 天水市| 资源县| 娄烦县| 潢川县| 山西省| 新宁县| 中宁县| 喀喇沁旗| 广河县| 明溪县| 荔波县| 阜平县| 黄冈市| 永靖县| 嵩明县| 招远市| 平江县| 连云港市| 崇义县| 诸暨市| 信丰县| 吉木乃县| 安国市| 名山县| 吴川市| 沅陵县| 阳高县| 宜都市| 永登县| 扶绥县| 双城市| 桂东县| 大方县| 涞源县|