本帖最后由 74Y1085 于 2023-5-21 15:15 编辑
测试求助一个python 统计图片尺寸的方法,
传入 宽和长2组数据 例如
7*12则匹配为A4. 允许长宽颠倒匹配项
图幅 最小值项 最大值项
A1 18 35
A1- 20 34
A1+ 25 43
a2 16 26
a2+ 18 31
A3 11 17
A4 7 12
之前有大佬帮助解决过,现在有些调整自己无法研究透转换为现在传入 最大最小值得匹配方法,求助
之前解决的原代码
[Python] 纯文本查看 复制代码 while True:
try:
isQuit = False
# 标准尺寸表
sizeMap = [
[12040, 8624, "A0"],
[10499, 7560, "A1"],
[12621, 7440, "A1+0.25"],
[7153, 5141, "A2"],
[9034, 5091, "A2+0.25"],
[4960, 3507, "A3"],
[2480, 3507, "A4"]
]
options = {
# True: 允许长宽颠倒
# False: 严格参数顺序
"wideMatching": True,
# 匹配范围, [下限,上限]
"scaleRange": [-0.05, +0.05]
}
sizeList = []
while True:
size = input("请输入图片的尺寸,无须区分长宽顺序,如:2480*3507\n输入q退出\n")
if size == "q":
isQuit = True
break
sizeList = size.split("*")
if len(sizeList) != 2 or not sizeList[0].isdigit() or not sizeList[1].isdigit():
print("输入格式错误,请重新输入")
continue
sizeList = [int(i) for i in sizeList]
break
if isQuit:
break
# 添加长宽颠倒匹配项
if options["wideMatching"]:
sizeMap = sizeMap + [[sizeMap[i][1], sizeMap[i][0], sizeMap[i][2]+"(长宽颠倒)"] for i in range(len(sizeMap))]
matched = False
# 匹配
for i in range(len(sizeMap)):
if sizeMap[i][0] * (1 + options["scaleRange"][0]) \
<= sizeList[0] <= \
sizeMap[i][0] * (1 + options["scaleRange"][1]) and \
sizeMap[i][1] * (1 + options["scaleRange"][0]) \
<= sizeList[1] <= \
sizeMap[i][1] * (1 + options["scaleRange"][1]):
print("匹配成功,对应的标准尺寸为:", sizeMap[i][2])
matched = True
break
if not matched:
print("匹配失败")
print("\n\n")
except ValueError:
print("错误信息:", ValueError)
print("捕捉到异常,回车键退出")
input()
|