lanyeeee / jmcomic-downloader

禁漫天堂 18comic.vip jmcomic 18comic 的多线程下载器,带图形界面,已打包exe,带收藏夹,免费下载收费的漫画,下载速度飞快
MIT License
260 stars 15 forks source link

[功能请求] 把下载的图片集合做成pdf #47

Open stepbystepcode opened 1 week ago

stepbystepcode commented 1 week ago

原因

图片看起来太不方便了

功能简述

自动把图片做成pdf

功能逻辑

添加pdf单选框

实现参考

我写的python脚本,测试可以正常使用 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")
stepbystepcode commented 1 week ago

我这个脚本是基于python3.12,在前面要添加变量temp_floder="temp_images",且脚本同目录下有temp_images目录,里面的文件为a-b.webp的格式,a为每话次序号,b为图片序号,如1-00001.webp