smileat2000 发表于 2022-4-14 13:09

不用if如何选项 判断

用来切割图片的
if太多了 求教有没有更简洁用来选择的方法
有没有更好的方法读取图片?
如何实现拖动图片到程序图标上自动读取拖入的图片?
import os
from tkinter import N
import cv2

#读取图片
def read():
    for file in os.listdir(os.getcwd()):
      if file.endswith("jpg") or file.endswith("jpeg") or file.endswith("png") or file.endswith("tif") or file.endswith("tif"):
            break
    return file

#入口
if __name__=="__main__":
    file=read()
    lname=os.path.splitext(file)[-1]
    os.rename(file, "img" + lname)
    img = cv2.imread("img" + lname)
    os.rename("img" + lname, file)

#选择
def put():
    k = input("\n    "+read()+"""\n
   
    1.三联画160x60(40x60 80x60 40x60)
   
    2.三联画200x70(50x70 100x70 50x70)
   
    3.三联画240x80(60x80 120x80 60x80)
   
    4.三联画280x90(70x90 140x90 70x90)

    5.四联画140x90(30x60 40x80 40x80 30x60)
   
    6.四联画180x110(40x80 50x100 50x100 40x80)
   
    7.四联画220x130(50x100 60x120 60x120 50x100)
   
    8.五联画180x90(36x50 36x70 36x90 36x70 36x50)

    9.五联画200x100(40x40 40x80 40x100 40x80 40x40)

    10.五联画200x100(40x60 40x80 40x100 40x80 40x60)

    11.五联画200x100(40x100 40x100 40x100 40x100 40x100)

    12.五联画250x120(50x50 50x100 50x120 50x100 50x50)

    13.五联画250x120(50x80 50x100 50x120 50x100 50x80)

    14.五联画240x130(60x60 40x120 40x120 40x120 60x60)

    15.五联画300x150(60x90 60x120 60x150 60x120 60x90)
   
    请选择:""")
    return k
k=put()

#切割
def qg(ns,names):
    i=0
    while i<len(names):
      cv2.imwrite(names,ns,)
      i=i+1
    return

#修改尺寸
def rsz(img,w,h):
    n=cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    return n

# 三联画160x60(40x60 80x60 40x60)
if k == '1':
    n=rsz(img,16000,6000)
    qg((n,n,n),('A6x60.tif','B80x60.tif','C40x60.tif'))

# 三联画200x70(50x70 100x70 50x70)
elif k == '2':
    n=rsz(img,20000,7000)
    qg((n,n,n),('A50x70.tif','B100x70.tif','C50x70.tif'))

# 三联画240x80(60x80 120x80 60x80)
elif k == '3':
    n=rsz(img,24000,8000)
    qg((n,n,n),('A60x80.tif','B120x80.tif','C60x80.tif'))

# 三联画280x90(70x90 140x90 70x90)
elif k == '4':
    n=rsz(img,28000,9000)
    qg((n,n,n),('A70x90.tif','B140x90.tif','C70x90.tif'))

# 四联画140x90(30x60 40x80 40x80 30x60)
elif k == '5':
    n=rsz(img,14000,9000)
    qg((n,n,n,n),('A30x60.tif','B40x80.tif','C40x80.tif','D30x60.tif'))

# 四联画180x110(40x80 50x100 50x100 40x80)
elif k == '6':
    n=rsz(img,18000,11000)
    qg((n,n,n,n),('A40x80.tif','B50x100.tif','C50x100.tif','D40x80.tif'))

# 四联画220x130(50x100 60x120 60x120 50x100)
elif k == '7':
    n=rsz(img,22000,13000)
    qg((n,n,n,n),('A50x100.tif','B60x120.tif','C60x120.tif','D50x100.tif'))

# 五联画180x90(36x50 36x70 36x90 36x70 36x50)
elif k == '8':
    n=rsz(img,18000,9000)
    qg((n,n,n,n,n),('A36x50.tif','B36x70.tif','C36x90.tif','D36x70.tif','E36x50.tif'))

# 五联画200x100(40x40 40x80 50x100 40x80 40x40)
elif k == '9':
    n=rsz(img,20000,10000)
    qg((n,n,n,n,n),('A40x40.tif','B40x80.tif','C40x100.tif','D40x80.tif','E40x40.tif'))

# 五联画200x100(40x60 40x80 50x100 40x80 40x60)
elif k == '10':
    n=rsz(img,20000,10000)
    qg((n,n,n,n,n),('A40x60.tif','B40x80.tif','C40x100.tif','D40x80.tif','E40x60.tif'))
   
# 五联画200x100(40x100 40x100 40x100 40x100 40x100)
elif k == '11':
    n=rsz(img,20000,10000)
    qg((n,n,n,n,n),('A40x100.tif','B40x100.tif','C40x100.tif','D40x100.tif','E40x100.tif'))

# 五联画250x120(50x50 50x100 50x120 50x100 50x50)
elif k == '12':
    n=rsz(img,25000,12000)
    qg((n,n,n,n,n),('A50x50.tif','B50x100.tif','C50x120.tif','D50x100.tif','E50x50.tif'))

# 五联画250x120(50x80 50x100 50x120 50x100 50x80)
elif k == '13':
    n=rsz(img,25000,12000)
    qg((n,n,n,n,n),('A50x80.tif','B50x100.tif','C50x120.tif','D50x100.tif','E50x80.tif'))

# 五联画240x130(60x60 40x120 40x120 40x120 60x60)
elif k == '14':
    n=rsz(img,24000,13000)
    qg((n,n,n,n,n),('A60x60.tif','B40x120.tif','C40x120.tif','D40x120.tif','E60x60.tif'))

# 五联画300x150(60x90 60x120 60x150 60x120 60x90)
elif k == '15':
    n=rsz(img,30000,15000)
    qg((n,n,n,n,n),('A60x90.tif','B60x120.tif','C60x150.tif','D60x120.tif','E60x90.tif'))

smileat2000 发表于 2022-4-14 14:23

修改了一下 把if换成了match case
还有更好的方法提高效率吗?
读取图片有没有更好的方法呢?from ast import match_case
import os
import cv2

def read():# 读取图片
    for file in os.listdir(os.getcwd()):
      if file.endswith("jpg") or file.endswith("jpeg") or file.endswith("png") or file.endswith("tif") or file.endswith("tiff"):
            break
    return file

def rsz(img, w, h):# 修改尺寸
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    return n

def qg(ns, names):# 切割
    i = 0
    while i < len(names):
      cv2.imwrite(names, ns, )
      i = i+1
    return

if __name__ == "__main__":# 入口
    file = read()
    lname = os.path.splitext(file)[-1]
    os.rename(file, "img" + lname)
    img = cv2.imread("img" + lname)
    os.rename("img" + lname, file)

def xz():# 选择
    k = input("\n    "+read()+"""\n
   
    1.三联画160x60(40x60 80x60 40x60)
   
    2.三联画200x70(50x70 100x70 50x70)
   
    3.三联画240x80(60x80 120x80 60x80)
   
    4.三联画280x90(70x90 140x90 70x90)

    5.四联画140x90(30x60 40x80 40x80 30x60)
   
    6.四联画180x110(40x80 50x100 50x100 40x80)
   
    7.四联画220x130(50x100 60x120 60x120 50x100)
   
    8.五联画180x90(36x50 36x70 36x90 36x70 36x50)

    9.五联画200x100(40x40 40x80 40x100 40x80 40x40)

    10.五联画200x100(40x60 40x80 40x100 40x80 40x60)

    11.五联画200x100(40x100 40x100 40x100 40x100 40x100)

    12.五联画250x120(50x50 50x100 50x120 50x100 50x50)

    13.五联画250x120(50x80 50x100 50x120 50x100 50x80)

    14.五联画240x130(60x60 40x120 40x120 40x120 60x60)

    15.五联画300x150(60x90 60x120 60x150 60x120 60x90)
   
    请选择:""")
    return k

match xz():
    case '1':# 三联画160x60(40x60 80x60 40x60)
      n = rsz(img, 16000, 6000)
      qg((n, n, n), ('A6x60.tif', 'B80x60.tif', 'C40x60.tif'))
    case '2':# 三联画200x70(50x70 100x70 50x70)
      n = rsz(img, 20000, 7000)
      qg((n, n, n),('A50x70.tif', 'B100x70.tif', 'C50x70.tif'))
    case '3':# 三联画240x80(60x80 120x80 60x80)
      n = rsz(img, 24000, 8000)
      qg((n, n, n),('A60x80.tif', 'B120x80.tif', 'C60x80.tif'))
    case '4':# 三联画280x90(70x90 140x90 70x90)
      n = rsz(img, 28000, 9000)
      qg((n, n, n),('A70x90.tif', 'B140x90.tif', 'C70x90.tif'))
    case '5':# 四联画140x90(30x60 40x80 40x80 30x60)
      n = rsz(img, 14000, 9000)
      qg((n, n, n,n), ('A30x60.tif', 'B40x80.tif', 'C40x80.tif', 'D30x60.tif'))
    case '6':# 四联画180x110(40x80 50x100 50x100 40x80)
      n = rsz(img, 18000, 11000)
      qg((n, n, n,n), ('A40x80.tif', 'B50x100.tif', 'C50x100.tif', 'D40x80.tif'))
    case '7':# 四联画220x130(50x100 60x120 60x120 50x100)
      n = rsz(img, 22000, 13000)
      qg((n, n, n,n), ('A50x100.tif', 'B60x120.tif', 'C60x120.tif', 'D50x100.tif'))
    case '8':# 五联画180x90(36x50 36x70 36x90 36x70 36x50)
      n = rsz(img, 18000, 9000)
      qg((n, n, n, n,n), ('A36x50.tif', 'B36x70.tif', 'C36x90.tif', 'D36x70.tif', 'E36x50.tif'))
    case '9':# 五联画200x100(40x40 40x80 50x100 40x80 40x40)
      n = rsz(img, 20000, 10000)
      qg((n, n, n, n,n), ('A40x40.tif', 'B40x80.tif', 'C40x100.tif', 'D40x80.tif', 'E40x40.tif'))
    case '10':# 五联画200x100(40x60 40x80 50x100 40x80 40x60)
      n = rsz(img, 20000, 10000)
      qg((n, n, n, n,n), ('A40x60.tif', 'B40x80.tif', 'C40x100.tif', 'D40x80.tif', 'E40x60.tif'))
    case '11':# 五联画200x100(40x100 40x100 40x100 40x100 40x100)
      n = rsz(img, 20000, 10000)
      qg((n, n, n, n,n), ('A40x100.tif', 'B40x100.tif', 'C40x100.tif', 'D40x100.tif', 'E40x100.tif'))
    case '12':# 五联画250x120(50x50 50x100 50x120 50x100 50x50)
      n = rsz(img, 25000, 12000)
      qg((n, n, n, n,n), ('A50x50.tif', 'B50x100.tif', 'C50x120.tif', 'D50x100.tif', 'E50x50.tif'))
    case '13':# 五联画250x120(50x80 50x100 50x120 50x100 50x80)
      n = rsz(img, 25000, 12000)
      qg((n, n, n, n,n), ('A50x80.tif', 'B50x100.tif', 'C50x120.tif', 'D50x100.tif', 'E50x80.tif'))
    case '14':# 五联画240x130(60x60 40x120 40x120 40x120 60x60)
      n = rsz(img, 24000, 13000)
      qg((n, n, n, n,n), ('A60x60.tif', 'B40x120.tif', 'C40x120.tif', 'D40x120.tif', 'E60x60.tif'))
    case '15':# 五联画300x150(60x90 60x120 60x150 60x120 60x90)
      n = rsz(img, 30000, 15000)
      qg((n, n, n, n,n), ('A60x90.tif', 'B60x120.tif', 'C60x150.tif', 'D60x120.tif', 'E60x90.tif'))

xiaoyu2032 发表于 2022-4-14 13:19

什么意思?Case不行吗?

yhw231 发表于 2022-4-14 13:40

switch case

fan1191237272 发表于 2022-4-14 13:50

把if里面的逻辑放到一个对象或者数组,比如对象obj = {1: aaa, 2: bbb},用的时候就直接obj

smileat2000 发表于 2022-4-14 14:25

:'(weeqw感觉控制台程序好麻烦啊!可是python做图形界面又要用到别的,打包出来exe文件会更大:'(weeqw

我心飞翔1995 发表于 2022-4-14 15:01

smileat2000 发表于 2022-4-14 14:25
感觉控制台程序好麻烦啊!可是python做图形界面又要用到别的,打包出来exe文件会更大

aardio了解一下

柳岸红尘客 发表于 2022-4-14 15:49

策略模式 了解一下

zxc9989 发表于 2022-4-14 15:50

def patten(size:tuple, cut_param, name:tuple):
        def inner(img):
                n = rsz(img, *size)
                qg(tuple(n for i in cut_param), name)
        return inner

def slice_2d(a, a_e, b, b_e):
        # 二维切片
        return slice(a, a_e), slice(b, b_e)

def cut_param(*param):
        """
       切割参数
        """
        for item in param:
                yield slice_2d(*item)


METHOD = {
        1: patten((1600, 6000), cut_param((0, 6000, 0, 4000), (0, 6000, 4000, 12000), (0, 6000, 12000, 16000)), ('A6x60.tif', 'B80x60.tif', 'C40x60.tif')),
        2: patten((20000,7000), cut_param((0, 7000, 0, 5000), (0, 7000, 5000, 15000), (0, 7000, 15000, 20000)), ('A50x70.tif','B100x70.tif','C50x70.tif'))
        # 剩下的手动加进去
}
大致逻辑可以这样实现,不过这样看上去其实也没好到哪去

smileat2000 发表于 2022-4-14 16:10

本帖最后由 smileat2000 于 2022-4-14 16:13 编辑

zxc9989 发表于 2022-4-14 15:50
def patten(size:tuple, cut_param, name:tuple):
      def inner(img):
                n = rsz ...
这个涉及到了哪些知识点啊 看不懂啊 我需要图片是无损处理的,要固定分辨率和压缩格式,文件格式{:301_972:}
页: [1] 2
查看完整版本: 不用if如何选项 判断