Open stepbystepcode opened 1 week ago
图片看起来太不方便了
自动把图片做成pdf
添加pdf单选框
我写的python脚本,测试可以正常使用 pip3 install fpdf2 tqdm pillow
pip3 install fpdf2 tqdm pillow
import os from fpdf import FPDF from PIL import Image from tqdm import tqdm def generate_pdf_from_images(): pdf = FPDF(unit="pt") with tempfile.TemporaryDirectory() as temp_dir: image_files = sorted( [f for f in os.listdir(temp_folder) if f.endswith(".webp")], key=lambda x: (int(x.split('-')[0]), int(x.split('-')[1].split('.')[0])) ) for img_name in tqdm(image_files, desc="Generating PDF"): img_path = os.path.join(temp_folder, img_name) if os.path.getsize(img_path) == 0: print(f"Warning: {img_name} is empty, skipping.") continue with Image.open(img_path) as image: image = image.convert("RGB") image_width, image_height = image.size pdf.add_page(format=(image_width, image_height)) pdf.set_auto_page_break(False) img_converted_path = os.path.join(temp_folder, f"converted_{img_name}.jpg") image.save(img_converted_path, "JPEG") pdf.image(img_converted_path, 0, 0, image_width, image_height) pdf.output("output.pdf") print(f"PDF successfully generated: output.pdf")
我这个脚本是基于python3.12,在前面要添加变量temp_floder="temp_images",且脚本同目录下有temp_images目录,里面的文件为a-b.webp的格式,a为每话次序号,b为图片序号,如1-00001.webp
原因
图片看起来太不方便了
功能简述
自动把图片做成pdf
功能逻辑
添加pdf单选框
实现参考
我写的python脚本,测试可以正常使用
pip3 install fpdf2 tqdm pillow