优化文件头

This commit is contained in:
Luthics 2023-01-08 22:33:35 +08:00
parent 6676a8e882
commit 338b90f6f2
6 changed files with 15 additions and 11 deletions

View File

Before

Width:  |  Height:  |  Size: 66 B

After

Width:  |  Height:  |  Size: 66 B

BIN
imgs/1/2x1.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 B

BIN
imgs/1/2x3.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 70 B

View File

Before

Width:  |  Height:  |  Size: 78 B

After

Width:  |  Height:  |  Size: 78 B

View File

Before

Width:  |  Height:  |  Size: 66 B

After

Width:  |  Height:  |  Size: 66 B

26
main.py
View File

@ -8,10 +8,10 @@ new_height = int(input("请输入新高度(px)"))
print("模式 0 直接缩放")
print("模式 1 高斯模糊缩放")
mode = int(input("请输入模式:"))
# img = 'imgs/bw.bmp'
# img = 'imgs/1/2x3.bmp'
# new_width = 512
# new_height = 512
# mode = 1
# mode = 0
if (mode != 1):
mode = 0
@ -41,7 +41,7 @@ def sizeByte(size, length):
return b[::-1]
# 文件头处理
# 位图文件头
# 检验文件头是否为 BM
if (imgBytes[0] != 66 or imgBytes[1] != 77):
print("当前文件非 BMP 格式")
@ -51,14 +51,18 @@ fileSize = byteSize(2, 4) #文件头中的文件大小
dataStart = byteSize(10, 4) #文件头中的数据开始字节
# bmp 文件头
headerSize = byteSize(14, 4)
width = byteSize(18, 4)
height = byteSize(22, 4)
nbplan = byteSize(26, 2)
bpp = byteSize(28, 2)
compression = byteSize(30, 4)
imageSize = byteSize(34, 4)
# 调色板不做处理
headerSize = byteSize(14, 4) #该头结构的大小40字节
width = byteSize(18, 4) #位图宽度,单位为像素(有符号整数)
height = byteSize(22, 4) #位图高度,单位为像素(有符号整数)
nbplan = byteSize(26, 2) #色彩平面数只有1为有效值
bpp = byteSize(28, 2) #每个像素所占位数即图像的色深。典型值为1、4、8、16、24和32
compression = byteSize(30, 4) #所使用的压缩方法,可取值见下表。
imageSize = byteSize(34, 4) #图像大小。指原始位图数据的大小(详见后文),与文件大小不是同一个概念。
wppm = byteSize(38, 4) #图像的横向分辨率,单位为像素每米(有符号整数)
hppm = byteSize(42, 4) #图像的纵向分辨率,单位为像素每米(有符号整数)
colorsNum = byteSize(46, 4) #调色板的颜色数为0时表示颜色数为默认的2^色深个
icolorsNum = byteSize(50, 4) #重要颜色数为0时表示所有颜色都是重要的通常不使用本项
colorsBoard = imgBytes[54:dataStart] #调色板
# 变量预处理
pixels = []