How to keep original picture before img.difference operation

I am using the in_memory_advanced_frame_differencing script and I would like to save the original picture that was obtained before the image subtraction.

So far my workaround was to take another snapshot after the trigger but this results in an unwanted delay.

Currently, I added this to the script:

if(triggered):
    img.save("triggered_%d.jpg" % frame_count)

But I find that the original img image seems to have been altered by calling the difference function? I use other languages where the difference result would have to be explicitly assigned to an object for that kind of behavior so I am clueless and haven’t found an easy way to do save my original img…

Hi, we don’t have memory to create a copy object on every call. So, all image manipulation methods unless they say they copy modify the image they are called on.

You need to allocate another frame buffer and copy the image to it to avoid this.

Excellent, I allocated space in the frame buffer:
img_ori_fb = sensor.alloc_extra_fb(sensor.width(), sensor.height(), sensor.RGB565)

And stored the image before the difference operation:
img_ori_fb.replace(img)

to then export the original:
img_ori_fb.save(“detection_%d.jpg” % detection_count,quality=90)

Thanks!