高斯模糊 √

This commit is contained in:
Luthics 2023-01-07 16:10:56 +08:00
parent 528869f071
commit 145279fa0a
1 changed files with 99 additions and 34 deletions

131
main.py
View File

@ -1,4 +1,26 @@
from math import floor from math import floor
from tqdm import tqdm
# 输入数据
img = input("请输入图片路径:")
new_width = int(input("请输入新宽度(px)"))
new_height = int(input("请输入新高度(px)"))
print("模式 0 直接缩放")
print("模式 1 高斯模糊缩放")
mode = int(input("请输入模式:"))
# img = 'imgs/bw.bmp'
# new_width = 512
# new_height = 512
# mode = 1
if (mode != 1):
mode = 0
# 读入图片
with open(img, 'rb') as f:
imgBytes = f.read()
for _ in tqdm(range(len(imgBytes)), "读入文件中"):
pass
# 十六进制转十进制 # 十六进制转十进制
@ -19,18 +41,6 @@ def sizeByte(size, length):
return b[::-1] return b[::-1]
# 输入数据
img = input("请输入图片路径:")
new_width = int(input("请输入新宽度(px)"))
new_height = int(input("请输入新高度(px)"))
# img = 'imgs/24.bmp'
# new_width = 128
# new_height = 128
# 读入图片
with open(img, 'rb') as f:
imgBytes = f.read()
# 文件头处理 # 文件头处理
# 检验文件头是否为 BM # 检验文件头是否为 BM
if (imgBytes[0] != 66 or imgBytes[1] != 77): if (imgBytes[0] != 66 or imgBytes[1] != 77):
@ -62,7 +72,7 @@ while (rowLength % 4 != 0):
# 计算像素点 # 计算像素点
i = dataStart i = dataStart
currentRow = 0 currentRow = 0
while (currentRow < height): for currentRow in tqdm(range(height), "读入像素点中"):
currentCol = 0 currentCol = 0
while (currentCol < width * (bpp // 8)): while (currentCol < width * (bpp // 8)):
if (bpp == 32): if (bpp == 32):
@ -87,7 +97,6 @@ while (currentRow < height):
imgBytes[dataStart + currentRow * rowLength + currentCol + 2] imgBytes[dataStart + currentRow * rowLength + currentCol + 2]
}) })
currentCol += bpp // 8 currentCol += bpp // 8
currentRow += 1
# 读入图片为像素数组完成 # 读入图片为像素数组完成
# 缩放图片 # 缩放图片
@ -99,14 +108,11 @@ for i in range(new_height):
newpixels.append([]) newpixels.append([])
# 计算新像素 # 计算新像素
currentRow = 0 for currentRow in tqdm(range(new_height), "计算新像素点中"):
while (currentRow < new_height): for currentCol in range(new_width):
currentCol = 0 ori_row = floor(currentRow / scale_h)
while (currentCol < new_width): ori_col = floor(currentCol // scale_w)
newpixels[currentRow].append(pixels[floor(currentRow / scale_h)][floor( newpixels[currentRow].append(pixels[ori_row][ori_col])
currentCol // scale_w)])
currentCol += 1
currentRow += 1
# 处理新文件 # 处理新文件
newimgArray = [66, 77] newimgArray = [66, 77]
@ -150,20 +156,77 @@ newimgArray.extend(sizeByte(new_imageSize, 4))
# 调色板不做处理 # 调色板不做处理
newimgArray.extend(sizeByte(0, new_headerSize + 14 - len(newimgArray))) newimgArray.extend(sizeByte(0, new_headerSize + 14 - len(newimgArray)))
for i in range(len(newpixels)):
def flur(row, col):
if (row - 1 < 0):
row = new_height + 1
if (row + 1 >= new_height):
row = 0
if (col - 1 < 0):
col = new_width + 1
if (col + 1 >= new_width):
col = 0
p = [0.0947416, 0.118318, 0.147761]
r = floor(newpixels[row - 1][col - 1]['r'] * p[0] +
newpixels[row - 1][col + 1]['r'] * p[0] +
newpixels[row + 1][col - 1]['r'] * p[0] +
newpixels[row + 1][col + 1]['r'] * p[0] +
newpixels[row][col + 1]['r'] * p[1] +
newpixels[row][col - 1]['r'] * p[1] +
newpixels[row - 1][col]['r'] * p[1] +
newpixels[row + 1][col]['r'] * p[1] +
newpixels[row][col]['r'] * p[2])
g = floor(newpixels[row - 1][col - 1]['g'] * p[0] +
newpixels[row - 1][col + 1]['g'] * p[0] +
newpixels[row + 1][col - 1]['g'] * p[0] +
newpixels[row + 1][col + 1]['g'] * p[0] +
newpixels[row][col + 1]['g'] * p[1] +
newpixels[row][col - 1]['g'] * p[1] +
newpixels[row - 1][col]['g'] * p[1] +
newpixels[row + 1][col]['g'] * p[1] +
newpixels[row][col]['g'] * p[2])
b = floor(newpixels[row - 1][col - 1]['b'] * p[0] +
newpixels[row - 1][col + 1]['b'] * p[0] +
newpixels[row + 1][col - 1]['b'] * p[0] +
newpixels[row + 1][col + 1]['b'] * p[0] +
newpixels[row][col + 1]['b'] * p[1] +
newpixels[row][col - 1]['b'] * p[1] +
newpixels[row - 1][col]['b'] * p[1] +
newpixels[row + 1][col]['b'] * p[1] +
newpixels[row][col]['b'] * p[2])
if (new_bpp // 8 == 3):
return [r, g, b]
else:
a = floor(newpixels[row - 1][col - 1]['a'] * p[0] +
newpixels[row - 1][col + 1]['a'] * p[0] +
newpixels[row + 1][col - 1]['a'] * p[0] +
newpixels[row + 1][col + 1]['a'] * p[0] +
newpixels[row][col + 1]['a'] * p[1] +
newpixels[row][col - 1]['a'] * p[1] +
newpixels[row - 1][col]['a'] * p[1] +
newpixels[row + 1][col]['a'] * p[1] +
newpixels[row][col]['a'] * p[2])
return [r, g, b, a]
for i in tqdm(range(len(newpixels)), "将像素点格式化中"):
row = newpixels[i] row = newpixels[i]
for col in range(len(row)): for col in range(len(row)):
if (new_bpp // 8 == 3): if (mode == 0):
pixel = [ if (new_bpp // 8 == 3):
newpixels[i][col]['r'], newpixels[i][col]['g'], pixel = [
newpixels[i][col]['b'] newpixels[i][col]['r'], newpixels[i][col]['g'],
] newpixels[i][col]['b']
]
else:
pixel = [
newpixels[i][col]['r'], newpixels[i][col]['g'],
newpixels[i][col]['b'], newpixels[i][col]['a']
]
newimgArray.extend(pixel)
else: else:
pixel = [ newimgArray.extend(flur(i, col))
newpixels[i][col]['r'], newpixels[i][col]['g'],
newpixels[i][col]['b'], newpixels[i][col]['a']
]
newimgArray.extend(pixel)
newimgArray.extend(sizeByte(0, rowLength - len(row) * (new_bpp // 8))) newimgArray.extend(sizeByte(0, rowLength - len(row) * (new_bpp // 8)))
# 写入新的文件 # 写入新的文件
@ -171,3 +234,5 @@ newimgBytes = bytes(newimgArray)
with open(img[:-4] + "_new" + img[-4:], 'wb') as f: with open(img[:-4] + "_new" + img[-4:], 'wb') as f:
f.write(newimgBytes) f.write(newimgBytes)
for _ in tqdm(range(len(newimgBytes)), "写出图片中"):
pass