好友
阅读权限10
听众
最后登录1970-1-1
|
import os
from tkinter import Tk, filedialog, messagebox, Button, Label, StringVar, Frame
from tkinter.ttk import Progressbar
from PIL import Image
import img2pdf
from pathlib import Path
# 初始化Tkinter主窗口
root = Tk()
root.title("Image Splitter and PDF Creator")
# 设置窗口的最小大小
root.minsize(500, 300)
# 设置窗口的背景颜色
root.configure(bg="white")
# 用于存储选择的文件夹路径的变量
selected_folder = StringVar(root)
# 创建并配置进度条
progress_bar = Progressbar(root, orient="horizontal", length=300)
progress_bar.pack(pady=20, padx=60, fill="x")
# 全局变量
selected_path = None
split_image_paths = []
def split_and_sort_images():
if not selected_folder.get():
messagebox.showerror("Error", "Please select a folder first.")
return
global selected_path
selected_path = Path(selected_folder.get())
if not selected_path.is_dir():
messagebox.showerror("Error", "Selected path does not exist.")
return
# 遍历选定文件夹中的所有子文件夹
for subdir, dirs, files in os.walk(str(selected_path)):
for folder in dirs:
folder_path = Path(subdir) / folder
if folder_path.is_dir():
process_images_in_folder(folder_path)
# 处理完成后,显示消息
messagebox.showinfo("Success", "All images have been processed successfully!")
def process_images_in_folder(folder_path):
# 创建分割图片的文件夹
split_folder_name = f"split_images_{folder_path.name}"
split_folder_path = selected_path / split_folder_name
split_folder_path.mkdir(exist_ok=True, parents=True)
# 遍历文件夹中的所有图片文件
for file in os.listdir(folder_path):
if file.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):
process_image(Path(folder_path) / file, split_folder_path)
# 生成PDF
pdf_folder_path = selected_path / "pdfs"
pdf_folder_path.mkdir(exist_ok=True, parents=True)
pdf_save_path = pdf_folder_path / f"{folder_path.name}.pdf"
with open(pdf_save_path, "wb") as f:
f.write(img2pdf.convert(split_image_paths))
messagebox.showinfo("PDF Created", f"PDF file has been created: {pdf_save_path}")
def process_image(image_path, split_folder_path):
try:
with Image.open(image_path) as image:
width, height = image.size
if width > 1:
left_part = image.crop((0, 0, width // 2, height))
right_part = image.crop((width // 2, 0, width, height))
left_image_path = split_folder_path / f"{os.path.splitext(image_path.name)[0]}_left.jpg"
right_image_path = split_folder_path / f"{os.path.splitext(image_path.name)[0]}_right.jpg"
left_part.save(left_image_path)
right_part.save(right_image_path)
# 将分割后图片的路径添加到列表中
split_image_paths.extend([str(left_image_path), str(right_image_path)])
except IOError as e:
messagebox.showerror("Error", f"Error opening image {image_path}: {e}")
def select_folder():
folder_path = filedialog.askdirectory()
if folder_path:
selected_folder.set(folder_path)
progress_bar['value'] = 0 # 重置进度条
start_button.config(state="normal")
# 创建选择文件夹的按钮
select_button = Button(root, text="Select Folder", command=select_folder, font=("Helvetica", 12))
select_button.pack(pady=15)
# 创建开始处理按钮
start_button = Button(root, text="Start Processing", command=split_and_sort_images, state="disabled", font=("Helvetica", 12))
start_button.pack(pady=15)
# 进入Tkinter事件循环
root.mainloop()
|
免费评分
-
查看全部评分
|