SciTech-EECS-Library:
Python 的 pdf 与 image 双向转换库
安装有关 Python 库
apt-get install -y qpdf poppler
# 必须先安装 poppler library 和 qpdf linrary
pip3 install img2pdf pdf2image Pillow
pdf 转 image
Approach:
- Import the
pdf2image
module - Store a PDF file with convert_from_path()
- Save image with save()
Below is the Implementation.
# import module
from pdf2image import convert_from_path# Store Pdf with convert_from_path function
images = convert_from_path('example.pdf')# Save pages as images in the pdf
for i in range(len(images)):images[i].save('page'+ str(i) +'.jpg', 'JPEG')
image 转 pdf
# importing necessary libraries
import os,sys, img2pdf
from PIL import Image# storing path
img_path = "./do_nawab.png"
pdf_path = "./file.pdf"with Image.open(img_path) as image:pdf_bytes = img2pdf.convert(image.filename)with open(pdf_path, "wb") as pdf_file:pdf_file.write(pdf_bytes)