python-3.x - 无法在谷歌云功能中使用 Wand/ImageMagick 加载 PDF

标签 python-3.x pdf imagemagick google-cloud-functions wand

尝试从本地文件系统加载 PDF 并收到“未授权”错误。

"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 时发生

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

_, 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 的 PDF 阅读已被禁用 Ghostscript had .该问题是设计使然,ImageMagick 团队的安全缓解措施将一直存在。 ImageMagick 再次启用 PDF 的 Ghostscript 处理,Google Cloud Functions 更新到新版本的 ImageMagick,再次启用 PDF 处理。

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

requirements.txt

google-cloud-storage
ghostscript==0.6

主.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)

希望这对面临同样问题的人有所帮助。

关于python-3.x - 无法在谷歌云功能中使用 Wand/ImageMagick 加载 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55484465/

相关文章:

ruby - 粗鲁的工作不好。该怎么办?

imagemagick - 截取嵌入式 Linux 帧缓冲区的屏幕截图

php - 构造 ImagickPixel 时设置 alpha channel

python - 根据两个字典对列表进行排序

android - 如何在 MySQL 上从 Android (Java) 下载 PDF

php - 将 SVG 转换为 PDF 时出现 TCPDF 自定义字体问题

pdf - 使用 datatables.net 导出为 pdf 时设置列宽

python - 工作池数据结构

python - 将图像从 pygame 转换为 PIL 图像

python - 在 Python 上对不同深度的列表子列表进行排列,同时保持它们独立