This commit is contained in:
parent
921d00d627
commit
16c159d8bf
|
@ -1,6 +0,0 @@
|
|||
imagesPerRow=3
|
||||
rowsPerPage=1
|
||||
imageWidth=60
|
||||
imageHeight=130
|
||||
xStart=10
|
||||
yStart=20
|
|
@ -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)
|
||||
|
|
Binary file not shown.
|
@ -2,7 +2,9 @@
|
|||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$" />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
|
|
|
@ -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 文件。
|
|
@ -1,6 +0,0 @@
|
|||
imagesPerRow=3
|
||||
rowsPerPage=2
|
||||
imageWidth=60
|
||||
imageHeight=130
|
||||
xStart=10
|
||||
yStart=20
|
Binary file not shown.
Loading…
Reference in New Issue