网站首页 > 技术文章 正文
视频:12.图像编码解码
# cv2.imdecode()函数
# 从指定的内存缓存中读取数据,并把数据转换(解码)成图像格式;主要用于从网络传输数据中恢复出图像。
# cv2.imencode()函数
# 将图片格式转换(编码)成流数据,赋值到内存缓存中;主要用于图像数据格式的压缩,方便网络传输。
#imdecode() 从网络读取图像数据 并转换成图片格式
import numpy as np
import urllib.request
import ssl
import cv2
# context=ssl._create_unverified_context()
# url='https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png'
# resp=urllib.request.urlopen(url,context=context)
# #print(resp)
# image=np.array(bytearray(resp.read()))
# image=cv2.imdecode(image,cv2.IMREAD_COLOR)
# print(image.shape)
# cv2.imshow("img",image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
#imencode() 把图片编码保存到本地
img=cv2.imread("img/bike.jpg")
print(img.shape)
img_encode=cv2.imencode(".jpg",img)[1]
#print(img_encode)
byte_encode=img_encode.tobytes()
# with open("img_encode.bin","wb") as f:
# f.write(byte_encode)
# f.flush()
with open("img_encode.bin","rb") as f:
byte_encode=f.read()
nparry=np.frombuffer(byte_encode,np.uint8)
img_decode=cv2.imdecode(nparry,cv2.IMREAD_COLOR)
print(img_decode.shape)
cv2.imshow("img",img_decode)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,
# Base64就是一种基于64个可打印字符(包括a-z、A-Z、0-9、/、+)来表示二进制数据的方法。
# Base64编码是从二进制到字符的过程,可用于在HTTP环境下传递较长的标识信息。
import base64
import cv2
import numpy as np
img=cv2.imread("img/bike.jpg")
print(img.shape)
data=cv2.imencode(".jpg",img)[1]
data=data.tobytes()
cv2tobase64=base64.b64encode(data)
#print(cv2tobase64,type(cv2tobase64))
res=cv2tobase64.decode("utf8")
#print(res,type(res))
imgdata=base64.b64decode(res)
#print(imgdata,type(imgdata))
nparr=np.frombuffer(imgdata,np.uint8)
img_decode=cv2.imdecode(nparr,cv2.IMREAD_COLOR)
#print(img_decode)
cv2.imshow("img",img_decode)
cv2.waitKey(0)
cv2.destroyAllWindows()
猜你喜欢
- 2025-07-05 黑客如何在被黑机器上检测 shellcode
- 2025-07-05 别再把 JWT 存在 localStorage 里了!2025 年前端鉴权新思路
- 2025-07-05 H.265已落后!下一代视频技术实现重大突破
- 2025-07-05 kubernetes基础知识之secret加密
- 2025-07-05 三十七,Web渗透提高班之hack the box在线靶场注册及入门知识
- 2025-07-05 Python使用Base64进行图片编码和解码
- 最近发表
- 标签列表
-
- axure 注册码 (25)
- mutex_lock (30)
- oracleclient (27)
- nfs (25)
- springbatch (28)
- oracle数据库备份 (25)
- dir (26)
- connectionstring属性尚未初始化 (23)
- output (32)
- panel滚动条 (28)
- centos 5 4 (23)
- sql学习 (33)
- c 数组 (33)
- pascal语言教程 (23)
- ppt 教程 (35)
- java7 (24)
- 自适应网站制作 (32)
- server服务自动停止 (25)
- 超链接去掉下划线 (34)
- 什么是堆栈 (22)
- map entry (25)
- ubuntu装qq (25)
- outputstreamwriter (26)
- fill_parent (22)
- mssqlserver jar (30)