简体   繁体   English

使用 Python 从 PDF 中提取高分辨率图像

[英]Extract images from PDF in high resolution with Python

I have managed to extract images from several PDF pages with the below code, but the resolution is quite low.我设法使用以下代码从几个 PDF 页面中提取图像,但分辨率很低。 Is there a way to adjust that?有没有办法调整它?

import fitz    
pdffile = "C:\\Users\\me\\Desktop\\myfile.pdf"
doc = fitz.open(pdffile)
for page_index in range(doc.pageCount):
    page = doc.loadPage(page_index)  
    pix = page.getPixmap()
    output = "image_page_" + str(page_index) + ".jpg"
    pix.writePNG(output)

I have also tried using the code here and updated if pix.n < 5" to "if pix.n - pix.alpha < 4 but this didn't output any images in my case.我也尝试使用此处的代码并将 if pix.n < 5" 更新为 "if pix.n - pix.alpha < 4 但这在我的情况下没有输出任何图像。

As stated in this issue for PyMuPDF, you have to use a matrix: issue on Github .如本期 PyMuPDF 中所述,您必须在 Github 上使用矩阵:问题

The example given is:给出的例子是:

zoom = 2    # zoom factor
mat = fitz.Matrix(zoom, zoom)
pix = page.getPixmap(matrix = mat, <...>)

Indicated in the issue is also that the default resolution is 72 dpi if you don't use a matrix which likely explains your getting low resolution.问题中还指出,如果您不使用可能解释分辨率低的矩阵,则默认分辨率为 72 dpi。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM