1. 程式人生 > 實用技巧 >借用GUI批量處理圖片

借用GUI批量處理圖片

import os
import tkinter as tk

from PIL import Image

"""
將python.py檔案打包為exe檔案
pip install pyinstaller
pyinstaller -F photo_process_gui.py
"""
dpi = 96
# 1 inch=2.54cm
width = 25.4 / 2.54
height = 19.05 / 2.54
width_pixel = width * dpi
height_pixel = height * dpi

window = tk.Tk()

entry = tk.Entry(window, width=20)
entry.pack()

entry2 
= tk.Entry(window, width=20) entry2.pack() # 修改圖片尺寸 def resize_image(filein, fileout, width, height): img = Image.open(filein) # 調整大小 img.thumbnail((width, height), Image.ANTIALIAS) # resize image with high-quality img.save(fileout) # 從GUI獲取輸入輸出資料夾 def change_state(): in_path
= entry.get() # 呼叫get()方法,將Entry中的內容獲取出來 out_path = entry2.get() print(in_path, out_path) files = os.listdir(in_path) for file in files: resize_image(os.path.join(in_path, file), os.path.join(out_path, file), width_pixel, height_pixel) button = tk.Button(window, text='執行', command=change_state).pack() window.mainloop()