Python-Wand Sequence Not Clearing From Memory
Asked Answered
V

1

7

If I do the following

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

I end up with memory faults using Wand.

I'm sure its the .sequence part. If I remove that, its fine. I've read all I can find on sequence, how its an Image vs SingleImage.

The SingleImage sequence part stays in memory. I've tried to use the following:

main.sequence[0].destroy()

but it does not get rid of the image in the memory.

I'm processing thousands of files, but after just a few dozen I get segmentation faults.

I'm pretty sure its closing the 'main' Image. Just not the main.sequence SingleImage.

Is there a way to forcibly close that?

I should say I've also tried this

with Image(image=img.sequence[0]) as main:

thinking the With statement would close it indirectly. But it does not.

Can anyone help?

Votyak answered 7/5, 2015 at 12:20 Comment(1)
You may reference to an answer here for some tips: #44210361Weakkneed
L
1

First things first - file a bug with wand. The wand.image.Image.destroy is not cleaning up wand.image.Sequence in the event an image sequence was allocated. Good find!

You are absolutely correct with main.sequence[0].destroy(); however, your only freeing the first allocated SingleImage in the sequence. So img.sequence[1:] is still setting in memory. A not-so-elegant solution would be to iterate & destroy all SingleImage's.

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()

comment: Reading an image from file to img, copying the data to main, and creating sub-images in a sequence seems very memory intensive. I'm sure your doing a lot more than identifying the image width, but can that be rewritten? Imagemagick does have a ping method ( not yet implemented in wand ) which doesn't read image data into memory.

Looselimbed answered 7/5, 2015 at 16:10 Comment(2)
I can try to file a bug report. However you workaround does not work. Even if I try to destroy each one individually, I still get huge memory use. It appears I didn't initially notice this, because once you hit the limit of your RAM, it starts to store images in /tmp. It wasn't until I ran out of disk space that I discovered this problem. Anyway, is there a way to destroy ALL open images (Images and SingleImages) currently open. I could just 'refresh/restart' at each loop. It works if I restart the script, how can I clear everything from the script at the start of each loop?Votyak
@Votyak Did you find a way?Restriction

© 2022 - 2024 — McMap. All rights reserved.