高斯模糊 √
This commit is contained in:
parent
528869f071
commit
145279fa0a
133
main.py
133
main.py
|
@ -1,4 +1,26 @@
|
|||
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]
|
||||
|
||||
|
||||
# 输入数据
|
||||
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
|
||||
if (imgBytes[0] != 66 or imgBytes[1] != 77):
|
||||
|
@ -62,7 +72,7 @@ while (rowLength % 4 != 0):
|
|||
# 计算像素点
|
||||
i = dataStart
|
||||
currentRow = 0
|
||||
while (currentRow < height):
|
||||
for currentRow in tqdm(range(height), "读入像素点中"):
|
||||
currentCol = 0
|
||||
while (currentCol < width * (bpp // 8)):
|
||||
if (bpp == 32):
|
||||
|
@ -87,7 +97,6 @@ while (currentRow < height):
|
|||
imgBytes[dataStart + currentRow * rowLength + currentCol + 2]
|
||||
})
|
||||
currentCol += bpp // 8
|
||||
currentRow += 1
|
||||
# 读入图片为像素数组完成
|
||||
|
||||
# 缩放图片
|
||||
|
@ -99,14 +108,11 @@ for i in range(new_height):
|
|||
newpixels.append([])
|
||||
|
||||
# 计算新像素
|
||||
currentRow = 0
|
||||
while (currentRow < new_height):
|
||||
currentCol = 0
|
||||
while (currentCol < new_width):
|
||||
newpixels[currentRow].append(pixels[floor(currentRow / scale_h)][floor(
|
||||
currentCol // scale_w)])
|
||||
currentCol += 1
|
||||
currentRow += 1
|
||||
for currentRow in tqdm(range(new_height), "计算新像素点中"):
|
||||
for currentCol in range(new_width):
|
||||
ori_row = floor(currentRow / scale_h)
|
||||
ori_col = floor(currentCol // scale_w)
|
||||
newpixels[currentRow].append(pixels[ori_row][ori_col])
|
||||
|
||||
# 处理新文件
|
||||
newimgArray = [66, 77]
|
||||
|
@ -150,24 +156,83 @@ newimgArray.extend(sizeByte(new_imageSize, 4))
|
|||
# 调色板不做处理
|
||||
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]
|
||||
for col in range(len(row)):
|
||||
if (new_bpp // 8 == 3):
|
||||
pixel = [
|
||||
newpixels[i][col]['r'], newpixels[i][col]['g'],
|
||||
newpixels[i][col]['b']
|
||||
]
|
||||
if (mode == 0):
|
||||
if (new_bpp // 8 == 3):
|
||||
pixel = [
|
||||
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:
|
||||
pixel = [
|
||||
newpixels[i][col]['r'], newpixels[i][col]['g'],
|
||||
newpixels[i][col]['b'], newpixels[i][col]['a']
|
||||
]
|
||||
newimgArray.extend(pixel)
|
||||
newimgArray.extend(flur(i, col))
|
||||
|
||||
newimgArray.extend(sizeByte(0, rowLength - len(row) * (new_bpp // 8)))
|
||||
|
||||
# 写入新的文件
|
||||
newimgBytes = bytes(newimgArray)
|
||||
|
||||
with open(img[:-4] + "_new" + img[-4:], 'wb') as f:
|
||||
f.write(newimgBytes)
|
||||
f.write(newimgBytes)
|
||||
for _ in tqdm(range(len(newimgBytes)), "写出图片中"):
|
||||
pass
|
Loading…
Reference in New Issue