首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从pdf中提取页面作为jpeg

从pdf中提取页面作为jpeg
EN

Stack Overflow用户
提问于 2017-09-12 19:44:53
回答 17查看 287.6K关注 0票数 160

在python代码中,如何有效地将某个页面保存在pdf中作为jpeg文件?(用例:我有一个python烧瓶web服务器,其中pdf-s将被上传,与每个页面对应的jpeg-s是存储的。)

这个解决方案是接近的,但问题是它没有将整个页面转换为jpeg。

EN

Stack Overflow用户

发布于 2021-01-17 08:17:28

下面是一个函数,用于将PDF文件一页或多页转换为单一合并的JPEG图像

代码语言:javascript
复制
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")

票数 3
EN
查看全部 17 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46184239

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档