当前位置: 移动技术网 > IT编程>脚本编程>Python > ubuntu环境下利用Wand将pdf转jpg, python代码

ubuntu环境下利用Wand将pdf转jpg, python代码

2020年07月11日  | 移动技术网IT编程  | 我要评论

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网