[Python] 纯文本查看 复制代码
import os
import cv2
import subprocess
def read(): # 读取图片
for file in os.listdir(os.getcwd()):
if file.endswith("png") or 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 cmd(shell): # 不显示控制台窗口
subprocess.call(shell,
creationflags=0x00000008)
return
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 re(names): # 删除
i = 0
while i < len(names):
os.remove(names[i])
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.五联画1100x75(30x45 30x60 30x75 30x60 30x45)
9.五联画200x100(40x60 40x80 40x100 40x80 40x60)
10.五联画250x120(50x80 50x100 50x120 50x100 50x80)
请选择:""")
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]), ('A40x60.tif', 'B80x60.tif', 'C40x60.tif'))
cmd('magick A40x60.tif B80x60.tif C40x60.tif -bordercolor black -resize 10% ABCDE.png')
cmd(
'magick -background None -gravity center xc:none[100x] ABCDE-0.png xc:none[100x] ABCDE-1.png xc:none[100x] ABCDE-2.png xc:none[100x] +append A.png')
re(('ABCDE-0.png', 'ABCDE-1.png',
'ABCDE-2.png'))
case '2': # 三联画200x70(100x70 100x70 100x70)
n = rsz(img, 20000, 7000)
qg((n[0:7000, 0:5000], n[0:7000, 5000:15000], n[0:7000, 15000:20000]),
('A100x70.tif', 'B100x70.tif', 'C100x70.tif'))
cmd('magick A100x70.tif B100x70.tif C100x70.tif -bordercolor black -resize 10% ABCDE.png')
cmd(
'magick -background None -gravity center xc:none[100x] ABCDE-0.png xc:none[100x] ABCDE-1.png xc:none[100x] ABCDE-2.png xc:none[100x] +append A.png')
re(('ABCDE-0.png', 'ABCDE-1.png',
'ABCDE-2.png'))
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'))
cmd('magick A60x80.tif B120x80.tif C60x80.tif -bordercolor black -resize 10% ABCDE.png')
cmd(
'magick -background None -gravity center xc:none[100x] ABCDE-0.png xc:none[100x] ABCDE-1.png xc:none[100x] ABCDE-2.png xc:none[100x] +append A.png')
re(('ABCDE-0.png', 'ABCDE-1.png',
'ABCDE-2.png'))
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'))
cmd('magick A70x90.tif B140x90.tif C70x90.tif -bordercolor black -resize 10% ABCDE.png')
cmd(
'magick -background None -gravity center xc:none[100x] ABCDE-0.png xc:none[100x] ABCDE-1.png xc:none[100x] ABCDE-2.png xc:none[100x] +append A.png')
re(('ABCDE-0.png', 'ABCDE-1.png',
'ABCDE-2.png'))
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': # 五联画1100x75(30x45 30x60 30x75 30x60 30x45)
n = rsz(img, 15000, 7500)
qg((n[1500:6000, 0:3000], n[750:6750, 3000:6000], n[0:7500, 6000:9000], n[750:6750, 9000:12000],
n[1500:6000, 12000:15000]), ('A30x45.tif', 'B30x60.tif', 'C30x75.tif', 'D30x60.tif', 'E30x45.tif'))
cmd('magick A30x45.tif B30x60.tif C30x75.tif D30x60.tif E30x45.tif -bordercolor black -resize 10% ABCDE.png')
cmd('magick -background None -gravity center xc:none[100x] ABCDE-0.png xc:none[100x] ABCDE-1.png xc:none[100x] ABCDE-2.png xc:none[100x] ABCDE-3.png xc:none[100x] ABCDE-4.png xc:none[100x] +append A.png')
re(('ABCDE-0.png', 'ABCDE-1.png',
'ABCDE-2.png', 'ABCDE-3.png', 'ABCDE-4.png'))
case '9': # 五联画200x100(40x60 40x80 100x100 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'))
cmd('magick A40x60.tif B40x80.tif C40x100.tif D40x80.tif E40x60.tif -bordercolor black -resize 10% ABCDE.png')
cmd('magick -background None -gravity center xc:none[100x] ABCDE-0.png xc:none[100x] ABCDE-1.png xc:none[100x] ABCDE-2.png xc:none[100x] ABCDE-3.png xc:none[100x] ABCDE-4.png xc:none[100x] +append A.png')
re(('ABCDE-0.png', 'ABCDE-1.png',
'ABCDE-2.png', 'ABCDE-3.png', 'ABCDE-4.png'))
case '10': # 五联画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'))
cmd('magick A50x80.tif B50x100.tif C50x120.tif D50x100.tif E50x80.tif -bordercolor black -resize 10% ABCDE.png')
cmd('magick -background None -gravity center xc:none[100x] ABCDE-0.png xc:none[100x] ABCDE-1.png xc:none[100x] ABCDE-2.png xc:none[100x] ABCDE-3.png xc:none[100x] ABCDE-4.png xc:none[100x] +append A.png')
re(('ABCDE-0.png', 'ABCDE-1.png',
'ABCDE-2.png', 'ABCDE-3.png', 'ABCDE-4.png'))