有的pdf电子书或者图片,特别是漫画类的经常是一个页面两个画面,而且右边开始阅读,相当于直接扫描了书一样。在手机或者平板电脑不方便观看,为了解决这个问题,写了py一个小程序,可以批量将图片进行从中间分割并将分割后的图片组成pdf电子书。注:这个排序方式是从分割图片的右侧开始排序。 永久分享链接 链接: https://pan.baidu.com/s/141NKjz-lWBFW0cVaq_8BNA?pwd=1234 提取码: 1234 图片, 程序
dazebat 发表于 2024-4-25 21:26 无源码。。。。紫薯布丁 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()