python pdf与图片互转

背景

PDF转图片网上免费的只能转三次,多次的要么下载安装,要么收费。故自己写代码转换。

环境

1、安装python
2、 安装工具包


pip install PyMuPDF
pip install PIL
pip install os
pip img2pdf

代码

1、PDF转图片

封装转换方法

def pyMuPDF_fitz(pdfPath, imagePath):
    startTime_pdf2img = datetime.datetime.now()  # 开始时间
    print("imagePath=" + imagePath)
    pdfDoc = fitz.open(pdfPath)
    for pg in range(pdfDoc.pageCount):
        page = pdfDoc[pg]
        rotate = int(0)
  # 每个尺寸的缩放系数为1.3,这将为我们生成分辨率提高2.6的图像。
 # 此处设置系数越大,分辨率越高,默认图片大小为:792X612, dpi=96
        zoom_x = 10  # (1.33333333-->1056x816)   (2-->1584x1224)
        zoom_y = 10
        mat = fitz.Matrix(zoom_x, zoom_y).preRotate(rotate)
        pix = page.getPixmap(matrix=mat, alpha=False)
        if not os.path.exists(imagePath):  # 判断存放图片的文件夹是否存在
        os.makedirs(imagePath)  # 若图片文件夹不存在就创建
        pix.writePNG(imagePath + '/' + 'images_%s.png' % pg)  # 将图片写入指定的文件夹内

    endTime_pdf2img = datetime.datetime.now()  # 结束时间
    print('pdf2img时间=', (endTime_pdf2img - startTime_pdf2img).seconds)

使用示例

if __name__ == "__main__":
    # 1、PDF地址
    pdfPath =  r'C:\Users\zl\Desktop\img\行程单.pdf';
    # 2、需要储存图片的目录
    imagePath = r'C:\Users\zl\Desktop\img'
    pyMuPDF_fitz(pdfPath, imagePath)
    print("finish")

2、图片转PDF

封装转换方法

from PIL import Image
import os
import img2pdf

def pictureTopdf(dirname,savedirname,reductionFactor,filename,isConvertBlack,fileformat):
    files = os.listdir(dirname)
    for fname in files:
        # if not fname.endswith(".jpg"):
        if not fname.endswith(fileformat):
            continue
        path = os.path.join(dirname, fname)
        savePath = os.path.join(savedirname, fname)
        if os.path.isdir(path):
            continue
        img = Image.open(path)
        if img.size[0] > img.size[1]:
            im_rotate = img.rotate(90, expand=True)
            size = (int(im_rotate.size[0] / reductionFactor), int(im_rotate.size[1] / reductionFactor))
            im_rotate = im_rotate.resize(size)
            if isConvertBlack:
                im_rotate = im_rotate.convert("L")
            im_rotate.save(savePath, quality=95)
        else:
            size = (int(img.size[0] / reductionFactor), int(img.size[1] / reductionFactor))
            img = img.resize(size)
            if isConvertBlack:
                img = img.convert("L")
            img.save(savePath, quality=95)

    with open(filename + ".pdf", "wb") as f:
        imgs = []
        files = os.listdir(savedirname)
        for fname in files:
            if not fname.endswith(fileformat):
                continue
            path = os.path.join(savedirname, fname)
            if os.path.isdir(path):
                continue
            imgs.append(path)
        f.write(img2pdf.convert(imgs))  #写出到代码文件的默认目录

使用示例

if __name__ == '__main__':
    # 源目录
    dirname = r'C:\Users\zl\Desktop\img'
    # 保存目录
    savedirname = r'C:\Users\zl\Desktop\img'
    files = os.listdir(dirname)
    # 压缩比例
    reductionFactor = 1
    # PDF保存名字
    filename = "didi"
    # 输出颜色版本
    isConvertBlack = 'N'.strip().upper() == "Y"
    # 源文件格式
    fileformat = ".png"

    pictureTopdf(dirname,savedirname,reductionFactor,filename,isConvertBlack,fileformat)

你可能感兴趣的:(python,开发语言)