Miss designer asked to quickly segment the sequence diagram
#! /usr/bin/env python
# - *- coding: utf-8-*-
# @ Time :2020/11/1812:51
# @ Author : ywy
# @ Platform:from tkinter.filedialog import*import windnd
from tkinter.messagebox import showerror,showinfo
from PIL import Image
def splitimage(src, rownum, colnum):
src =src.strip()
rownum =int(rownum)
colnum =int(colnum)
img = Image.open(src)
w, h = img.size
if rownum <= h and colnum <= w:print('Original image info: %sx%s, %s, %s'%(w, h, img.format, img.mode))print('Start processing image cutting,Please wait...')
s = os.path.split(src)
fn = s[1].split('.')
basename = fn[0]
ext = fn[-1]
dstpath=basename
if not os.path.exists(dstpath):
os.makedirs(dstpath)
num =0
rowheight = h // rownum
colwidth = w // colnumfor r inrange(rownum):for c inrange(colnum):
box =(c * colwidth, r * rowheight,(c +1)* colwidth,(r +1)* rowheight)
img.crop(box).save(os.path.join(dstpath, f'{basename}_{num}.{ext}'), ext)
num = num +1print('After the picture is cut, a total of generated%s small pictures.'% num)return f'After the picture is cut, a total of generated{num}A small picture is stored in the current program directory{dstpath}Under folder'else:print('Illegal row and column cutting parameters!')return'Illegal row and column cutting parameters!'
root_1 =Tk()
rownum =StringVar()
colnum =StringVar()
z =StringVar()
root_1.title('Picture cutting')
count =Label(root_1, text='Number of cutting rows')
count.grid(row=0, column=0)
enter_1 =Entry(root_1, state='normal', textvariable=rownum,bd=2,width=50)
enter_1.grid(row=0, column=1)
count =Label(root_1, text='Number of cutting columns')
count.grid(row=1, column=0)
enter_2 =Entry(root_1, state='normal', textvariable=colnum,bd=2,width=50)
enter_2.grid(row=1, column=1)
def dragged_files(files):
src =files[0].decode('gbk')
rownum = enter_1.get()
colnum = enter_2.get()print(src,rownum,colnum)if rownum and colnum:showinfo('prompt',splitimage(src,rownum,colnum))else:showerror('错误prompt','The number of cutting rows and the number of cutting columns must have values')
count =Label(root_1, text='Drag and drop your picture to the window')
count.grid(row=2, column=0)
windnd.hook_dropfiles(root_1,func=dragged_files)
root_1.mainloop()