赞
踩
import fitz
imgdoc = fitz.open(img_file)
# imgdoc = fitz.open('png', img_bytes)
pdfbytes = imgdoc.convertToPDF()
with open(pdf_file, 'wb') as f:
f.write(pdfbytes)`
import fitz import os pdf_file = r'c:\Users\Name\Desktop\test.pdf' pdfDoc = fitz.open(pdf_file) # pdfDoc = fitz.open('pdf', io_bytes) page = pdfDoc[0] # 设置缩放和旋转系数 w = page.MediaBox.width h = page.MediaBox.height length = w if w>=h else h zoom = 1600/length # 将长边缩放到1600像素宽 trans = fitz.Matrix(zoom, zoom).preRotate(0) # 传递矩阵 clip_rate = (0.2,0.1,0.9,1) #剪切百分比(x_from_rate, y_from_rate, x_to_rate, y_to_rate) clip = (w*clip_rate[0],h*clip_rate[1], w*clip_rate[2], h*clip_rate[3]) pm = page.getPixmap(matrix=trans, alpha=False, clip=clip) img_path = os.path.join(os.path.splitext(pdf_file)[0] + '.png') # 开始写图像 pm.writePNG(img_path)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。