快刀图像均分器源代码.py

"""快刀图像均分器源代码.py,本程序会把文件夹下面所有图像进行切割,利用pillow做的图像分割,加上tkinter的可视化界面,输入行数和列数就能把大量的图像快速平均切分并保存好。"""

from tkinter import *
from tkinter import filedialog,messagebox
import os,time
from splitimage import *                # 图像切割模块,需要请和李兴球联系

def isimage(image):
    """通过判断扩展名来略微判断一个文件是否是图像,只支持列表中的图像"""
    ext = os.path.splitext(image)[-1] 
    if ext.lower() in [".gif",".jpg",".png",".jpeg",".bmp"]:
        return True
    else:
        return False
    
def checkfolder(*folder):
    for fd in folder:
        if not os.path.exists(fd): os.mkdir(fd)
    
game_name = "快刀图像均分器"
input_folder = os.getcwd() + os.sep + "images"    # 默认的输入文件夹 
output_folder = os.getcwd() + os.sep  + "output"  # 默认的输出文件夹 
ziti = ('',12,'normal')

window = Tk()
window.resizable(width = False,height = False)# 设置窗口不能变化大小
window.geometry("800x360")                    # 设置窗口的几何尺寸 
window.title(game_name)                       # 设置窗口的标题

label0 = Label(window,text=game_name,font=('',22,'bold'),fg='red')
label0.place(x=230,y=30) 

"输入文件夹代码段"
label_input = Label(window,text='输入文件夹:',font=ziti)
label_input.place(x=10,y=110)
text_input=Entry(window,width=60,font=ziti)
text_input.place(x=120,y=110)
text_input.insert(0,input_folder)

def select_input_folder():
    dirname = filedialog.askdirectory(parent=window,initialdir="/",title='请选择输入文件夹。')
    text_input.delete(0,END)      #删除所有内容
    text_input.insert(0,dirname)
button_input=Button(window,width=18,text='选择',font=ziti,command = select_input_folder)
button_input.place(x=620,y=110)


"输出文件夹代码段"
label_output = Label(window,text='输出文件夹:',font=ziti)
label_output.place(x=10,y=160)
text_output=Entry(window,width=60,font=ziti)
text_output.place(x=120,y=160)
text_output.insert(0,output_folder)

def select_output_folder():
    dirname = filedialog.askdirectory(parent=window,initialdir="/",title='请选择输出文件夹。')
    text_output.delete(0,END)      #删除所有内容
    text_output.insert(0,dirname)
def open_output_folder():
    os.system("explorer " + text_output.get())
    
button_output=Button(window,width=8,text='选择',font=ziti,command = select_output_folder)
button_output.place(x=620,y=160)
button_open_output = Button(window,width = 8 ,text = '打开',font = ziti,command = open_output_folder)
button_open_output.place(x=700,y=160)

"行数选择"
label_rows = Label(window,text = '行数:',font = ziti)
label_rows.place(x = 10 ,y = 210)
text_rows = Entry(window,width=6,font=ziti)
text_rows.place(x=120,y=210)
text_rows.insert(0,"2")
"列数选择"
label_cols = Label(window,text = '列数:',font = ziti)
label_cols.place(x = 10 ,y = 260)
text_cols = Entry(window,width=6,font=ziti)
text_cols.place(x=120,y=260)
text_cols.insert(0,"2")
    
def process_images():
    loginfo = ""
    button_split.config(state='disabled')  # 按钮有三种状态,'normal','active','disabled'
    label_status.config(text = '处理中')
    rows = int(text_rows.get())
    cols = int(text_cols.get())
    amounts = rows * cols
    input_path = text_input.get()
    output_path = text_output.get()
    checkfolder(input_path,output_path)    # 检查文件夹是否存在,如果不存在,则创建
    photos = [input_path  + os.sep + image  for image in os.listdir(input_path) if isimage(image)] # 加载图像
    error_amounts = 0
    for photo in photos:
        label_status.config(text = photo)
        a,b,c = split_image(photo,rows,cols,output_path)
        if len(a) != amounts :
            info = str(time.ctime()) + ",处理'" + photo + "'出错!"
            loginfo = loginfo + info + "\n\n"
            error_amounts = error_amounts + 1
        window.update()
    label_status.config(text = "处理完毕,共发生 " + str(error_amounts) + " 个错误。详情见‘错误记录.txt’文件。")
    button_split.config(state='normal')    
    f = open("错误记录.txt",mode='w')
    f.write(loginfo)
    f.close()

button_split = Button(window,width=8,text='开始处理',font=('',28,'normal'),command = process_images)
button_split.place(x=200,y=210)

explain_info = "本程序会把文件夹中每一个图像文件进行切分\n\n请在'行数'和'列数'文本框里输入数值。\n\n作者:李兴球 , www.scratch8.net"
button_explain = Button(window,width=8,text='使用说明',font=('',28,'normal'),command = lambda:messagebox.showinfo(game_name,explain_info))
button_explain.place(x=400,y=210)

label_status = Label(window,text = "",font = ziti)
label_status.place( x = 10,y = 300)

window.mainloop()