[英]Convert PDF file to multipage image
我正在嘗試使用 PyMuPDF 將多頁 PDF 文件轉換為圖像:
pdffile = "input.pdf"
doc = fitz.open(pdffile)
page = doc.loadPage() # number of page
pix = page.getPixmap()
output = "output.tif"
pix.writePNG(output)
但是我需要將 PDF 文件的所有頁面轉換為多頁 tiff 中的單個圖像,當我給頁面參數一個頁面范圍時,它只需要一頁,有人知道我該怎么做嗎?
當您想轉換 PDF 的所有頁面時,您需要一個 for 循環。 此外,當您調用.getPixmap()
,您需要像matrix = mat
這樣的屬性來基本上提高您的分辨率。 這是代碼片段(不確定這是否是您想要的,但這會將所有 PDF 轉換為圖像):
doc = fitz.open(pdf_file)
zoom = 2 # to increase the resolution
mat = fitz.Matrix(zoom, zoom)
noOfPages = doc.pageCount
image_folder = '/path/to/where/to/save/your/images'
for pageNo in range(noOfPages):
page = doc.loadPage(pageNo) #number of page
pix = page.getPixmap(matrix = mat)
output = image_folder + str(pageNo) + '.jpg' # you could change image format accordingly
pix.writePNG(output)
print('Converting PDFs to Image ... ' + output)
# do your things afterwards
import fitz
from PIL import Image
input_pdf = "input.pdf"
output_name = "output.tif"
compression = 'zip' # "zip", "lzw", "group4" - need binarized image...
zoom = 2 # to increase the resolution
mat = fitz.Matrix(zoom, zoom)
doc = fitz.open(input_pdf)
image_list = []
for page in doc:
pix = page.getPixmap(matrix = mat)
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
image_list.append(img)
if image_list:
image_list[0].save(
output_name,
save_all=True,
append_images=image_list[1:],
compression=compression,
dpi=(300, 300),
)
import fitz
pdffile = "input.pdf"
doc = fitz.open(pdffile)
for page in doc:
pix = page.getPixmap()
output = "output.tif"
pix.save(output)
PyMuPDF 支持 output 的一組有限的圖像類型。 TIFF不在其中。
但是,有一種與 Pillow 接口的簡單方法,它支持多幀 TIFF output。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.