用python实现把PDF转成图片,测试成功转化代码

import os
import fitz

def covert2pic(file_path, zoom, png_path):
    doc = fitz.open(file_path)
    total = doc.page_count
    for pg in range(total):
        page = doc[pg]
        zoom = int(zoom)  # 值越大,分辨率越高,文件越清晰
        rotate = int(0)

        trans = fitz.Matrix(zoom / 100.0, zoom / 100.0).prerotate(rotate)
        pm = page.get_pixmap(matrix=trans, alpha=False)
        if not os.path.exists(png_path):
            os.mkdir(png_path)
        save = os.path.join(png_path, '%s.png' %(pg+1))
        pm.save(save)
    doc.close()

if __name__ == "__main__":
    pdfPath = r'C:\Users\Thinkpad\Desktop\100\3证CE.pdf'
    imagePath = r'C:\Users\Thinkpad\Desktop\100\imgs'
    covert2pic(pdfPath, 200, imagePath)

你可能感兴趣的:(python)