base64是网络上最常见的用于传输8bit字节码的编码方式之一,是一种基于64个可打印字符来表示二进制数据的方法。通过http传输图片常常将图片数据转换成base64之后再进行传输。
base64简介
base64是网络上最常见的用于传输8bit字节码的编码方式之一,base64就是一种基于64个可打印字符来表示二进制数据的方法。可查看rfc2045~rfc2049,上面有mime的详细规范。
base64编码是从二进制到字符的过程,可用于在http环境下传递较长的标识信息。例如,在java persistence系统hibernate中,就采用了base64来将一个较长的唯一标识符(一般为128-bit的uuid)编码为一个字符串,用作http表单和http get url中的参数。在其他应用程序中,也常常需要把二进制数据编码为适合放在url(包括隐藏表单域)中的形式。此时,采用base64编码具有不可读性,需要解码后才能阅读。
python中将图片转base64
1
2
3
4
5
6
7
|
# -*- coding: utf-8 -*-
import base64
with open ( "/home/chaowei/1.png" , "rb" ) as f:
# b64encode是编码,b64decode是解码
base64_data = base64.b64encode(f.read())
# base64.b64decode(base64data)
print (base64_data)
|
在前端html中调用base64编码
1
|
<img src = "data:image/jpg;base64,这里是base64的编码" / >
|
另外,有很多网站提供转换api,在线编码解码
base64转为图片
1
2
3
4
5
6
7
8
|
import os,base64
with open ( "c:\\users\\wonai\\desktop\\1.txt" , "r" ) as f:
#str = "ivborw0kggoaaaansuheugaaanwaaaaocaiaaaaaowpzaaaaaxnsr0iars4c6qaaaarnqu1baacxjwv8yquaaaajcehzcwaadsmaaa7dacdvqgqaaaqusurbvhhe7zptmomgdir7rh6o5+lpvewp01xugshaokgx+8z+7pkrtf6son7e/kmcnslw68wemkmf3ossehsnakhsliq0ifcsge4uijtdhyqgeuoy0j0chlk7knagqkkguloauhyxehpekmladwpcg8rhcrv/hkn3stigw4f88dyox89nobjmanuoc0emxphhcyx9+mowhghkmdlchm0bzzvzet6dssw7xjewk8hu+/o1x7zf1237/uu4t/o46v6szuarozb9kqbo7on4rjlykqcyynnajsbx3gmrj6wtzxirvla+90f82g+nm4fx3zoxgqykqrauu7b8fprdoeyjja7k5obyt1ywse4mxfdc3nrrprnqtqeumuuxourmcghdkfl/ots8melxu2mudo0bxuczl8efvgu0emsqjkgpm2h8y/cwgtw1c3el8ywxhhkwxgolapnj0vcrrw+ooikvcxf0o6yexwlqdanqymf1clhsi22d9hunxobcvzamabmio5bxrdrqot3m3ofuad4/hdolschx7avxzrijqtgsufmu6hb+hglnlc5d5kiwpcaqth7idk/lvld9z0rux4vywl2uj4wy6xbdl91ml57+ejsrnemnw/lcrkkln9nnkbulvksdabjm/zmbyh+pdwuuw6kdeyxpzesfzgarlng1m1enrcfglluuj5mvtg+uyxgzc+1+kn/dkdyutsvbqo7vnnagfkptrh9b8pqtgq/prcifdtaujaiww8aduyckllrcppkyczfkj5cylsznqtkmsyf58oyalmpg6jnlhylc9uxhidwvbr1ns8ahc9pgqlkkai3foorvuk4jgeytjigvtm+mncqrmsfogdj0mololhcmclk3m0kmpzef0mndgvb6ljqbmkb8p5grq34dstrcdpepp5mrnwrnocwsjk9i7nyqugzpytwuszuqe0qvucat5tgh9itmxedcdihjpccvagfi8uj4pgx3k3uhgberq9dtbjmjp1tnymskosh1ugqke23mxlrsri4yksuafnz5braugypw0/idsvhmxhjbei6lrezj0asuoc7tr8bondd9pnkco4lrny9cdgcexjqobdhqvsfpy7z7dsqhp9khxp9dznekbsr+iy3/n31tqvfye17xfuzktu507+4px4usfwbrm32lbzfyxphgrmtn3cwqqaef8a0urmhlajym8rc1iq2deoxvkudvjalmzromst8+4n+egm9rrwzl/dpavlddne9su36jyx6ectkuxufaumjozfwqsxldubntlyo/ckccnss112ydmkkgf/4xkl8rhndrowchbkmrv61qgfbwimepbrqglg105aovchdkcve4ty0chlknrly1qgfcwseep7zrgfc20zwrvihakcve49q2chdknrpy1qof/gdxihmwmc+csaaaaabjru5erkjggg=="
imgdata = base64.b64decode(f.read())
file = open ( '1.jpg' , 'wb' )
file .write(imgdata)
file .close()
|
python中将base64转为opencv的mat格式
1
2
3
4
5
6
7
|
import cv2
import base64
imgdata = base64.b64decode(base64_data)
nparr = np.fromstring(imgdata, np.uint8)
img_np = cv2.imdecode(nparr, cv2.imread_color)
#cv2.imshow("test",img_np)
#cv2.waitkey(0)
|
python中将opencv的mat格式转为base64
1
2
3
4
5
6
7
8
|
import cv2
import base64
imgdata = base64.b64decode(base64_data)
nparr = np.fromstring(imgdata, np.uint8)
img_np = cv2.imdecode(nparr, cv2.imread_color)
image = cv2.imencode( '.jpg' , img_np)[ 1 ]
base64_data = str (base64.b64encode(image))[ 2 : - 1 ])
|
到此这篇关于python opencv实现传统图片格式与base64转换的文章就介绍到这了,更多相关opencv传统图片格式与base64转换内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/pythonClub/p/10497994.html