吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1585|回复: 4
收起左侧

[求助] Python Opencv图像处理和多线程的问题

[复制链接]
smileat2000 发表于 2022-2-21 17:23
有没有方法优化一下这个代码,效率太低了

import os
import cv2
import threading
#读取图片
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.四联画140x90(30x60 40x80 40x80 30x60)
   
    5.四联画180x110(40x80 50x100 50x100 40x80)
   
    6.四联画220x130(50x100 60x120 60x120 50x100)
   
    7.五联画200x100(40x40 40x80 40x100 40x80 40x40)
    8.五联画200x100(40x60 40x80 40x100 40x80 40x60)
    9.五联画200x100(40x100 40x100 40x100 40x100 40x100)
    10.五联画250x120(50x50 50x100 50x120 50x100 50x50)
    11.五联画250x120(50x80 50x100 50x120 50x100 50x80)
   
    请选择:""")
    return k
k=put()
h = img.shape[0]
w = img.shape[1]
# 三联画160x60(40x60 80x60 40x60)
if k == '1':
    h = int(img.shape[1] / 16 * 6)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    # 第一张图
    def n31(n1):
        n1 = n[0:h, 0:int(w / 16 * 4)]
        cv2.imwrite(
            'n1.tif', n1,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n1.tif', '三联画A40x60.tif')
    # 第二张图
    def n32(n2):
        n2 = n[0:h, int(w / 16 * 4):int(w / 16 * 12)]
        cv2.imwrite(
            'n2.tif', n2,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n2.tif', '三联画B80x60.tif')
    # 第三张图
    def n33(n3):
        n3 = n[0:h, int(w / 16 * 12):w]
        cv2.imwrite(
            'n3.tif', n3,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n3.tif', '三联画C40x60.tif')
    argsn = [n, h, w]
    threads = []
    t1 = threading.Thread(target=n31, args=(argsn, ))
    threads.append(t1)
    t2 = threading.Thread(target=n32, args=(argsn, ))
    threads.append(t2)
    t3 = threading.Thread(target=n33, args=(argsn, ))
    threads.append(t3)
    for t in threads:
        t.setDaemon = True
        t.start()
# 三联画200x70(50x70 100x70 50x70)
elif k == '2':
    h = int(img.shape[1] / 20 * 7)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    # 第一张图
    def n31(n1):
        n1 = n[0:h, 0:int(w / 20 * 5)]
        cv2.imwrite(
            'n1.tif', n1,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n1.tif', '三联画A50x70.tif')
    # 第二张图
    def n32(n2):
        n2 = n[0:h, int(w / 20 * 5):int(w / 20 * 15)]
        cv2.imwrite(
            'n2.tif', n2,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n2.tif', '三联画B100x70.tif')
    # 第三张图
    def n33(n3):
        n3 = n[0:h, int(w / 20 * 15):w]
        cv2.imwrite(
            'n3.tif', n3,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n3.tif', '三联画C50x70.tif')
    argsn = [n, h, w]
    threads = []
    t1 = threading.Thread(target=n31, args=(argsn, ))
    threads.append(t1)
    t2 = threading.Thread(target=n32, args=(argsn, ))
    threads.append(t2)
    t3 = threading.Thread(target=n33, args=(argsn, ))
    threads.append(t3)
    for t in threads:
        t.setDaemon = True
        t.start()
# 三联画240x80(60x80 120x80 60x80)
elif k == '3':
    h = int(img.shape[1] / 24 * 8)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    # 第一张图
    def n31(n1):
        n1 = n[0:h, 0:int(w / 24 * 6)]
        cv2.imwrite(
            'n1.tif', n1,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n1.tif', '三联画A60x80.tif')
    # 第二张图
    def n32(n2):
        n2 = n[0:h, int(w / 24 * 6):int(w / 24 * 18)]
        cv2.imwrite(
            'n2.tif', n2,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n2.tif', '三联画B120x80.tif')
    # 第三张图
    def n33(n3):
        n3 = n[0:h, int(w / 24 * 18):w]
        cv2.imwrite(
            'n3.tif', n3,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n3.tif', '三联画C60x80.tif')
    argsn = [n, h, w]
    threads = []
    t1 = threading.Thread(target=n31, args=(argsn, ))
    threads.append(t1)
    t2 = threading.Thread(target=n32, args=(argsn, ))
    threads.append(t2)
    t3 = threading.Thread(target=n33, args=(argsn, ))
    threads.append(t3)
    for t in threads:
        t.setDaemon = True
        t.start()
# 四联画140x90(30x60 40x80 40x80 30x60)
elif k == '4':
    h = int(img.shape[1] / 14 * 9)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    # 第一张图
    def n41(n1):
        n1 = n[int(h / 9 * 2):int(h / 9 * 8), 0:int(w / 14 * 3)]
        cv2.imwrite(
            'n1.tif', n1,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n1.tif', '四联画A30x60.tif')
    # 第二张图
    def n42(n2):
        n2 = n[int(h / 9):h, int(w / 14 * 3):int(w / 14 * 7)]
        cv2.imwrite(
            'n2.tif', n2,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n2.tif', '四联画B40x80.tif')
    # 第三张图
    def n43(n3):
        n3 = n[0:int(h / 9 * 8), int(w / 14 * 7):int(w / 14 * 11)]
        cv2.imwrite(
            'n3.tif', n3,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n3.tif', '四联画C40x80.tif')
    # 第四张图
    def n44(n4):
        n4 = n[int(h / 9):int(h / 9 * 7), int(w / 14 * 11):w]
        cv2.imwrite(
            'n4.tif', n4,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n4.tif', '四联画D30x60.tif')
    argsn = [n, h, w]
    threads = []
    t1 = threading.Thread(target=n41, args=(argsn, ))
    threads.append(t1)
    t2 = threading.Thread(target=n42, args=(argsn, ))
    threads.append(t2)
    t3 = threading.Thread(target=n43, args=(argsn, ))
    threads.append(t3)
    t4 = threading.Thread(target=n44, args=(argsn, ))
    threads.append(t4)
    for t in threads:
        t.setDaemon = True
        t.start()
# 四联画180x110(40x80 50x100 50x100 40x80)
elif k == '5':
    h = int(img.shape[1] / 18 * 11)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    # 第一张图
    def n41(n1):
        n1 = n[int(h / 11 * 2):int(h / 11 * 10), 0:int(w / 18 * 4)]
        cv2.imwrite(
            'n1.tif', n1,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n1.tif', '四联画A40x80.tif')
    # 第二张图
    def n42(n2):
        n2 = n[int(h / 11):h, int(w / 18 * 4):int(w / 18 * 9)]
        cv2.imwrite(
            'n2.tif', n2,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n2.tif', '四联画B50x100.tif')
    # 第三张图
    def n43(n3):
        n3 = n[0:int(h / 11 * 10), int(w / 18 * 9):int(w / 18 * 14)]
        cv2.imwrite(
            'n3.tif', n3,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n3.tif', '四联画C50x100.tif')
    # 第四张图
    def n44(n4):
        n4 = n[int(h / 11):int(h / 11 * 9), int(w / 18 * 14):w]
        cv2.imwrite(
            'n4.tif', n4,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n4.tif', '四联画D40x80.tif')
    argsn = [n, h, w]
    threads = []
    t1 = threading.Thread(target=n41, args=(argsn, ))
    threads.append(t1)
    t2 = threading.Thread(target=n42, args=(argsn, ))
    threads.append(t2)
    t3 = threading.Thread(target=n43, args=(argsn, ))
    threads.append(t3)
    t4 = threading.Thread(target=n44, args=(argsn, ))
    threads.append(t4)
    for t in threads:
        t.setDaemon = True
        t.start()
# 四联画220x130(50x100 60x120 60x120 50x100)
elif k == '6':
    h = int(img.shape[1] / 22 * 13)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    # 第一张图
    def n41(n1):
        n1 = n[int(h / 13 * 2):int(h / 13 * 12), 0:int(w / 22 * 5)]
        cv2.imwrite(
            'n1.tif', n1,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n1.tif', '四联画A50x100.tif')
    # 第二张图
    def n42(n2):
        n2 = n[int(h / 13):h, int(w / 22 * 5):int(w / 22 * 11)]
        cv2.imwrite(
            'n2.tif', n2,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n2.tif', '四联画B60x120.tif')
    # 第三张图
    def n43(n3):
        n3 = n[0:int(h / 13 * 12), int(w / 22 * 11):int(w / 22 * 17)]
        cv2.imwrite(
            'n3.tif', n3,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n3.tif', '四联画C60x120.tif')
    # 第四张图
    def n44(n4):
        n4 = n[int(h / 13):int(h / 13 * 11), int(w / 22 * 17):w]
        cv2.imwrite(
            'n4.tif', n4,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n4.tif', '四联画D50x100.tif')
    argsn = [n, h, w]
    threads = []
    t1 = threading.Thread(target=n41, args=(argsn, ))
    threads.append(t1)
    t2 = threading.Thread(target=n42, args=(argsn, ))
    threads.append(t2)
    t3 = threading.Thread(target=n43, args=(argsn, ))
    threads.append(t3)
    t4 = threading.Thread(target=n44, args=(argsn, ))
    threads.append(t4)
    for t in threads:
        t.setDaemon = True
        t.start()
# 五联画200x100(40x40 40x80 50x100 40x80 40x40)
elif k == '7':
    h = int(img.shape[1] / 2)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    # 第一张图
    def n51(n1):
        n1 = n[int(h / 10 * 3):int(h / 10 * 7), 0:int(w / 20 * 4)]
        cv2.imwrite(
            'n1.tif', n1,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n1.tif', '五联画A40x40.tif')
    # 第二张图
    def n52(n2):
        n2 = n[int(h / 10):int(h / 10 * 9), int(w / 20 * 4):int(w / 20 * 8)]
        cv2.imwrite(
            'n2.tif', n2,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n2.tif', '五联画B40x80.tif')
    # 第三张图
    def n53(n3):
        n3 = n[0:h, int(w / 20 * 8):int(w / 20 * 12)]
        cv2.imwrite(
            'n3.tif', n3,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n3.tif', '五联画C40x100.tif')
    # 第四张图
    def n54(n4):
        n4 = n[int(h / 10):int(h / 10 * 9), int(w / 20 * 12):int(w / 20 * 16)]
        cv2.imwrite(
            'n4.tif', n4,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n4.tif', '五联画D40x80.tif')
    # 第五张图
    def n55(n5):
        n5 = n[int(h / 10 * 3):int(h / 10 * 7), int(w / 20 * 16):w]
        cv2.imwrite(
            'n5.tif', n5,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n5.tif', '五联画E40x40.tif')
    argsn = [n, h, w]
    threads = []
    t1 = threading.Thread(target=n51, args=(argsn, ))
    threads.append(t1)
    t2 = threading.Thread(target=n52, args=(argsn, ))
    threads.append(t2)
    t3 = threading.Thread(target=n53, args=(argsn, ))
    threads.append(t3)
    t4 = threading.Thread(target=n54, args=(argsn, ))
    threads.append(t4)
    t5 = threading.Thread(target=n55, args=(argsn, ))
    threads.append(t5)
    for t in threads:
        t.setDaemon = True
        t.start()
# 五联画200x100(40x60 40x80 50x100 40x80 40x60)
elif k == '8':
    h = int(img.shape[1] / 2)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    # 第一张图
    def n51(n1):
        n1 = n[int(h / 10 * 2):int(h / 10 * 8), 0:int(w / 20 * 4)]
        cv2.imwrite(
            'n1.tif', n1,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n1.tif', '五联画A40x60.tif')
    # 第二张图
    def n52(n2):
        n2 = n[int(h / 10):int(h / 10 * 9), int(w / 20 * 4):int(w / 20 * 8)]
        cv2.imwrite(
            'n2.tif', n2,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n2.tif', '五联画B40x80.tif')
    # 第三张图
    def n53(n3):
        n3 = n[0:h, int(w / 20 * 8):int(w / 20 * 12)]
        cv2.imwrite(
            'n3.tif', n3,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n3.tif', '五联画C40x100.tif')
    # 第四张图
    def n54(n4):
        n4 = n[int(h / 10):int(h / 10 * 9), int(w / 20 * 12):int(w / 20 * 16)]
        cv2.imwrite(
            'n4.tif', n4,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n4.tif', '五联画D40x80.tif')
    # 第五张图
    def n55(n5):
        n5 = n[int(h / 10 * 2):int(h / 10 * 8), int(w / 20 * 16):w]
        cv2.imwrite(
            'n5.tif', n5,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n5.tif', '五联画E40x60.tif')
    argsn = [n, h, w]
    threads = []
    t1 = threading.Thread(target=n51, args=(argsn, ))
    threads.append(t1)
    t2 = threading.Thread(target=n52, args=(argsn, ))
    threads.append(t2)
    t3 = threading.Thread(target=n53, args=(argsn, ))
    threads.append(t3)
    t4 = threading.Thread(target=n54, args=(argsn, ))
    threads.append(t4)
    t5 = threading.Thread(target=n55, args=(argsn, ))
    threads.append(t5)
    for t in threads:
        t.setDaemon = True
        t.start()
elif k == '9':
    h = int(img.shape[1] / 2)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    # 第一张图
    def n51(n1):
        n1 = n[0:h, 0:int(w / 20 * 4)]
        cv2.imwrite(
            'n1.tif', n1,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n1.tif', '五联画A40x100.tif')
    # 第二张图
    def n52(n2):
        n2 = n[0:h, int(w / 20 * 4):int(w / 20 * 8)]
        cv2.imwrite(
            'n2.tif', n2,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n2.tif', '五联画B40x100.tif')
    # 第三张图
    def n53(n3):
        n3 = n[0:h, int(w / 20 * 8):int(w / 20 * 12)]
        cv2.imwrite(
            'n3.tif', n3,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n3.tif', '五联画C40x100.tif')
    # 第四张图
    def n54(n4):
        n4 = n[0:h, int(w / 20 * 12):int(w / 20 * 16)]
        cv2.imwrite(
            'n4.tif', n4,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n4.tif', '五联画D40x100.tif')
    # 第五张图
    def n55(n5):
        n5 = n[0:h, int(w / 20 * 16):w]
        cv2.imwrite(
            'n5.tif', n5,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n5.tif', '五联画E40x100.tif')
    argsn = [n, h, w]
    threads = []
    t1 = threading.Thread(target=n51, args=(argsn, ))
    threads.append(t1)
    t2 = threading.Thread(target=n52, args=(argsn, ))
    threads.append(t2)
    t3 = threading.Thread(target=n53, args=(argsn, ))
    threads.append(t3)
    t4 = threading.Thread(target=n54, args=(argsn, ))
    threads.append(t4)
    t5 = threading.Thread(target=n55, args=(argsn, ))
    threads.append(t5)
    for t in threads:
        t.setDaemon = True
        t.start()
# 五联画250x120(50x50 50x100 50x120 50x100 50x50)
elif k == '10':
    h = int(img.shape[1] / 25 * 12)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    # 第一张图
    def n51(n1):
        n1 = n[int(h / 12 * 3.5):int(h / 12 * 8.5), 0:int(w / 25 * 5)]
        cv2.imwrite(
            'n1.tif', n1,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n1.tif', '五联画A50x50.tif')
    # 第二张图
    def n52(n2):
        n2 = n[int(h / 12):int(h / 12 * 11), int(w / 25 * 5):int(w / 25 * 10)]
        cv2.imwrite(
            'n2.tif', n2,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n2.tif', '五联画B50x100.tif')
    # 第三张图
    def n53(n3):
        n3 = n[0:h, int(w / 25 * 10):int(w / 25 * 15)]
        cv2.imwrite(
            'n3.tif', n3,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n3.tif', '五联画C50x120.tif')
    # 第四张图
    def n54(n4):
        n4 = n[int(h / 12):int(h / 12 * 11), int(w / 25 * 15):int(w / 25 * 20)]
        cv2.imwrite(
            'n4.tif', n4,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n4.tif', '五联画D50x100.tif')
    # 第五张图
    def n55(n5):
        n5 = n[int(h / 12 * 3.5):int(h / 12 * 8.5), int(w / 25 * 20):w]
        cv2.imwrite(
            'n5.tif', n5,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n5.tif', '五联画E50x50.tif')
    argsn = [n, h, w]
    threads = []
    t1 = threading.Thread(target=n51, args=(argsn, ))
    threads.append(t1)
    t2 = threading.Thread(target=n52, args=(argsn, ))
    threads.append(t2)
    t3 = threading.Thread(target=n53, args=(argsn, ))
    threads.append(t3)
    t4 = threading.Thread(target=n54, args=(argsn, ))
    threads.append(t4)
    t5 = threading.Thread(target=n55, args=(argsn, ))
    threads.append(t5)
    for t in threads:
        t.setDaemon = True
        t.start()
# 五联画250x120(50x80 50x100 50x120 50x100 50x80)
elif k == '11':
    h = int(img.shape[1] / 25 * 12)
    n = cv2.resize(img, (w, h), interpolation=cv2.INTER_AREA)
    # 第一张图
    def n51(n1):
        n1 = n[int(h / 12 * 2):int(h / 12 * 10), 0:int(w / 25 * 5)]
        cv2.imwrite(
            'n1.tif', n1,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n1.tif', '五联画A50x80.tif')
    # 第二张图
    def n52(n2):
        n2 = n[int(h / 12):int(h / 12 * 11), int(w / 25 * 5):int(w / 25 * 10)]
        cv2.imwrite(
            'n2.tif', n2,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n2.tif', '五联画B50x100.tif')
    # 第三张图
    def n53(n3):
        n3 = n[0:h, int(w / 25 * 10):int(w / 25 * 15)]
        cv2.imwrite(
            'n3.tif', n3,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n3.tif', '五联画C50x120.tif')
    # 第四张图
    def n54(n4):
        n4 = n[int(h / 12):int(h / 12 * 11), int(w / 25 * 15):int(w / 25 * 20)]
        cv2.imwrite(
            'n4.tif', n4,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n4.tif', '五联画D50x100.tif')
    # 第五张图
    def n55(n5):
        n5 = n[int(h / 12 * 2):int(h / 12 * 10), int(w / 25 * 20):w]
        cv2.imwrite(
            'n5.tif', n5,
            (cv2.IMWRITE_TIFF_RESUNIT, 2, cv2.IMWRITE_TIFF_COMPRESSION, 5,
             cv2.IMWRITE_TIFF_XDPI, 72, cv2.IMWRITE_TIFF_YDPI, 72))
        os.rename('n5.tif', '五联画E50x80.tif')
    argsn = [n, h, w]
    threads = []
    t1 = threading.Thread(target=n51, args=(argsn, ))
    threads.append(t1)
    t2 = threading.Thread(target=n52, args=(argsn, ))
    threads.append(t2)
    t3 = threading.Thread(target=n53, args=(argsn, ))
    threads.append(t3)
    t4 = threading.Thread(target=n54, args=(argsn, ))
    threads.append(t4)
    t5 = threading.Thread(target=n55, args=(argsn, ))
    threads.append(t5)
    for t in threads:
        t.setDaemon = True
        t.start()
os.system('pause')

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

ReLoading 发表于 2022-2-21 18:25
这代码写的  啧啧啧
简直不忍直视
写个函数 包裹起来就那么累吗?
自带的线程池不用,偏要用线程
服了
 楼主| smileat2000 发表于 2022-2-21 19:38
ReLoading 发表于 2022-2-21 18:25
这代码写的  啧啧啧
简直不忍直视
写个函数 包裹起来就那么累吗?

不会啊Python小白
写这个纯属是为了方便工作,一直是用PS切割图的,但是又不准确,又慢,还容易做错
能不能教教我啊
kof21411 发表于 2022-2-21 22:15
大量的相同代码,应该把重复代码抽离出来写成一个函数,把图片规格写成参数,统一调用一个函数
 楼主| smileat2000 发表于 2022-2-22 12:43
kof21411 发表于 2022-2-21 22:15
大量的相同代码,应该把重复代码抽离出来写成一个函数,把图片规格写成参数,统一调用一个函数

能示范一下吗
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

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

GMT+8, 2024-11-25 16:36

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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