[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'))