在python代码中,如何有效地将某个页面保存在pdf中作为jpeg文件?(用例:我有一个python烧瓶web服务器,其中pdf-s将被上传,与每个页面对应的jpeg-s是存储的。)
这个解决方案是接近的,但问题是它没有将整个页面转换为jpeg。
发布于 2021-01-17 08:17:28
下面是一个函数,用于将PDF文件与一页或多页转换为单一合并的JPEG图像。
import os
import tempfile
from pdf2image import convert_from_path
from PIL import Image
def convert_pdf_to_image(file_path, output_path):
# save temp image files in temp dir, delete them after we are finished
with tempfile.TemporaryDirectory() as temp_dir:
# convert pdf to multiple image
images = convert_from_path(file_path, output_folder=temp_dir)
# save images to temporary directory
temp_images = []
for i in range(len(images)):
image_path = f'{temp_dir}/{i}.jpg'
images[i].save(image_path, 'JPEG')
temp_images.append(image_path)
# read images into pillow.Image
imgs = list(map(Image.open, temp_images))
# find minimum width of images
min_img_width = min(i.width for i in imgs)
# find total height of all images
total_height = 0
for i, img in enumerate(imgs):
total_height += imgs[i].height
# create new image object with width and total height
merged_image = Image.new(imgs[0].mode, (min_img_width, total_height))
# paste images together one by one
y = 0
for img in imgs:
merged_image.paste(img, (0, y))
y += img.height
# save merged image
merged_image.save(output_path)
return output_path示例用法:-
convert_pdf_to_image("path_to_Pdf/1.pdf", "output_path/output.jpeg")
https://stackoverflow.com/questions/46184239
复制相似问题