可以轉的庫有:wand、PyMuPDF、pdf2image、PythonMagick
wand和PyMuPDF沒找到二進制數據轉圖片的接口
PythonMagick沒有測試成功
pdf2image轉換速度很快,且支持二進制數據直接轉圖片,所以選擇pdf2image
pdf2image需要poppler和pillow的支持
pip install pillow
windows下安裝pippler:
下載地址:http://blog.alivate.com.au/poppler-windows/
然后將bin/文件夾添加到PATH
注意:配置之后需要重啟一下電腦才會生效
centos下安裝pippler:
yum install poppler poppler-cpp-devel poppler-utils
轉換代碼:
from pdf2image import convert_from_path,convert_from_bytes
# pdf的二進制數據轉圖片
images = convert_from_bytes(resp.content)
for i, image in enumerate(images):
image.save("第%d頁.png" % i, "PNG")
# pdf文件轉圖片
images = convert_from_path(pdf_path)
for i, image in enumerate(images):
image.save("第%d頁.png" % i, "PNG")
參考:
https://blog.csdn.net/zbj18314469395/article/details/98329442
https://blog.51cto.com/11142243/2299706