ubuntu环境下利用Wand将pdf转jpg, python代码
程序员文章站
2024-01-05 14:57:34
转换函数如下:```pythondef pdf2jpg(filename): savepath = 'tmpfile' if not os.path.exists(savepath): os.makedirs(savepath) name = os.path.splitext(os.path.basename(filename))[0] with wandImage(filename=filename, resol...
python 转换函数如下:
from wand.image import Image as wandImage
def pdf2jpg(filename):
savepath = 'tmpfile'
if not os.path.exists(savepath):
os.makedirs(savepath)
name = os.path.splitext(os.path.basename(filename))[0]
with wandImage(filename=filename, resolution=300) as img:
num = len(img.sequence)
with img.convert('jpg') as converted:
converted.save(filename=os.path.join('%s/%s.jpg' % (savepath, name)))
images = []
filelist = []
if num == 1:
imagename = os.path.join('%s/%s.jpg' % (savepath, name))
image = cv2.imread(imagename)
images.append(image)
filelist.append(imagename)
else:
for k in range(num):
imagename = os.path.join('%s/%s-%d.jpg' % (savepath, name, k))
image = cv2.imread(imagename)
images.append(image)
filelist.append(imagename)
return images, filelist
wand依赖库的安装:
pip3 install Wand
apt-get install libmagickwand-dev
转换过程可能出现的问题:
wand.exceptions.PolicyError: not authorized `/tmp/xxx.pdf’ Ubuntu PDF转图片错误
解决办法:
vim /etc/ImageMagick-6/policy.xml
将:
<policy domain="coder" rights="none" pattern="PDF" />
改为:
<policy domain="coder" rights="read|write" pattern="PDF" />
本文地址:https://blog.csdn.net/u013403054/article/details/107257272