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
将:
改为:
本文地址:https://blog.csdn.net/u013403054/article/details/107257272
希望与广大网友互动??
点此进行留言吧!