python – ImageMagick无权将PDF转换为图像

我有一个程序,我需要使用Image Magick将PDF转换为图像.我使用子进程包这样做:

        cmd = 'magick convert -density 300 '+pdfFile+'['+str(rangeTuple[0])+'-'+str(rangeTuple[1])+'] -depth 8 '+'temp.tiff' #WINDOWS
        if(os.path.isfile('temp.tiff')):
            os.remove('temp.tiff')
        subprocess.call(cmd,shell=True)
        im = Image.open('temp.tiff')

我得到的错误是:

convert-im6.q16: not authorized `temp2.pdf' @ error/constitute.c/ReadImage/412.
convert-im6.q16: no images defined `temp.tiff' @ error/convert.c/ConvertImageCommand/3258.
Traceback (most recent call last):
  File "UKExtraction2.py", line 855, in <module>
    doItAllUpper("A0","UK5.csv","temp",59,70,"box",2,1000,firstPageCoordsUK,boxCoordUK,voterBoxCoordUK,internalBoxNumberCoordUK,externalBoxNumberCoordUK,addListInfoUK)
  File "UKExtraction2.py", line 776, in doItAllUpper
    doItAll(tempPDFName,outputCSV,2,pdfs,formatType,n_blocks,writeBlockSize,firstPageCoords,boxCoord,voterBoxCoord,internalBoxNumberCoord,externalBoxNumberCoord,addListInfo,pdfName)
  File "UKExtraction2.py", line 617, in doItAll
    mainProcess(pdfName,(0,noOfPages-1),formatType,n_blocks,outputCSV,writeBlockSize,firstPageCoords,boxCoord,voterBoxCoord,internalBoxNumberCoord,externalBoxNumberCoord,addListInfo,bigPDFName,basePages)
  File "UKExtraction2.py", line 542, in mainProcess
    im = Image.open('temp.tiff')
  File "/home/rohit/.local/lib/python3.6/site-packages/PIL/Image.py", line 2609, in open
    fp = builtins.open(filename, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'temp.tiff'

其中最重要的是:

convert-im6.q16: not authorized `temp2.pdf' @ error/constitute.c/ReadImage/412.

我认为这是因为ImageMagick无权访问PDF.现在该怎么办?我在Linux服务器上.任何帮助表示赞赏.

最佳答案
emcconville是对的.更具体地说,编辑Imagemagick policy.xml文件以取消注释该行:

  <!-- <policy domain="module" rights="none" pattern="{PS,PDF,XPS}" /> -->

并将其从rights =“none”更改为rights =“read | write”

  <policy domain="module" rights="read|write" pattern="{PS,PDF,XPS}" />

由于Ghostscript代表中发现了安全漏洞,我认为这是对policy.xml文件的最新补充.我认为这个漏洞现在已经修复了当前版本的Ghostscript,即9.25.

注意:在某些系统上,策略行将具有domain =“coder”而不是domain =“module”

转载注明原文:python – ImageMagick无权将PDF转换为图像 - 代码日志