在 python 中,无需重新采样即可从 PDF 中提取图像? [英] Extract images from PDF without resampling, in python?

查看:21
本文介绍了在 python 中,无需重新采样即可从 PDF 中提取图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何以原始分辨率和格式从 pdf 文档中提取所有图像?(意思是将 tiff 提取为 tiff,将 jpeg 提取为 jpeg 等,无需重新采样).布局并不重要,我不在乎源图像是否位于页面上.

How might one extract all images from a pdf document, at native resolution and format? (Meaning extract tiff as tiff, jpeg as jpeg, etc. and without resampling). Layout is unimportant, I don't care were the source image is located on the page.

我使用的是 python 2.7,但如果需要也可以使用 3.x.

I'm using python 2.7 but can use 3.x if required.

推荐答案

您可以使用 PyMuPDF 模块.这会将所有图像输出为 .png 文件,但开箱即用且速度很快.

You can use the module PyMuPDF. This outputs all images as .png files, but worked out of the box and is fast.

import fitz
doc = fitz.open("file.pdf")
for i in range(len(doc)):
    for img in doc.getPageImageList(i):
        xref = img[0]
        pix = fitz.Pixmap(doc, xref)
        if pix.n < 5:       # this is GRAY or RGB
            pix.writePNG("p%s-%s.png" % (i, xref))
        else:               # CMYK: convert to RGB first
            pix1 = fitz.Pixmap(fitz.csRGB, pix)
            pix1.writePNG("p%s-%s.png" % (i, xref))
            pix1 = None
        pix = None

见这里获取更多资源

这篇关于在 python 中,无需重新采样即可从 PDF 中提取图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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