Python-Wand non-memory clearing sequence

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 

Ultimately, with memory errors, Wand is used.

I am sure this is part. If I remove it, it will be fine. I read everything that I can find in the sequence, like its image versus SingleImage.

Part of the SingleImage sequence remains in memory. I tried using the following:

 main.sequence[0].destroy() 

but he will not get rid of the image in memory.

I process thousands of files, but after several tens I get segmentation errors.

I am pretty sure that it covers the "main" image. Just not main.sequence SingleImage.

Is there a way to force this close?

I have to say I tried this too

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

thinking that the With statement will close it indirectly. But this is not so.

Can anyone help?

+6
source share
1 answer

First, first - the error file with the wand . wand.image.Image.destroy does not clear wand.image.Sequence if a sequence of images is selected. Good find!

You are absolutely right with main.sequence[0].destroy() ; however, your only release is the first highlighted SingleImage in the sequence. Therefore img.sequence[1:] is still set in memory. A not-so-elegant solution would be to SingleImage over and destroy all 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() 

comment: reading an image from a file on img , copying data to main , and creating sub-images in a sequence seems very intense in memory. I am sure that you are doing much more than determining the width of an image, but can this be rewritten? Imagemagick has a ping method (not yet implemented in wand ) that does not read image data into memory.

+1
source

Source: https://habr.com/ru/post/986740/


All Articles