Page 1 of 1

Is it possible to average thousands of images with ImageMagick?

Posted: 2017-12-05T19:06:57-07:00
by ProGamer
Is it possible to average thousands of images with ImageMagick? Whenever I have tried in the past, I would get an error with something like "core dumped".

Re: Is it possible to average thousands of images with ImageMagick?

Posted: 2017-12-05T19:51:48-07:00
by snibgo
Yes, it is possible. If the images are small enough to fit into memory at the same time, a simple command will do that. (The practical limit, on my 12 GB laptop, is about 30,000 images.)

If the images won't fit into memory, you need to split them into batches. Find the average of each batch, then average those results.

Re: Is it possible to average thousands of images with ImageMagick?

Posted: 2017-12-05T20:09:25-07:00
by fmw42
You can write a script loop for you OS over each image and do a cumulative running average. See https://en.wikipedia.org/wiki/Moving_average.

Please always provide your IM version and platform, since syntax and scripting may differ.

Re: Is it possible to average thousands of images with ImageMagick?

Posted: 2017-12-06T22:29:43-07:00
by ProGamer
My OS Info:

Code: Select all

Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-1038-aws x86_64)

The ImageMagick Version Info:

Code: Select all

Version: ImageMagick 6.8.9-9 Q16 x86_64 2017-07-31 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2014 ImageMagick Studio LLC
Features: DPC Modules OpenMP
Delegates: bzlib cairo djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png rsvg tiff wmf x xml zlib

Re: Is it possible to average thousands of images with ImageMagick?

Posted: 2017-12-06T23:21:50-07:00
by fmw42
Here is a trivial example of a running average in bash unix shell script. It only needs two images in memory and the output at any one time. It averages 5 versions of the rose: image in arr, which if done correctly, will reproduce the rose: image. The -fill colorize creates a black image (zero value) of the same size as the rose: image to which the first rose: image is averaged at 100% for the rose and 0 for the black image. The averaging is done using -blend and a running pct value.

Code: Select all

convert rose: -fill black -colorize 100 tmp.miff
arr=(rose: rose: rose: rose: rose:)
num=${#arr[*]}
k=1
for ((i=0; i<num; i++)); do
pct=`convert xc: -format "%[fx:100/($k)]" info:`
echo "i=$i; pct=$pct; ${arr[$i]}"
convert tmp.miff "${arr[$i]}" -compose blend -set option:compose:args $pct -composite tmp.miff
k=$((k+1))
done
convert tmp.miff average.png
rm -f tmp.miff
or using a list rather than an arr

Code: Select all

convert rose: -fill black -colorize 100 tmp.miff
list="rose: rose: rose: rose: rose:"
k=1
for img in $list; do
pct=`convert xc: -format "%[fx:100/($k)]" info:`
echo "i=$i; pct=$pct; $img"
convert tmp.miff $img -compose blend -set option:compose:args $pct -composite tmp.miff
k=$((k+1))
done
convert tmp.miff average.png
rm -f tmp.miff

Re: Is it possible to average thousands of images with ImageMagick?

Posted: 2017-12-06T23:58:25-07:00
by snibgo
If they all fit into memory at the same time, you can do it like this:

Code: Select all

convert rose: -duplicate 19999 -evaluate-sequence Mean out.png
This (pointlessly) finds the average of 20,000 copies of "rose:". It takes 10 seconds on my laptop. (I suppose "-duplicate" works like "-clone", so it doesn't actually copy pixels to a new image unless the image is changed in some way.)

Instead of "rose: -duplicate 19999", you would list your input files, possibly with a wildcard.