赞
踩
现在确实有很多线上的工具可以把pdf文件转为图片,比如smallpdf等等,都很好用。但我们有时会碰到一些敏感数据,或者要批量去转,那么需要自己写脚本来实现,以下脚本可以提供这个功能~
def pdf2img(pdf_dir, result_path): if not os.path.exists(result_path): os.makedirs(result_path, exist_ok=True) pdf_path_list = glob.glob(os.path.join(pdf_dir, "*.pdf")) for pdf_path in tqdm(pdf_path_list): pdf_name = os.path.split(pdf_path)[1].split(".")[0] with fitz.open(pdf_path) as pdf: pdf_page_count = pdf.pageCount for pg in range(0, pdf_page_count): page = pdf[pg] mat = fitz.Matrix(2, 2) pm = page.getPixmap(matrix=mat, alpha=False) # if width or height > 2000 pixels, don't enlarge the image if pm.width > 2000 or pm.height > 2000: pm = page.getPixmap(matrix=fitz.Matrix(1, 1), alpha=False) img = Image.frombytes("RGB", [pm.width, pm.height], pm.samples) # img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) img = Image.fromarray(np.array(img)) img.save(os.path.join(result_path, f"{pdf_name}_{pg}.jpg"))
说明:
pdf_dir:pdf文件的路径
result_path:保存的路径
以上即可完成pdf文件转换为图片, Enjoy~
∼ O n e p e r s o n g o f a s t e r , a g r o u p o f p e o p l e c a n g o f u r t h e r ∼ \sim_{One\ person\ go\ faster,\ a\ group\ of\ people\ can\ go\ further}\sim ∼One person go faster, a group of people can go further∼
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。