Is it possible to average thousands of images with ImageMagick?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
ProGamer
Posts: 56
Joined: 2017-01-12T23:14:26-07:00
Authentication code: 1151

Is it possible to average thousands of images with ImageMagick?

Post 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".
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
ProGamer
Posts: 56
Joined: 2017-01-12T23:14:26-07:00
Authentication code: 1151

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

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
Post Reply