Is it possible to average thousands of images with ImageMagick?
Is it possible to average thousands of images with ImageMagick?
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".
-
- 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?
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.
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
- 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?
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.
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?
My OS Info:
The ImageMagick Version 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
- 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?
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.
or using a list rather than an arr
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
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
-
- 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?
If they all fit into memory at the same time, you can do it like this:
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.
Code: Select all
convert rose: -duplicate 19999 -evaluate-sequence Mean out.png
Instead of "rose: -duplicate 19999", you would list your input files, possibly with a wildcard.
snibgo's IM pages: im.snibgo.com