本帖最后由 naonao211 于 2024-11-2 16:34 编辑
2024.11.02更新,增加图片合并功能,可指定合并页数(多少页合并到一个图片),以及合并方向:横向合并或纵向合并
代码如下:
[Python] 纯文本查看 复制代码 import os
from PyPDF2 import PdfReader
from pdf2image import convert_from_path
from PIL import Image
def convert_pdf_to_images(pdf_path, output_folder):
# 创建输出文件夹如果不存在
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# 打开PDF文件
with open(pdf_path, 'rb') as file:
reader = PdfReader(file)
num_pages = len(reader.pages)
# 遍历每一页
for page_number in range(num_pages):
# 将PDF页面转换为图像
images = convert_from_path(pdf_path, first_page=page_number + 1, last_page=page_number + 1)
image = images[0]
# 保存图像
image_filename = f"{output_folder}/{page_number + 1:03d}.png"
image.save(image_filename, "PNG")
print(f"Saved {image_filename}")
def merge_images(image_files, output_path, num_per_merge, direction):
images = [Image.open(img) for img in image_files]
widths, heights = zip(*(i.size for i in images))
if direction == 1: # 横向合并
total_width = sum(widths)
max_height = max(heights)
else: # 竖向合并
total_width = max(widths)
max_height = sum(heights)
new_im = Image.new('RGB', (total_width, max_height))
if direction == 1: # 横向合并
x_offset = 0
for im in images:
new_im.paste(im, (x_offset, 0))
x_offset += im.size[0]
else: # 竖向合并
y_offset = 0
for im in images:
new_im.paste(im, (0, y_offset))
y_offset += im.size[1]
new_im.save(output_path)
print(f"合并后的图片保存在: {output_path}")
def main():
num_per_merge = int(input("输入多少个页面合并到一个图片中,例如2,则每2页合并为一个图片: "))
direction = int(input("横向合并输入1,竖向合并输入2: "))
pdf_files = [f for f in os.listdir('.') if f.endswith('.pdf')]
for pdf_file in pdf_files:
output_folder = f'转换后_{pdf_file[:-4]}'
convert_pdf_to_images(pdf_file, output_folder)
image_files = sorted([f"{output_folder}/{img}" for img in os.listdir(output_folder)])
for i in range(0, len(image_files), num_per_merge):
merge_images(image_files[i:i + num_per_merge], f"{output_folder}/merged_{i // num_per_merge + 1}.png",
num_per_merge, direction)
if __name__ == "__main__":
main()
效果图:
前两天看到有人发pdf转图片的软件,正好自己也在学python,就用Python写了一个,它可以自动检测当前py文件目录下的pdf文件,然后逐页转换成png格式,并存储到目录下的“转换后的文件夹”,文件名以001,002,003...命名。效果图:
注意:pdf2image库需要安装Poppler,安装后需要重启代码编辑器
代码:[Python] 纯文本查看 复制代码 import os
from PyPDF2 import PdfReader
from pdf2image import convert_from_path
def convert_pdf_to_images(pdf_path, output_folder):
# 创建输出文件夹如果不存在
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# 打开PDF文件
with open(pdf_path, 'rb') as file:
reader = PdfReader(file)
num_pages = len(reader.pages)
# 遍历每一页
for page_number in range(num_pages):
# 将PDF页面转换为图像
images = convert_from_path(pdf_path, first_page=page_number + 1, last_page=page_number + 1)
image = images[0]
# 保存图像
image_filename = f"{output_folder}/{page_number + 1:03d}.png"
image.save(image_filename, "PNG")
print(f"Saved {image_filename}")
# 获取当前目录下所有PDF文件
pdf_files = [f for f in os.listdir('.') if f.endswith('.pdf')]
# 转换每个PDF文件
for pdf_file in pdf_files:
convert_pdf_to_images(pdf_file, '转换后')
目录和名字之类的大家可以修改成自己需要的。
|