Python:从pdf中提取页面作为jpeg [英] Python: Extract a page from a pdf as a jpeg

查看:1041
本文介绍了Python:从pdf中提取页面作为jpeg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In python code, how to efficiently save a certain page in a pdf as a jpeg file? (Use case: I've a python flask web server where pdf-s will be uploaded and jpeg-s corresponding to each page is stores.)

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

This solution is close, but the problem is that it does not convert the entire page to jpeg.

推荐答案

可以使用pdf2image库。

The pdf2image library can be used.

您只需使用

pip install pdf2image

安装完成后,您可以使用以下代码来获取图像。

Once installed you can use following code to get images.

from pdf2image import convert_from_path
pages = convert_from_path('pdf_file', 500)

以jpeg格式保存页面

Saving pages in jpeg format

for page in pages:
    page.save('out.jpg', 'JPEG')






编辑:Github repo pdf2i mage 还提到它使用 pdftoppm 并且它需要其他安装:


the Github repo pdf2image also mentions that it uses pdftoppm and that it requires other installations:


pdftoppm是实现真正魔力的软件。它作为更大的软件包的一部分分发,名为 poppler
Windows用户必须安装 Windows的poppler
Mac用户必须安装适用于Mac的poppler
Linux用户将预装了发行版的pdftoppm(在Ubuntu和Archlinux上测试),如果不是,请运行 sudo apt install poppler-utils

以下是Windows的正确安装: http://blog.alivate.com.au/poppler-windows/

Here is the proper installation for Windows: http://blog.alivate.com.au/poppler-windows/

这篇关于Python:从pdf中提取页面作为jpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆