当前位置:网站首页>(10) Sequence and deserialization of image data
(10) Sequence and deserialization of image data
2022-08-10 19:57:00 【Heng Youcheng】
欢迎访问个人网络日志知行空间
文章目录
1.Pythoninput and output streams
Python的io
模块提供了Python
The main functions in processing various types of input and output,Python
The main ones dealt with are3
种类型的io
,分别是text I/O
,binary I/O
和raw I/O
.All three types of concrete objects are文件对象
,有时也被称为流
, 类文件对象
.Various stream objects handle different file types.例如,往binary
Write to the stream objectstr
An object of type will report a type errorTypeError
,同样往text
Write to the stream objectbinary
Type of content will also report an error.
text
Stream objects are mainly dealt withstr
Type file read and write,When the file is stored in bytes,通过text
Stream objects can make file codecs and cross-platform support such as newlines insensitive.最简单的创建text
The way to stream objects is throughopen
函数,如
import io
f = open('text.txt', 'r', encoding='utf8')
# A stream of bytes in memory can be passed throughio.StringIO来创建
f = io.StringIO("some text")
binary I/O
Used to handle bytes-like objects,并返回一个字节对象bytes object
,Python
中的bytes
类和str
差不多,Just add the quote before the definitionb
,如bytes_data = b'data'
,bytes
对象只支持ASCII
码字符,Any binary code exceeds127
的字符在bytes
object must start with 转移序列
来表示.类字节对象bytes-like object
是支持缓存协议
Buffer ProtocolAnd can output oneC
An object of the language contiguous cache,binary I/O
The file is not automatically encoded, decoded and converted to newlines.binary I/O
When working with non-text data and or when you want to manually control the processing of text data.创建binary I/O
The easiest way to object is:
f = open("myfile.jpg", "rb")
# 从内存中读取
f = io.BytesIO(b"some initial binary data: \x00\x01")
2.png
文件格式简介
png
图片格式是Portable Network Graphics(P ortable N etwork G raphics,PNG
)It is a bitmap graphics format that supports lossless compression,支援索引、灰度、RGB三种颜色方案以及Alpha通道等特性.PNG的开发目标是改善并取代GIF作为适合网络传输的格式而不需专利许可,PNG于1997年3月作为知识性RFC 2083发布,于2004年作为ISO/IEC标准发布2.
png
文件中,You can see it after reading byte by byte,开头的8A bytecode is always a hexadecimal number89 50 4E 47 0D 0A 1A 0A
,这正是png
The signature of the format file,是用来标识png
格式的,So just modify the suffix name of the file,The software is still able to know what the picture ispng
格式的.下面是通过python
的builtin.open
函数按bytes
对象读取png
The output section you get after the file,
p = "sample.png"
with open(p,'rb') as f:
img_data = f.read()
print(img_data)
# b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x0...'
可以看到前8个字节分别是\x89PNG\r\n\x1a\n
,十六进制数0x50
,0x4e
,0x47
,0x0d
,oxoa
分别对应的ASCII
code is exactlyP
,N
,G
,CR
,LF
3.
png
There are two types of data blocks defined in ,一种是PNGFile must contain、Read and write software must also support key blocks(critical chunk
);另一种叫做辅助块(ancillary chunks
),PNG允许软件忽略它不认识的附加块.
关键数据块中有4个标准数据块:
File header data blockIHDR(header chunk):包含有图像基本信息,such as width,作为第一个数据块出现并只出现一次,Its length is fixed13个字节:
描述
长度
图片宽度
4字节
图片高度
4字节
图像深度
1字节
颜色类型
1字节
压缩方法
1字节
过滤方式
1字节
扫描方式
1字节
As in the open image above
IHDR\x00\x00\x00\xb6\x00\x00\x00\x86
It can be seen that the image width and height are\x00\x00\x00\xb6
即182和\x00\x00\x00\x86
即134.Palette data blockPLTE(palette chunk):必须放在图像数据块之前.
图像数据块IDAT(image data chunk):Stores actual image data.PNG数据允许包含多个连续的图像数据块.
图像结束数据IEND(image trailer chunk):at the end of the file,表示PNG数据流结束.
辅助数据块:常见的有:
Data block identifier | Data block meaning | Data block location restrictions |
---|---|---|
cHRM | 基色和白色点数据块 | 放在PLTE和IDAT之前 |
gAMA | 图像γ数据块 | 放在PLTE和IDAT之前 |
pHYs | 物理像素尺寸数据块 | 放在IDAT之前 |
tIME | 图像最后修改时间数据块 | 无限制 |
tEXt | File basic message data block | 无限制 |
3.PILThe library parses image data
PILThe library parses image data4
PIL.Image.open(fp, mode='r', formats=None)
fp
The parameter supports image file paths,如string/Path/文件对象
.对于普通的png
文件,可以通过PIL.Image.open(filename)
function to read directly,
from PIL import Image
p = "sample.png"
# 1.read from data path
img = Image.open(p)
# 获取图像的exif信息
print(img.getexif())
# 2.read from file object
with open(p) as f:
img_b = io.BytesIO(f.read())
img = Image.open(img_b)
PIL.Image.frombuffer(mode, size, data, decoder_name='raw', *args)
Creates an image based on a byte buffer,值得注意的是frombuffer
Parse pixel data only,So only from pixel data(同png
中IDAT
部分)restore the image from the byte cache,不能直接使用png
中读取的数据,Because it contains auxiliary data, etc.可以解析Image.tobytes
或numpy.tobytes(order='c'))
Generate the result to construct the image. 同样还有Image.frombytes
函数.
import numpy as np
with open(p) as f:
img_b = io.BytesIO(f.read())
img = Image.open(img_b)
# 1.from Image.tobytes
img_bytes = img.tobytes()
img = Image.frombuffer('RGB', (182, 134), img_bytes)
# 2.from numpy.tobytes
fb = io.BytesIO()
fb.write(img_bytes)
img_bytes_buffer = fb.getbuffer()
img_bytes = np.frombuffer(img_bytes_buffer,dtype=np.uint8).reshape(182, 134, -1)
img_bytes = img_bytes.tobytes(order='C')img = Image.frombuffer('RGB', (182, 134), img_bytes)
4.opencv解析图像
同样,使用OpenCV
读取图像,可通过imread
,也可以使用decode
Methods are parsed from memory data,譬如png
格式的文件,可以先通过builtin.open
读取成字节流,再使用np.frombuffer(bytes, dtype=uint8)
Used after constructing the cached data of the imageimdecode
method to obtain image data.
from PIL import Image
p = "sample.png"
with open(p, 'rb') as f:
img_bytes_data = f.read()
img_buffer = np.frombuffer(img_bytes_data, dtype=np.uint8)
img = cv2.imdecode(img_buffer, cv2.IMREAD_COLOR)
cv2.cvtColor(img, cv2.COLOR_RGB2BGR, img)
plt.imshow(img)
Of course this is just an example,正常cv2.imdecode
Certainly not used in this way,通常与cv2.imencode
结合使用,For the transfer of image data.
5.labelme
标注文件中的img_data
数据解析
import base64
import cv2
import numpy as np
from PIL import Image
from matplotlib import pyplot as plt
json_path = "sample.json"
data = json.load(open(json_path))
data = data["imageData"]
bytes_data = base64.b64decode(data)
# 1.use Image to reconstruct image
bd = io.BytesIO(bytes_data)
img = Image.open(bd)
# 2.use cv2.imdecode to reconstruct image
img_data = np.frombuffer(bytes_data, dtype=np.uint8)
img = cv2.imdecode(img_data, cv2.IMREAD_COLOR)
cv2.cvtColor(img, cv2.COLOR_RGB2BGR, img)
plt.imshow(img)
Image data is saved tojson
文件中时,为了可视化的方便,Typically used for image database64
进行了编码,因此解析json
中的img_data
data to be used firstbase64
Decode the bytecode of the image data,再使用numpy.frombuffer(bytes, dtype=uint8)
方法创建cv2.imdecode
The memory block data that can be parsed,cv2.imdecode
The full image data is parsed instead of just pixel data,Include information such as the width and height of the image and the creation date,So no additional information is needed to restore the image.base64
模块支持Base16/Base32/Base64/Base85
编解码方法5,Its role is mainly to encode binary data into printableASCII
码,以Base16
算法为例,Base16
Encoding will slice the bytes into 4个一组,So this encoding will be used later2个字符,The data will become the original2倍.类推,Base32
Encoding will slice the bytes into 5 个一组,每 5 bytes can be reassembled into 8个字符.
欢迎访问个人网络日志知行空间
参考资料
边栏推荐
猜你喜欢
Metasploit——渗透攻击模块(Exploit)
Modern Privacy-Preserving Record Linkage Techniques: An Overview论文总结
你不知道的浏览器页面渲染机制
Rider调试ASP.NET Core时报thread not gc-safe的解决方法
spark学习笔记(九)——sparkSQL核心编程-DataFrame/DataSet/DF、DS、RDD三者之间的转换关系
【初学必备】3d游戏建模入门基础知识
越折腾越好用的 3 款开源 APP
QoS Quality of Service Seven Switch Congestion Management
30分钟使用百度EasyDL实现健康码/行程码智能识别
Redis persistence mechanism
随机推荐
Modern Privacy-Preserving Record Linkage Techniques: An Overview论文总结
【毕业设计】基于STM32的天气预报盒子 - 嵌入式 单片机 物联网
365天挑战LeetCode1000题——Day 053 求解方程 解析 模拟
【luogu CF1534F2】Falling Sand (Hard Version)(性质)(dfs)(线段树 / 单调队列 / 贪心)
含有PEG 间隔基和一个末端伯胺基团(CAS:1006592-62-6)化学试剂
[教你做小游戏] 只用几行原生JS,写一个函数,播放音效、播放BGM、切换BGM
Common ports and services
Public Key Retrieval is not allowed(不允许公钥检索)【解决办法】
端口探测详解
Introduction to 3 d games beginners essential 】 【 modeling knowledge
测试/开发程序员值这么多钱么?“我“不会愿赌服输......
机器学习|模型评估——AUC
[教你做小游戏] 斗地主的手牌,如何布局?看25万粉游戏区UP主怎么说
大家要的Biotin-PEG3-Br/acid/NHS ester/alcohol/amine合集分享
铱钌合金/氧化铱仿生纳米酶|钯纳米酶|GMP-Pd纳米酶|金钯复合纳米酶|三元金属Pd-M-Ir纳米酶|中空金铂合金纳米笼核-多空二氧化硅壳纳米酶
Redis 持久化机制
魔方电池如何“躺赢”?解锁荣威iMAX8 EV“头等舱”安全密码
Optimization is a habit The starting point is to 'stand close to the critical'
云渲染的应用正在扩大,越来越多的行业需要可视化服务
《分布式微服务电商》专题(一)-项目简介