吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 974|回复: 11
收起左侧

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

  [复制链接]
smileat2000 发表于 2022-3-29 12:19
[Python] 纯文本查看 复制代码
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[0]
w = img.shape[1]

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

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

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

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

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

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

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

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

# 五联画200x100(40x40 40x80 50x100 40x80 40x40)
elif k == '8':
    h = int(img.shape[1] / 2)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    qg((n[int(h / 10 * 3):int(h / 10 * 7), 0:int(w / 20 * 4)],n[int(h / 10):int(h / 10 * 9), int(w / 20 * 4):int(w / 20 * 8)],n[0:h, int(w / 20 * 8):int(w / 20 * 12)],n[int(h / 10):int(h / 10 * 9), int(w / 20 * 12):int(w / 20 * 16)],n[int(h / 10 * 3):int(h / 10 * 7), int(w / 20 * 16):w]),('A40x40.jpg','B40x80.jpg','C40x100.jpg','D40x80.jpg','E40x40.jpg'))

# 五联画200x100(40x60 40x80 50x100 40x80 40x60)
elif k == '9':
    h = int(img.shape[1] / 2)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    qg((n[int(h / 10 * 2):int(h / 10 * 8), 0:int(w / 20 * 4)],n[int(h / 10):int(h / 10 * 9), int(w / 20 * 4):int(w / 20 * 8)],n[0:h, int(w / 20 * 8):int(w / 20 * 12)],n[int(h / 10):int(h / 10 * 9), int(w / 20 * 12):int(w / 20 * 16)],n[int(h / 10 * 2):int(h / 10 * 8), int(w / 20 * 16):w]),('A40x60.jpg','B40x80.jpg','C40x100.jpg','D40x80.jpg','E40x60.jpg'))

# 五联画200x100(40x100 40x100 40x100 40x100 40x100)
elif k == '10':
    h = int(img.shape[1] / 2)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    qg((n[0:h, 0:int(w / 20 * 4)],n[0:h, int(w / 20 * 4):int(w / 20 * 8)],n[0:h, int(w / 20 * 8):int(w / 20 * 12)],n[0:h, int(w / 20 * 12):int(w / 20 * 16)],n[0:h, int(w / 20 * 16):w]),('A40x100.jpg','B40x100.jpg','C40x100.jpg','D40x100.jpg','E40x100.jpg'))

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

# 五联画250x120(50x80 50x100 50x120 50x100 50x80)
elif k == '12':
    h = int(img.shape[1] / 25 * 12)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    qg((n[int(h / 12 * 2):int(h / 12 * 10), 0:int(w / 25 * 5)],n[int(h / 12):int(h / 12 * 11), int(w / 25 * 5):int(w / 25 * 10)],n[0:h, int(w / 25 * 10):int(w / 25 * 15)],n[int(h / 12):int(h / 12 * 11), int(w / 25 * 15):int(w / 25 * 20)],n[int(h / 12 * 2):int(h / 12 * 10), int(w / 25 * 20):w]),('A50x80.jpg','B50x100.jpg','C50x120.jpg','D50x100.jpg','E50x80.jpg'))

# 五联画240x130(60x60 40x120 40x120 40x120 60x60)
elif k == '13':
    h = int(img.shape[1] / 24 * 13)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    qg((n[int(h / 26 * 7):int(h / 26 * 19), 0:int(w / 48 * 12)],n[0:int(h / 26 * 24), int(w / 48 * 12):int(w / 48 * 20)],n[int(h/26*2):h, int(w / 48 * 20):int(w / 48 * 28)],n[0:int(h / 26 * 24), int(w / 48 * 28):int(w / 48 * 36)],n[int(h / 26 * 7):int(h / 26 * 19), int(w / 48 * 36):w]),('A60x60.jpg','B40x120.jpg','C40x120.jpg','D40x120.jpg','E60x60.jpg'))

# 五联画300x150(60x90 60x120 60x150 60x120 60x90)
elif k == '14':
    h = int(img.shape[1] / 2)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    qg((n[int(h / 15 * 3):int(h / 15 * 12), 0:int(w / 30 * 6)],n[int(h / 15*1.5):int(h / 15 * 13.5), int(w / 30 * 6):int(w / 30 * 12)],n[0:h, int(w / 30 * 12):int(w / 30 * 18)],n[int(h / 15*1.5):int(h / 15 * 13.5), int(w / 30 * 18):int(w / 30 * 24)],n[int(h / 15 * 3):int(h / 15 * 12), int(w / 30 * 24):w]),('A60x90.jpg','B60x120.jpg','C60x150.jpg','D60x120.jpg','E60x90.jpg'))

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

LuoShang 发表于 2022-3-29 12:52
感觉没有优化的余地了,看起来挺简洁的
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

已经搞了一个函数了,下面那些不知道再怎么弄了
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 14:28

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表