无法在Google Cloud Function中使用Wand/ImageMagick加载PDF [英] Can't load PDF with Wand/ImageMagick in Google Cloud Function

查看:141
本文介绍了无法在Google Cloud Function中使用Wand/ImageMagick加载PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图从本地文件系统加载PDF并收到未授权"错误.

Trying to load a PDF from the local file system and getting a "not authorized" error.

在读取self.raise_exception()文件的"/env/local/lib/python3.7/site-packages/wand/image.py"行4896中,文件"/env/local/lib/python3.7" /site-packages/wand/resource.py,第222行,在raise_exception中引发e wand.exceptions.PolicyError:未经授权的`/tmp/tmp_iq12nws'@ error/constitute.c/ReadImage/412

"File "/env/local/lib/python3.7/site-packages/wand/image.py", line 4896, in read self.raise_exception() File "/env/local/lib/python3.7/site-packages/wand/resource.py", line 222, in raise_exception raise e wand.exceptions.PolicyError: not authorized `/tmp/tmp_iq12nws' @ error/constitute.c/ReadImage/412

PDF文件已成功从GCS保存到本地服务器",但不会被Wand加载.将图像加载到OpenCV中不是问题,只是在尝试使用Wand/ImageMagick加载PDF时发生

The PDF file is successfully saved to the local 'server' from GCS but won't be loaded by Wand. Loading images into OpenCV isn't an issue, just happening when trying to load PDFs using Wand/ImageMagick

下面是将PDF从GCS加载到本地文件系统到Wand/ImageMagick的代码

Code to load the PDF from GCS to local file system into Wand/ImageMagick is below

_, temp_local_filename = tempfile.mkstemp()
gcs_blob = STORAGE_CLIENT.bucket('XXXX').get_blob(results["storedLocation"])
gcs_blob.download_to_filename(temp_local_filename)
# load the pdf into a set of images using imagemagick
with(Image(filename=temp_local_filename, resolution=200)) as source:
    #run through pages and save images etc.

应该授权ImageMagick访问本地文件系统上的文件,以便它应加载文件而不会出现问题,而不是出现此未授权"错误.

ImageMagick should be authorised to access files on the local filesystem so it should load the file without issue instead of this 'Not Authorised' error.

推荐答案

由于存在安全漏洞

PDF reading by ImageMagick has been disabled because of a security vulnerability Ghostscript had. The issue is by design and a security mitigation from the ImageMagick team will exist until. ImageMagick Enables Ghostscript processing of PDFs again and Google Cloud Functions update to that new version of ImageMagick with PDF processing enabled again.

我找不到GCF中的ImageMagick/Wand问题修复程序,但作为将PDF转换为Google Cloud Functions中图像的一种解决方法,您可以使用此[ghostscript包装器] [2]直接请求将PDF转换为通过Ghostscript图像并绕过ImageMagick/Wand.然后,您可以毫无问题地将PNG加载到ImageMagick或OpenCV中.

There's no fix for the ImageMagick/Wand issue in GCF that I could find but as a workaround for converting PDFs to images in Google Cloud Functions, you can use this [ghostscript wrapper][2] to directly request the PDF conversion to an image via Ghostscript and bypass ImageMagick/Wand. You can then load the PNGs into ImageMagick or OpenCV without issue.

requirements.txt

google-cloud-storage
ghostscript==0.6

main.py

    # create a temp filename and save a local copy of pdf from GCS
    _, temp_local_filename = tempfile.mkstemp()
    gcs_blob = STORAGE_CLIENT.bucket('XXXX').get_blob(results["storedLocation"])
    gcs_blob.download_to_filename(temp_local_filename)
    # create a temp folder based on temp_local_filename
    temp_local_dir = tempfile.mkdtemp()
    # use ghostscript to export the pdf into pages as pngs in the temp dir
    args = [
        "pdf2png", # actual value doesn't matter
        "-dSAFER",
        "-sDEVICE=pngalpha",
        "-o", temp_local_dir+"page-%03d.png",
        "-r300", temp_local_filename
        ]
    # the above arguments have to be bytes, encode them
    encoding = locale.getpreferredencoding()
    args = [a.encode(encoding) for a in args]
    #run the request through ghostscript
    ghostscript.Ghostscript(*args)
    # read the files in the tmp dir and process the pngs individually
    for png_file_loc in glob.glob(temp_local_dir+"*.png"):
        # loop through the saved PNGs, load into OpenCV and do what you want
        cv_image = cv2.imread(png_file_loc, cv2.IMREAD_UNCHANGED)

希望这可以帮助遇到相同问题的人.

Hope this helps someone facing the same issue.

这篇关于无法在Google Cloud Function中使用Wand/ImageMagick加载PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆