Skip to content

Instantly share code, notes, and snippets.

@iqiancheng
Last active April 29, 2025 03:49
Show Gist options
  • Save iqiancheng/20ddccfcbe7d145f250805c4efa92179 to your computer and use it in GitHub Desktop.
Save iqiancheng/20ddccfcbe7d145f250805c4efa92179 to your computer and use it in GitHub Desktop.
image_to_pdf_converter
import img2pdf
import os
import re
from pathlib import Path
from typing import List, Optional
class ImageToPdfConverter:
"""将图片文件转换为高质量PDF的工具类"""
def __init__(self, image_folder: str, output_pdf: str, file_ext: str = '.jpg'):
self.image_folder = Path(image_folder)
self.output_pdf = Path(output_pdf)
self.file_ext = file_ext
def extract_number(self, filename: str) -> int:
"""从文件名中提取括号内的数字作为排序依据"""
match = re.search(r'\((\d+)\)', filename)
return int(match.group(1)) if match else float('inf')
def get_sorted_images(self) -> List[Path]:
"""获取并按序号排序的图片文件路径列表"""
if not self.image_folder.exists():
raise FileNotFoundError(f"文件夹 '{self.image_folder}' 不存在")
image_files = [f for f in self.image_folder.iterdir()
if f.is_file() and f.suffix.lower() == self.file_ext]
return sorted(image_files, key=lambda x: self.extract_number(x.name))
def convert(self) -> Optional[Path]:
"""执行转换过程,返回生成的PDF文件路径"""
try:
image_paths = self.get_sorted_images()
if not image_paths:
print(f"在 '{self.image_folder}' 中没有找到 {self.file_ext} 文件")
return None
# 确保输出目录存在
self.output_pdf.parent.mkdir(parents=True, exist_ok=True)
# 转换为PDF,保持原始质量
with open(self.output_pdf, "wb") as f:
f.write(img2pdf.convert([str(img) for img in image_paths]))
print(f"无损质量PDF已成功保存为 '{self.output_pdf}'")
return self.output_pdf
except Exception as e:
print(f"转换过程中出错: {e}")
return None
def main():
"""主函数"""
# 使用示例
image_folder = 'path_to_your_images' # 替换为实际图片文件夹路径
output_pdf = 'output.pdf' # 输出PDF文件名
converter = ImageToPdfConverter(image_folder, output_pdf)
converter.convert()
if __name__ == "__main__":
main()
from PIL import Image
import os
import re
# 设置图片文件夹路径
image_folder = 'path_to_your_images' # 替换为你的文件夹路径
output_pdf = 'output.pdf' # 输出的PDF文件名
# 正则提取括号内的数字
def extract_number(filename):
match = re.search(r'\((\d+)\)', filename)
return int(match.group(1)) if match else float('inf')
# 获取所有jpg文件,按括号内数字排序
image_files = sorted(
[f for f in os.listdir(image_folder) if f.endswith('.jpg')],
key=extract_number
)
# 打开所有图片
images = []
for file in image_files:
img_path = os.path.join(image_folder, file)
img = Image.open(img_path)
if img.mode != 'RGB':
img = img.convert('RGB')
images.append(img)
# 保存成PDF
images[0].save(output_pdf, save_all=True, append_images=images[1:])
print(f"PDF已保存为 {output_pdf}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment