可以从sys中读取参数

This commit is contained in:
Luthics 2023-01-09 20:47:27 +08:00
parent 007d16227c
commit 271b30e24f
3 changed files with 153 additions and 27 deletions

57
.vscode/settings.json vendored
View File

@ -1,3 +1,58 @@
{
"python.formatting.provider": "yapf"
"python.formatting.provider": "yapf",
"code-runner.runInTerminal": true,
"code-runner.saveFileBeforeRun": true,
"code-runner.executorMap": {
"javascript": "node",
"java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"php": "php",
"python": "python -u $fileName imgs/xjtu.bmp imgs/xjtu_new.bmp 512*512",
"perl": "perl",
"perl6": "perl6",
"ruby": "ruby",
"go": "go run",
"lua": "lua",
"groovy": "groovy",
"powershell": "powershell -ExecutionPolicy ByPass -File",
"bat": "cmd /c",
"shellscript": "bash",
"fsharp": "fsi",
"csharp": "scriptcs",
"vbscript": "cscript //Nologo",
"typescript": "ts-node",
"coffeescript": "coffee",
"scala": "scala",
"swift": "swift",
"julia": "julia",
"crystal": "crystal",
"ocaml": "ocaml",
"r": "Rscript",
"applescript": "osascript",
"clojure": "lein exec",
"haxe": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
"rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",
"racket": "racket",
"scheme": "csi -script",
"ahk": "autohotkey",
"autoit": "autoit3",
"dart": "dart",
"pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
"d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
"haskell": "runhaskell",
"nim": "nim compile --verbosity:0 --hints:off --run",
"lisp": "sbcl --script",
"kit": "kitc --run",
"v": "v run",
"sass": "sass --style expanded",
"scss": "scss --style expanded",
"less": "cd $dir && lessc $fileName $fileNameWithoutExt.css",
"FortranFreeForm": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"fortran-modern": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"fortran_fixed-form": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"fortran": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"sml": "cd $dir && sml $fileName"
}
}

BIN
imgs/wei.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 MiB

123
main.py
View File

@ -1,33 +1,107 @@
from math import floor, ceil
from tqdm import tqdm
import sys
import os
def error(code):
print("[error:" + str(code) + "]", end="", file=sys.stderr)
if (code == 1):
print('参数数量错误', file=sys.stderr)
elif (code == 2):
print('文件无法读入,请检查你的图片路径', file=sys.stderr)
elif (code == 3):
print('输出路径无法写出,请检查你的路径', file=sys.stderr)
elif (code == 4):
print('参数有误,缩放请使用 __*__旋转请使用 90/180/270', file=sys.stderr)
elif (code == 5):
print('缩放参数错误,新高/宽必须大于 0', file=sys.stderr)
elif (code == 6):
print('指定文件不是BMP格式无法读入', file=sys.stderr)
elif (code == 7):
print('暂不支持此图片,目前仅支持 bpp=24/32 的文件', file=sys.stderr)
exit(code)
def warn(code):
print("[warn:" + str(code) + "]", end="")
if (code == 1):
print('缩放模式异常,已重置为双线性插值模式')
elif (code == 2):
print('附加功能异常,已恢复默认值')
else:
print('未知错误')
################## 数据输入 Start ##################
# 输入数据
if (len(sys.argv) not in [3, 4, 5, 6]):
error(1)
img = input("请输入图片路径:")
new_width = int(input("请输入新宽度(px)"))
new_height = int(input("请输入新高度(px)"))
print("模式 0 双线性插值")
print("模式 1 最邻近插值")
print("模式 2 高斯模糊缩放")
mode = int(input("请输入模式:"))
# 输入数据
# img = input("请输入图片路径:")
# new_width = int(input("请输入新宽度(px)"))
# new_height = int(input("请输入新高度(px)"))
# print("模式 0 双线性插值")
# print("模式 1 最邻近插值")
# print("模式 2 高斯模糊缩放")
# mode = int(input("请输入模式:"))
# img = 'imgs/text.bmp'
# new_width = 2560 * 3
# new_height = 2560
# mode = 0
if (mode not in [0, 1, 2]):
mode = 0
input_file = sys.argv[1]
output_file = sys.argv[2]
new = sys.argv[3]
# 模式 0 双线性插值
# 模式 1 最邻近插值
# 模式 2 旋转图片
mode = 0
flur = 0
if ('*' in new):
new_width, new_height = map(int, sys.argv[3].split('*'))
if (new_width <= 0 or new_height <= 0):
error(5)
if (len(sys.argv) >= 5):
mode = int(sys.argv[4])
if (mode not in [0, 1]):
warn(1)
mode = 0
if (len(sys.argv) >= 6):
flur = int(sys.argv[5])
if (flur not in [0, 1]):
warn(2)
flur = 0
else:
mode = 0
else:
angle = int(new)
if (angle not in [90, 180, 270]):
error(4)
mode = 2
################## 数据输入 End ##################
################## 文件读入 Start ##################
if (not os.path.exists(input_file) or not os.access(input_file, os.R_OK)):
error(2)
if (not os.access(output_file, os.W_OK) and os.access(output_file, os.F_OK)):
error(3)
# 读入图片
with open(img, 'rb') as f:
imgBytes = f.read()
with open(input_file, 'rb') as f:
try:
imgBytes = f.read()
except:
error(2)
for _ in tqdm(range(len(imgBytes)), "读入文件中"):
pass
@ -57,8 +131,7 @@ def sizeByte(size, length):
# 位图文件头
# 检验文件头是否为 BM
if (imgBytes[0] != 66 or imgBytes[1] != 77):
print("当前文件非 BMP 格式")
exit()
error(6)
fileSize = byteSize(2, 4) #文件头中的文件大小
dataStart = byteSize(10, 4) #文件头中的数据开始字节
@ -90,8 +163,7 @@ while (i < len(colorsBoard)):
i += 4
if (not (bpp in [24, 32])):
print("不支持该图片")
exit()
error(7)
################## 文件头处理 End ##################
################## 像素点读入 Start ##################
@ -114,13 +186,6 @@ while (rowLength % 4 != 0 or rowLength == 0):
i = dataStart
for currentRow in tqdm(range(height), "读入像素点中"):
currentCol = 0
# if (bpp in [1, 2, 4, 8]):
# # 其实可以直接存入索引,之后再取出,但是为了易于理解,这里直接将像素数据插入 pixels 数组,这会导致效率的损失
# b = 0
# # while(b < width*bpp):
# # if(bpp==1):
# # print("")
# else:
while (currentCol < width * (bpp // 8)):
if (bpp == 32):
pixels[currentRow].append({
@ -148,7 +213,10 @@ for currentRow in tqdm(range(height), "读入像素点中"):
currentCol += bpp // 8
# 读入图片为像素数组完成
# 缩放图片
################## 像素点读入 End ##################
################## 缩放图片 Start ##################
# 变量预处理
scale_w = new_width / width
scale_h = new_height / height
@ -217,6 +285,8 @@ for currentRow in tqdm(range(new_height), "计算新像素点中"):
ori_col = floor(currentCol / scale_w)
newpixels[currentRow].append(pixels[ori_row][ori_col])
################## 缩放图片 End ##################
# 处理新文件
newimgArray = [66, 77]
@ -328,7 +398,8 @@ def flur(row, col):
for i in tqdm(range(len(newpixels)), "将像素点格式化中"):
row = newpixels[i]
for col in range(len(row)):
if (mode == 2):
if (1 == flur):
print("?")
newimgArray.extend(flur(i, col))
else:
if (new_bpp // 8 == 3):
@ -347,7 +418,7 @@ for i in tqdm(range(len(newpixels)), "将像素点格式化中"):
# 写入新的文件
newimgBytes = bytes(newimgArray)
with open(img[:-4] + "_" + str(mode) + "_new" + img[-4:], 'wb') as f:
with open(output_file, 'wb') as f:
f.write(newimgBytes)
for _ in tqdm(range(len(newimgBytes)), "写出图片中"):
pass