写了个有关又无关的demo
对着示意图凑活看一下吧=_=
[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()
附件:
https://taozhiyu.lanzouf.com/iDzyT0lcsxte |