【问题标题】:python wand error "wand.resource.DestroyedResourceError: <wand.image.Image: (closed)> is destroyed already"python wand 错误“wand.resource.DestroyedResourceError: <wand.image.Image: (closed)> 已经被破坏”
【发布时间】:2019-01-15 02:52:40
【问题描述】:

我正在尝试运行一个脚本,该脚本将遍历特定文件夹并使用 Wand 创建它在该文件夹中找到的每个 .pdf 文件的 .png 文件。

from wand.image import Image
import os

pdf_dir = r"D:\Program Files\Python\Python36-32\tom's shitty programs\Downloads"

对于 x在 os.listdir(pdf_dir):
if x.endswith(".pdf"):
pdf_path = pdf_dir + '\\' + x
with Image(filename=pdf_path, resolution=300) as pdf:
page_index = 0
height = pdf.height
with Image(width=pdf.width, height=len(pdf.sequence)*height) 为 png:
用于 pdf 中的页面。序列:
png.composite(page, 0, page_index * height)
page_index += 1
png.save(filename=pdf_path[:-3] + "png")

这会返回以下错误:

Traceback(最近一次调用最后一次):

文件“D:\Program Files\Python\Python36-32\tom's shitty programs\venv\lib\site-packages\wand\image.py”,第 1799 行,在 wand 返回 self.resource

文件“D:\Program Files\Python\Python36-32\tom's shitty programs\venv\lib\site-packages\wand\resource.py”,第 151 行,资源引发 DestroyedResourceError(repr(self) + '已经被销毁了')

wand.resource.DestroyedResourceError: 已被销毁

在处理上述异常的过程中,又发生了一个异常:

Traceback(最近一次调用最后一次):

文件“D:/Program Files/Python/Python36-32/tom's shitty programs/wand_test.py”,第 13 行,带有 Image(width=pdf.width, height=len(pdf.sequence)*height)作为png:

文件“D:\Program Files\Python\Python36-32\tom's shitty programs\venv\lib\site-packages\wand\image.py”,第 1817 行,宽度返回库。MagickGetImageWidth(self.wand)

文件“D:\Program Files\Python\Python36-32\tom's shitty programs\venv\lib\site-packages\wand\image.py”,第 1801 行,在 wand raise ClosedImageError(repr(self) + '已经关门了')

wand.image.ClosedImageError: 已经关闭

感谢您的帮助...谢谢

【问题讨论】:

    标签: python python-3.x pdf wand


    【解决方案1】:

    这只是一些小的with ... as .. context manager 问题和/或拼写错误。正如错误消息所述,您正在尝试在资源已关闭后使用变量 (pdf)。仔细检查缩进。

    with Image(filename=pdf_path, resolution=300) as pdf:
        page_index = 0
        height = pdf.height
        with Image(width=pdf.width, height=len(pdf.sequence)*height) as png:
            for page in pdf.sequence:
                png.composite(page, 0, page_index * height)
                page_index += 1
            png.save(filename=pdf_path[:-3] + "png")
    

    如果您使用的是 0.5.0 版的 Wand,那么您也许可以利用 wand.image.Image.concat 方法。

    with Image(filename=pdf_path, resolution=300) as pdf:
        pdf.concat(True)
        pdf.save(filename=pdf_path[:-3] + "png")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-02
      • 1970-01-01
      • 2010-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-01
      • 1970-01-01
      相关资源
      最近更新 更多