如果我执行以下操作
for root, dirs, files in os.walk(myDir):
for myFile in files:
with Image(filename=myFile) as img:
with Image(image=img) as main:
print main.sequence[0].width
我最终使用 Wand 出现内存故障。
我确定它是 .sequence 部分。如果我删除它,它的罚款。我已经阅读了所有我能找到的序列,它是一个 Image 还是 SingleImage。
SingleImage 序列部分保留在内存中。我尝试使用以下内容:
main.sequence[0].destroy()
但它并没有摆脱内存中的图像。
我正在处理数以千计的文件,但仅仅处理了几十个文件后,我就遇到了段错误。
我很确定它关闭了“主”图像。只是不是 main.sequence SingleImage。
有没有办法强行关闭它?
我应该说我也试过这个
with Image(image=img.sequence[0]) as main:
认为 With 语句会间接关闭它。但事实并非如此。
任何人都可以帮忙吗?
最佳答案
第一件事 - 提交 bug with wand . wand.image.Image.destroy
不清理 wand.image.Sequence
如果分配了图像序列。好找!
您对 main.sequence[0].destroy()
完全正确;但是,您只能释放第一个分配的 SingleImage
在序列中。所以img.sequence[1:]
还在内存中设置。一个不太优雅的解决方案是迭代并销毁所有 SingleImage
的。
for root, dirs, files in os.walk(myDir):
for myFile in files:
with Image(filename=myFile) as img:
with Image(image=img) as main:
first = True
for frame in main.sequence:
if first:
print frame.width
first = False
frame.destroy()
评论:从文件中读取图像到
img
, 将数据复制到 main
,并按序列创建子图像似乎非常占用内存。我确定您所做的不仅仅是识别图像宽度,但这可以重写吗? Imagemagick 确实有一个 ping不将图像数据读入内存的方法(尚未在 wand
中实现)。
关于imagemagick - Python-Wand 序列未从内存中清除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30100956/