读入调色板,修改rowLength计算

This commit is contained in:
Luthics 2023-01-08 22:52:06 +08:00
parent 338b90f6f2
commit f160f27709
1 changed files with 38 additions and 13 deletions

51
main.py
View File

@ -1,27 +1,35 @@
from math import floor
from tqdm import tqdm
################## 数据输入 Start ##################
# 输入数据
img = input("请输入图片路径:")
new_width = int(input("请输入新宽度(px)"))
new_height = int(input("请输入新高度(px)"))
print("模式 0 直接缩放")
print("模式 1 高斯模糊缩放")
mode = int(input("请输入模式:"))
# img = 'imgs/1/2x3.bmp'
# new_width = 512
# new_height = 512
# mode = 0
# img = input("请输入图片路径:")
# new_width = int(input("请输入新宽度(px)"))
# new_height = int(input("请输入新高度(px)"))
# print("模式 0 直接缩放")
# print("模式 1 高斯模糊缩放")
# mode = int(input("请输入模式:"))
img = 'imgs/1/2x1.bmp'
new_width = 512
new_height = 512
mode = 0
if (mode != 1):
mode = 0
################## 数据输入 End ##################
################## 文件读入 Start ##################
# 读入图片
with open(img, 'rb') as f:
imgBytes = f.read()
for _ in tqdm(range(len(imgBytes)), "读入文件中"):
pass
################## 文件读入 End ##################
# 十六进制转十进制
def byteSize(begin, length):
@ -41,6 +49,8 @@ def sizeByte(size, length):
return b[::-1]
################## 文件头处理 Start ##################
# 位图文件头
# 检验文件头是否为 BM
if (imgBytes[0] != 66 or imgBytes[1] != 77):
@ -64,13 +74,28 @@ colorsNum = byteSize(46, 4) #调色板的颜色数为0时表示颜色数为
icolorsNum = byteSize(50, 4) #重要颜色数为0时表示所有颜色都是重要的通常不使用本项
colorsBoard = imgBytes[54:dataStart] #调色板
colors = []
i = 0
while (i < len(colorsBoard)):
colors.append({
'r': colorsBoard[i],
'g': colorsBoard[i + 1],
'b': colorsBoard[i + 2],
'a': colorsBoard[i + 3],
})
i += 4
################## 文件头处理 End ##################
################## 像素点读入 Start ##################
# 变量预处理
pixels = []
for i in range(height):
pixels.append([])
rowLength = width
rowLength *= bpp // 8
while (rowLength % 4 != 0):
rowLength = floor(width * bpp / 8)
while (rowLength % 4 != 0 or rowLength == 0):
rowLength += 1
# 计算像素点