应该是因为API更新,所以网上很多的教程都会报错。这里记录一下。
相关链接:https://blog.csdn.net/weixin_54537901/article/details/128126669
修正内容:
fitz.Matrix(zoom_x, zoom_y).preRotate(rotate)-->fitz.Matrix(zoom_x, zoom_y).prerotate(rotate)
page.getPixmap(matrix=trans, alpha=False)-->page.get_pixmap(matrix=trans, alpha=False)
pm.writePNG(pic_pwd)-->pm.save(pic_pwd)
PDF转图片的功能
import fitz
import os
# pdf路径
tmp = r"."
export_file = r"./output"
save_path = r"./output"
os.makedirs(export_file, exist_ok=True)
pdf_dir = [i for i in os.listdir(tmp) if os.path.splitext(i)[-1] == ".pdf"]
def pdf_to_jpg(name):
# 拼接pdf的文件路径
pwd_name = os.path.join(tmp, name)
with fitz.open(pwd_name) as doc:
# 将文件名同我们的保存路径拼接起来(保存图片的文件夹)
dir_name = os.path.splitext(name)[0]
pdf_name = os.path.join(export_file, dir_name)
# print(pdf_name)
temp = 0
# (保存图片的文件夹)不存咋则生成
# exsitsdir.judge(pdf_name)
os.makedirs(pdf_name, exist_ok=True)
for pg in range(doc.page_count):
page = doc[pg]
temp += 1
rotate = int(0)
# 每个尺寸的缩放系数为2,这将为我们生成分辨率提高四倍的图像。
zoom_x = 2.0
zoom_y = 2.0
trans = fitz.Matrix(zoom_x, zoom_y).prerotate(rotate)
pm = page.get_pixmap(matrix=trans, alpha=False)
pic_name = '{}.jpg'.format(temp)
# 拼接生成pdf的文件路径
pic_pwd = os.path.join(pdf_name, pic_name)
# print(pic_pwd)
pm.save(pic_pwd)
def main():
#多线程
# pool = Pool(2)
# for i in pdf_dir:
# res = pool.apply_async(pdf_to_jpg, (i,))
# pool.close()
# pool.join()
#单线程
for i in pdf_dir:
pdf_to_jpg(i)
if __name__ == '__main__':
main()