smileat2000 发表于 2022-3-29 12:19

求助 这个代码要怎么优化才能效率更高 代码更少

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.五联画200x100(40x40 40x80 40x100 40x80 40x40)

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

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

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

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

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

    14.五联画300x150(60x90 60x120 60x150 60x120 60x90)
   
    请选择:""")
    return k
k=put()
h = img.shape
w = img.shape

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

# 三联画160x60(40x60 80x60 40x60)
if k == '1':
    h = int(img.shape / 16 * 6)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    qg((n,n,n),('A6x60.jpg','B80x60.jpg','C40x60.jpg'))

# 三联画200x70(50x70 100x70 50x70)
elif k == '2':
    h = int(img.shape / 20 * 7)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    qg((n,n,n),('A50x70.jpg','B100x70.jpg','C50x70.jpg'))

# 三联画240x80(60x80 120x80 60x80)
elif k == '3':
    h = int(img.shape / 24 * 8)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    qg((n,n,n),('A60x80.jpg','B120x80.jpg','C60x80.jpg'))

# 三联画280x90(70x90 140x90 70x90)
elif k == '4':
    h = int(img.shape / 28 * 9)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    qg((n,n,n),('A70x90.jpg','B140x90.jpg','C70x90.jpg'))

# 四联画140x90(30x60 40x80 40x80 30x60)
elif k == '5':
    h = int(img.shape / 14 * 9)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    qg((n,n,n,n),('A30x60.jpg','B40x80.jpg','C40x80.jpg','D30x60.jpg'))

# 四联画180x110(40x80 50x100 50x100 40x80)
elif k == '6':
    h = int(img.shape / 18 * 11)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    qg((n,n,n,n),('A40x80.jpg','B50x100.jpg','C50x100.jpg','D40x80.jpg'))

# 四联画220x130(50x100 60x120 60x120 50x100)
elif k == '7':
    h = int(img.shape / 22 * 13)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    qg((n,n,n,n),('A50x100.jpg','B60x120.jpg','C60x120.jpg','D50x100.jpg'))

# 五联画200x100(40x40 40x80 50x100 40x80 40x40)
elif k == '8':
    h = int(img.shape / 2)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    qg((n,n,n,n,n),('A40x40.jpg','B40x80.jpg','C40x100.jpg','D40x80.jpg','E40x40.jpg'))

# 五联画200x100(40x60 40x80 50x100 40x80 40x60)
elif k == '9':
    h = int(img.shape / 2)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    qg((n,n,n,n,n),('A40x60.jpg','B40x80.jpg','C40x100.jpg','D40x80.jpg','E40x60.jpg'))

# 五联画200x100(40x100 40x100 40x100 40x100 40x100)
elif k == '10':
    h = int(img.shape / 2)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    qg((n,n,n,n,n),('A40x100.jpg','B40x100.jpg','C40x100.jpg','D40x100.jpg','E40x100.jpg'))

# 五联画250x120(50x50 50x100 50x120 50x100 50x50)
elif k == '11':
    h = int(img.shape / 25 * 12)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    qg((n,n,n,n,n),('A50x50.jpg','B50x100.jpg','C50x120.jpg','D50x100.jpg','E50x50.jpg'))

# 五联画250x120(50x80 50x100 50x120 50x100 50x80)
elif k == '12':
    h = int(img.shape / 25 * 12)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    qg((n,n,n,n,n),('A50x80.jpg','B50x100.jpg','C50x120.jpg','D50x100.jpg','E50x80.jpg'))

# 五联画240x130(60x60 40x120 40x120 40x120 60x60)
elif k == '13':
    h = int(img.shape / 24 * 13)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    qg((n,n,n,n,n),('A60x60.jpg','B40x120.jpg','C40x120.jpg','D40x120.jpg','E60x60.jpg'))

# 五联画300x150(60x90 60x120 60x150 60x120 60x90)
elif k == '14':
    h = int(img.shape / 2)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    qg((n,n,n,n,n),('A60x90.jpg','B60x120.jpg','C60x150.jpg','D60x120.jpg','E60x90.jpg'))

LuoShang 发表于 2022-3-29 12:52

感觉没有优化的余地了,看起来挺简洁的{:301_998:}

RoyPenn 发表于 2022-3-29 13:47

弄个函数?

kesai 发表于 2022-3-29 13:48

本帖最后由 kesai 于 2022-3-29 13:49 编辑

根据k值映射对应参数,这样if语句就不需要了

然并卵zh 发表于 2022-3-29 14:04

策略模式,各部件的处理逻辑封装成策略,新建一个map,key为1-14的输入,value为对应的策略

keweiye 发表于 2022-3-29 14:12

ctrl+c   ctrl+v 更简单

wCoding 发表于 2022-3-29 14:20

代码层面的调整:
1、减少endswith判断,先取出文件扩展名称,然后拿扩展名称和几个扩展名成字符串匹配。
2、把类似 ”/ 13 * 12”,“h / 13 * 12”的结果值提前计算,缓存到临时变量。
算法层面的调整,不熟悉功能,暂无建议。

smileat2000 发表于 2022-3-29 14:24

wCoding 发表于 2022-3-29 14:20
代码层面的调整:
1、减少endswith判断,先取出文件扩展名称,然后拿扩展名称和几个扩展名成字符串匹配。
...

怎么字符串匹配呢?好像还是得要判断

ID88512 发表于 2022-3-29 15:52

函数传参      

smileat2000 发表于 2022-3-29 17:03

ID88512 发表于 2022-3-29 15:52
函数传参

已经搞了一个函数了,下面那些不知道再怎么弄了
页: [1] 2
查看完整版本: 求助 这个代码要怎么优化才能效率更高 代码更少