python棒错误“;wand.resource.DestroyedResourceError:<;wand.image.image:(关闭)>;已经被摧毁了;

python棒错误“;wand.resource.DestroyedResourceError:<;wand.image.image:(关闭)>;已经被摧毁了;,python,python-3.x,pdf,wand,Python,Python 3.x,Pdf,Wand,我正在尝试运行一个脚本,它将遍历一个特定的文件夹,并使用Wand创建一个.png文件,其中包含它在该文件夹中找到的每个.pdf文件 from wand.image import Imageimport ospdf_dir = r"D:\Program Files\Python\Python36-32\tom's shitty programs\Downloads"for x in os.listdir(pdf_dir): if x.endswith(".pdf"): pdf_

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

from wand.image import Image
import os

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

for x in 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) as png:
for page in pdf.sequence:
png.composite(page, 0, page_index * height)
page_index += 1
png.save(filename=pdf_path[:-3] + "png") 从wand.image导入image导入os

pdf\u dir=r“D:\Program Files\Python\Python36-32\tom's shitty programs\Downloads”
作为pdf:
页面索引=0
高度=pdf.height
带图像(宽度=pdf.width,高度=len(pdf.sequence)*高度)作为png:
用于pdf中的页面。序列:
png.composite(页面,0,页面索引*高度)
页面索引+=1
png.save(文件名=pdf\path)[:-3]+“png”) 这将返回以下错误:

回溯(最近一次呼叫最后一次):

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

文件“D:\Program Files\Python\Python36-32\tom's shitty programs\venv\lib\site packages\wand\resource.py”,第151行,位于resource Rase DestroyedResourceError(repr(self)+“已销毁”)

wand.resource.DestroyedResourceError:已销毁

在处理上述异常期间,发生了另一个异常:

回溯(最近一次呼叫最后一次):

文件“D:/Program Files/Python/Python36-32/tom's shitty programs/wand_test.py”,第13行,图像(宽度=pdf.width,高度=len(pdf.sequence)*高度)为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:已关闭

非常感谢您的帮助…谢谢您这只是一些小问题和/或打字错误。正如错误消息所述,您正在尝试在关闭资源后使用变量(
pdf
)。请仔细检查缩进

图像(文件名=pdf\u路径,分辨率=300)为pdf:
页码索引=0
高度=pdf.height
图像(宽度=pdf.width,高度=len(pdf.sequence)*高度)为png:
对于pdf.sequence中的页面:
png.composite(第0页,第页索引*高度)
页码索引+=1
保存(文件名=pdf_路径[:-3]+“png”)
如果您使用的是Wand的0.5.0版,那么您可能能够利用该方法

图像(文件名=pdf\u路径,分辨率=300)为pdf:
pdf.concat(真)
保存(文件名=pdf_路径[:-3]+“png”)