吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

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

[求助] 不用if如何选项 判断

[复制链接]
smileat2000 发表于 2022-4-14 13:09
用来切割图片的
if太多了 求教有没有更简洁用来选择的方法
有没有更好的方法读取图片?
如何实现拖动图片到程序图标上自动读取拖入的图片?
[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.五联画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[i],ns[i],[cv2.IMWRITE_TIFF_RESUNIT,3,cv2.IMWRITE_TIFF_XDPI,100,cv2.IMWRITE_TIFF_YDPI,100])
        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[0:6000, 0:4000],n[0:6000, 4000:12000],n[0:6000, 12000:16000]),('A6x60.tif','B80x60.tif','C40x60.tif'))

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

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

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

# 四联画140x90(30x60 40x80 40x80 30x60)
elif k == '5':
    n=rsz(img,14000,9000)
    qg((n[2000:8000, 0:3000],n[1000:9000, 3000:7000],n[0:8000, 7000:11000],n[1000:7000,11000:14000]),('A30x60.tif','B40x80.tif','C40x80.tif','D30x60.tif'))

# 四联画180x110(40x80 50x100 50x100 40x80)
elif k == '6':
    n=rsz(img,18000,11000)
    qg((n[2000:10000, 0:4000],n[1000:11000, 4000:9000],n[0:10000,9000:14000],n[1000:9000,14000:18000]),('A40x80.tif','B50x100.tif','C50x100.tif','D40x80.tif'))

# 四联画220x130(50x100 60x120 60x120 50x100)
elif k == '7':
    n=rsz(img,22000,13000)
    qg((n[2000:12000, 0:5000],n[1000:13000, 5000:11000],n[0:12000, 11000:17000],n[1000:11000, 17000:22000]),('A50x100.tif','B60x120.tif','C60x120.tif','D50x100.tif'))

# 五联画180x90(36x50 36x70 36x90 36x70 36x50)
elif k == '8':
    n=rsz(img,18000,9000)
    qg((n[2000:7000, 0:3600],n[1000:8000, 3600:7200],n[0:9000, 7200:10800],n[1000:8000, 10800:14400],n[2000:7000, 14400:18000]),('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[3000:7000, 0:4000],n[1000:9000, 4000:8000],n[0:10000, 8000:12000],n[1000:9000, 12000:16000],n[3000:7000, 16000:20000]),('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[2000:8000, 0:4000],n[1000:9000, 4000:8000],n[0:10000, 8000:12000],n[1000:9000, 12000:16000],n[2000:8000, 16000:20000]),('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[0:10000, 0:4000],n[0:10000, 4000:8000],n[0:10000,8000:12000],n[0:10000, 12000:16000],n[0:10000, 16000:20000]),('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[3500:8500, 0:5000],n[1000:11000, 5000:10000],n[0:12000, 10000:15000],n[1000:11000, 15000:20000],n[3500:8500, 20000:25000]),('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[2000:10000, 0:5000],n[1000:11000, 5000:10000],n[0:12000, 10000:15000],n[1000:11000, 15000:20000],n[2000:10000, 20000:25000]),('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[3500:9500, 0:6000],n[0:12000,6000:10000],n[1000:13000, 10000:14000],n[0:12000,14000:18000],n[3500:9500, 18000:24000]),('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[6000:24000, 0:6000],n[1500:13500, 6000:12000],n[0:15000, 12000:18000],n[1500:13500, 18000:24000],n[3000:12000, 24000:30000]),('A60x90.tif','B60x120.tif','C60x150.tif','D60x120.tif','E60x90.tif'))

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

 楼主| smileat2000 发表于 2022-4-14 14:23
修改了一下 把if换成了match case
还有更好的方法提高效率吗?
读取图片有没有更好的方法呢?
[Python] 纯文本查看 复制代码
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[i], ns[i], [cv2.IMWRITE_TIFF_RESUNIT, 3,cv2.IMWRITE_TIFF_XDPI, 100, cv2.IMWRITE_TIFF_YDPI, 100])
        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[0:6000, 0:4000], n[0:6000, 4000:12000], n[0:6000,12000:16000]), ('A6x60.tif', 'B80x60.tif', 'C40x60.tif'))
    case '2':  # 三联画200x70(50x70 100x70 50x70)
        n = rsz(img, 20000, 7000)
        qg((n[0:7000, 0:5000], n[0:7000, 5000:15000], n[0:7000, 15000:20000]),('A50x70.tif', 'B100x70.tif', 'C50x70.tif'))
    case '3':  # 三联画240x80(60x80 120x80 60x80)
        n = rsz(img, 24000, 8000)
        qg((n[0:8000, 0:6000], n[0:8000, 6000:18000], n[0:8000, 18000:24000]),('A60x80.tif', 'B120x80.tif', 'C60x80.tif'))
    case '4':  # 三联画280x90(70x90 140x90 70x90)
        n = rsz(img, 28000, 9000)
        qg((n[0:9000, 0:7000], n[0:9000, 7000:21000], n[0:9000, 21000:28000]),('A70x90.tif', 'B140x90.tif', 'C70x90.tif'))
    case '5':  # 四联画140x90(30x60 40x80 40x80 30x60)
        n = rsz(img, 14000, 9000)
        qg((n[2000:8000, 0:3000], n[1000:9000, 3000:7000], n[0:8000, 7000:11000],n[1000:7000, 11000:14000]), ('A30x60.tif', 'B40x80.tif', 'C40x80.tif', 'D30x60.tif'))
    case '6':  # 四联画180x110(40x80 50x100 50x100 40x80)
        n = rsz(img, 18000, 11000)
        qg((n[2000:10000, 0:4000], n[1000:11000, 4000:9000], n[0:10000, 9000:14000],n[1000:9000, 14000:18000]), ('A40x80.tif', 'B50x100.tif', 'C50x100.tif', 'D40x80.tif'))
    case '7':  # 四联画220x130(50x100 60x120 60x120 50x100)
        n = rsz(img, 22000, 13000)
        qg((n[2000:12000, 0:5000], n[1000:13000, 5000:11000], n[0:12000, 11000:17000],n[1000:11000, 17000:22000]), ('A50x100.tif', 'B60x120.tif', 'C60x120.tif', 'D50x100.tif'))
    case '8':  # 五联画180x90(36x50 36x70 36x90 36x70 36x50)
        n = rsz(img, 18000, 9000)
        qg((n[2000:7000, 0:3600], n[1000:8000, 3600:7200], n[0:9000, 7200:10800], n[1000:8000, 10800:14400],n[2000:7000, 14400:18000]), ('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[3000:7000, 0:4000], n[1000:9000, 4000:8000], n[0:10000, 8000:12000], n[1000:9000, 12000:16000],n[3000:7000, 16000:20000]), ('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[2000:8000, 0:4000], n[1000:9000, 4000:8000], n[0:10000, 8000:12000], n[1000:9000, 12000:16000],n[2000:8000, 16000:20000]), ('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[0:10000, 0:4000], n[0:10000, 4000:8000], n[0:10000, 8000:12000], n[0:10000, 12000:16000],n[0:10000, 16000:20000]), ('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[3500:8500, 0:5000], n[1000:11000, 5000:10000], n[0:12000, 10000:15000], n[1000:11000, 15000:20000],n[3500:8500, 20000:25000]), ('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[2000:10000, 0:5000], n[1000:11000, 5000:10000], n[0:12000, 10000:15000], n[1000:11000, 15000:20000],n[2000:10000, 20000:25000]), ('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[3500:9500, 0:6000], n[0:12000, 6000:10000], n[1000:13000, 10000:14000], n[0:12000, 14000:18000],n[3500:9500, 18000:24000]), ('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[6000:24000, 0:6000], n[1500:13500, 6000:12000], n[0:15000, 12000:18000], n[1500:13500, 18000:24000],n[3000:12000, 24000:30000]), ('A60x90.tif', 'B60x120.tif', 'C60x150.tif', 'D60x120.tif', 'E60x90.tif'))
xiaoyu2032 发表于 2022-4-14 13:19
yhw231 发表于 2022-4-14 13:40
fan1191237272 发表于 2022-4-14 13:50
把if里面的逻辑放到一个对象或者数组,比如对象obj = {1: aaa, 2: bbb},用的时候就直接obj[k]

免费评分

参与人数 1吾爱币 +1 收起 理由
英雄初本涩 + 1 我很赞同!

查看全部评分

 楼主| smileat2000 发表于 2022-4-14 14:25
感觉控制台程序好麻烦啊!可是python做图形界面又要用到别的,打包出来exe文件会更大
我心飞翔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
[Python] 纯文本查看 复制代码
def patten(size:tuple, cut_param, name:tuple):
	def inner(img):
		n = rsz(img, *size)
		qg(tuple(n[i] 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
[mw_shl_code=python,true]def patten(size:tuple, cut_param, name:tuple):
        def inner(img):
                n = rsz ...

这个涉及到了哪些知识点啊 看不懂啊 我需要图片是无损处理的,要固定分辨率和压缩格式,文件格式
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

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

GMT+8, 2024-11-25 15:34

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

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