diff --git a/main/config.txt b/main/config.txt deleted file mode 100644 index 5f2fa11..0000000 --- a/main/config.txt +++ /dev/null @@ -1,6 +0,0 @@ -imagesPerRow=3 -rowsPerPage=1 -imageWidth=60 -imageHeight=130 -xStart=10 -yStart=20 \ No newline at end of file diff --git a/main/create_pdf.py b/main/create_pdf.py index ae0cc70..2c06881 100644 --- a/main/create_pdf.py +++ b/main/create_pdf.py @@ -2,6 +2,8 @@ import shutil import sys import zipfile import os +import re + from PIL import Image from fpdf import FPDF @@ -14,7 +16,8 @@ def read_config(config_path): 'imageWidth': 60, 'imageHeight': 130, 'xStart': 10, - 'yStart': 20 + 'yStart': 20, + 'sortOrder': 0 } # 如果配置文件不存在,返回默认配置 @@ -29,6 +32,10 @@ def read_config(config_path): config[name.strip()] = int(value.strip()) return config +def extract_numbers(filename): + """从文件名中提取所有数字并连接起来""" + numbers = re.findall(r'\d+', filename) + return int(''.join(numbers)) if numbers else 0 def collect_images(src_dir, tmp_dir): os.makedirs(tmp_dir, exist_ok=True) @@ -63,8 +70,11 @@ def process_images_to_pdf(tmp_dir, output_pdf_path, config): # 收集所有支持格式的图像文件 extracted_files = [f for f in os.listdir(tmp_dir) if f.endswith(supported_formats)] - # Sort the files in descending order based on the timestamp in the filename - sorted_files_descending = sorted(extracted_files, key=lambda x: int(x.split('.')[0]), reverse=True) + # 根据配置文件中的 sortOrder 参数确定排序顺序 + reverse_order = True if config['sortOrder'] == 0 else False + + # 使用正则表达式提取文件名中的数字并进行排序 + sorted_files = sorted(extracted_files, key=lambda x: extract_numbers(x), reverse=reverse_order) # Create a PDF document with images arranged in rows, three images per row, six images per page, # proportionally scaled @@ -99,7 +109,7 @@ def process_images_to_pdf(tmp_dir, output_pdf_path, config): ratio = min(max_width / width, max_height / height) return width * ratio, height * ratio - for idx, image_file in enumerate(sorted_files_descending): + for idx, image_file in enumerate(sorted_files): if idx % max_images_per_page == 0: pdf.add_page() current_x = x_start @@ -131,7 +141,7 @@ def main(): config_path = os.path.join(current_dir, 'config.txt') tmp_dir = os.path.join(current_dir, 'tmp') output_dir_path = os.path.join(current_dir, 'out') - origin_dir_path = os.path.join(current_dir, 'orgin') + origin_dir_path = os.path.join(current_dir, 'origin') # 读取配置文件 config = read_config(config_path) diff --git a/main/orgin/Fw_1718199817706 - 副本.zip b/main/origin/Fw_1718199817706 - 副本.zip similarity index 100% rename from main/orgin/Fw_1718199817706 - 副本.zip rename to main/origin/Fw_1718199817706 - 副本.zip diff --git a/main/orgin/Fw_1718199817706.zip b/main/origin/Fw_1718199817706.zip similarity index 100% rename from main/orgin/Fw_1718199817706.zip rename to main/origin/Fw_1718199817706.zip diff --git a/main/out/output.pdf b/main/out/output.pdf deleted file mode 100644 index 7cb3d8d..0000000 Binary files a/main/out/output.pdf and /dev/null differ diff --git a/pdfGenerator.iml b/pdfGenerator.iml index ad3c0a3..456e24f 100644 --- a/pdfGenerator.iml +++ b/pdfGenerator.iml @@ -2,7 +2,9 @@ - + + + diff --git a/res/README.txt b/res/README.txt deleted file mode 100644 index d8c1d75..0000000 --- a/res/README.txt +++ /dev/null @@ -1,43 +0,0 @@ -图片收集与PDF生成脚本 -======================== - -该脚本用于从 `origin` 目录及其子目录中收集图片,并生成包含所有收集到的图片的 PDF 文件。支持的图片格式包括 `.jpg`, `.jpeg`, `.png`, `.tiff`, `.bmp`, 和 `.gif`。 - -文件结构 -======== - -确保你的目录结构如下 -/origin -# 包含一些压缩文件和图片 -/out -config.txt # 可选 -create_pdf.exe - - -使用说明 -======== - -配置文件 (config.txt) ---------------------- - -你可以在项目根目录中创建一个 `config.txt` 文件,以自定义 PDF 生成的布局参数。默认参数如下: -imagesPerRow=3 -rowsPerPage=2 -imageWidth=60 -imageHeight=130 -xStart=10 -yStart=20 - -这些参数的含义如下: - -- `imagesPerRow`: 每行图片的数量 -- `rowsPerPage`: 每页的行数 -- `imageWidth`: 每张图片的宽度比例 -- `imageHeight`: 每张图片的最大高度 -- `xStart`: 图片在页面上的起始 x 坐标 -- `yStart`: 图片在页面上的起始 y 坐标 - -运行脚本 --------- - -双击运行 `create_pdf.exe`。脚本将读取配置文件中的参数(如果存在),否则使用默认参数,收集 `origin` 目录及其子目录中(包含压缩包)的所有支持格式的图片并生成 PDF 文件。 diff --git a/res/config.txt b/res/config.txt deleted file mode 100644 index 8086db4..0000000 --- a/res/config.txt +++ /dev/null @@ -1,6 +0,0 @@ -imagesPerRow=3 -rowsPerPage=2 -imageWidth=60 -imageHeight=130 -xStart=10 -yStart=20 \ No newline at end of file diff --git a/res/create_pdf.exe b/res/create_pdf.exe deleted file mode 100644 index 55568bd..0000000 Binary files a/res/create_pdf.exe and /dev/null differ