欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

上一篇:

下一篇: