Script to average images
Script to average images
Hi.
After some searching I've found this forum. What I want to do is take a series of images eg 1.jpg thro 999.jpg and average them:
1 thro 5
2 thro 6
3 thro 7
and so on.
I figure the script would use a command such as
convert -average P2.JPG P2.JPG P3.JPG P4.JPG P5.JPG new1.jpp
but I don't know enough about scripting to get this working, nor am I super familiar with ImageMagick!
Even better would be if the images were weighted during the calculation.
The hint say to post version: Version: ImageMagick 6.5.7-8 2012-04-30 Q16 http://www.imagemagick.org
Thanks
Woody
After some searching I've found this forum. What I want to do is take a series of images eg 1.jpg thro 999.jpg and average them:
1 thro 5
2 thro 6
3 thro 7
and so on.
I figure the script would use a command such as
convert -average P2.JPG P2.JPG P3.JPG P4.JPG P5.JPG new1.jpp
but I don't know enough about scripting to get this working, nor am I super familiar with ImageMagick!
Even better would be if the images were weighted during the calculation.
The hint say to post version: Version: ImageMagick 6.5.7-8 2012-04-30 Q16 http://www.imagemagick.org
Thanks
Woody
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Script to average images
The current command is
convert image1 ... imageN -evaluate-sequence mean result
But that would require you to have enough memory to hold all 999 images in memory, which sounds impractical.
The best way would be to do a loop over each image and do a running average.
It would be something like this in unix
numimages=4
convert 1.jpg tmpA.mpc
i=1
while [ $i -lt $numimages ]; do
j=`expr $i + 1`
echo "i=$i; j=$j"
convert $j.jpg tmpB.mpc
#compute running average percentages
new=`convert xc: -format "%[fx:100/$j]" info:`
old=`convert xc: -format "%[fx:100-$new]" info:`
composite -blend $old%x$new% -gravity center tmpA.mpc tmpB.mpc tmpA.mpc
i=`expr $i + 1`
done
convert tmpA.mpc result.jpg
rm tmpA.mpc tmpA.cache tmpB.mpc tmpb.cache
If you just want to average over 5 images at a time then
convert image1 ... image5 -evaluate-sequence result
should suffice
If you need to do that shifting the image by 1 each time, then write a loop that would take the first 5 and average and output, then on the next iteration of the loop increment each image by 1.
totnum=999
seqnum=5
num=$((totnum-seqnum))
i=1
while [ $i -le $num ]; do
i1=$i
i2=$((i+1))
i3=$((i+2))
i4=$((i+3))
i5=$((i+4))
convert $i1.jpg $i2.jpg $i3.jpg $i4.jpg $i5.jpg -evaluate-sequence mean result$i.jpg
i=$((i+1))
done
convert image1 ... imageN -evaluate-sequence mean result
But that would require you to have enough memory to hold all 999 images in memory, which sounds impractical.
The best way would be to do a loop over each image and do a running average.
It would be something like this in unix
numimages=4
convert 1.jpg tmpA.mpc
i=1
while [ $i -lt $numimages ]; do
j=`expr $i + 1`
echo "i=$i; j=$j"
convert $j.jpg tmpB.mpc
#compute running average percentages
new=`convert xc: -format "%[fx:100/$j]" info:`
old=`convert xc: -format "%[fx:100-$new]" info:`
composite -blend $old%x$new% -gravity center tmpA.mpc tmpB.mpc tmpA.mpc
i=`expr $i + 1`
done
convert tmpA.mpc result.jpg
rm tmpA.mpc tmpA.cache tmpB.mpc tmpb.cache
If you just want to average over 5 images at a time then
convert image1 ... image5 -evaluate-sequence result
should suffice
If you need to do that shifting the image by 1 each time, then write a loop that would take the first 5 and average and output, then on the next iteration of the loop increment each image by 1.
totnum=999
seqnum=5
num=$((totnum-seqnum))
i=1
while [ $i -le $num ]; do
i1=$i
i2=$((i+1))
i3=$((i+2))
i4=$((i+3))
i5=$((i+4))
convert $i1.jpg $i2.jpg $i3.jpg $i4.jpg $i5.jpg -evaluate-sequence mean result$i.jpg
i=$((i+1))
done
Last edited by fmw42 on 2012-07-09T12:07:03-07:00, edited 1 time in total.
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Script to average images
I did something similar but only composing two images.
I read in one list, added an 'blank' image on the end, opened parenthesis, cloned that list and insert that added image on the end to the beginning, then inserted a "null:" seperator image before closing parenthesis.
At this point you have two lists seperated by a "null:" image with the second list 'rolled' or displaced by one image.
You can now use -layers composite to merge the two lists using some "compose" method (like a 50% blend)
The result is each image averaged with the next, with the ends averaged with a 'blank' image, and all in one IM command.
But only two images have been merged together. With more work you can average more images together, but only two images at a time.
I have used this to generate images with 'symbols' indicating how the next and previous 'images' relates to the current image.
See -layers composite (compose two lists of images together in sequence)
http://www.imagemagick.org/Usage/anim_mods/#composite
YES this is in the Animations Area of IM examples, but animations are just lists of images!!!!
I read in one list, added an 'blank' image on the end, opened parenthesis, cloned that list and insert that added image on the end to the beginning, then inserted a "null:" seperator image before closing parenthesis.
At this point you have two lists seperated by a "null:" image with the second list 'rolled' or displaced by one image.
You can now use -layers composite to merge the two lists using some "compose" method (like a 50% blend)
The result is each image averaged with the next, with the ends averaged with a 'blank' image, and all in one IM command.
But only two images have been merged together. With more work you can average more images together, but only two images at a time.
I have used this to generate images with 'symbols' indicating how the next and previous 'images' relates to the current image.
See -layers composite (compose two lists of images together in sequence)
http://www.imagemagick.org/Usage/anim_mods/#composite
YES this is in the Animations Area of IM examples, but animations are just lists of images!!!!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: Script to average images
been offline for a couple days: OS is OSX, can do 10.6, 10.6 and 10.7
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Script to average images
The scripts I wrote above (untested for the most part) should work fine on Mac OSX. I do my devepment on Mac OSX Snow Leopard. But the scripts should work on any unix system.
Re: Script to average images
thanks!
Whats the best numbering scheme eg 1.jpg 2.jpg 3.jpg etc?
Whats the best numbering scheme eg 1.jpg 2.jpg 3.jpg etc?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Script to average images
Woody wrote:thanks!
Whats the best numbering scheme eg 1.jpg 2.jpg 3.jpg etc?
For an average it does not matter what order the images are in unless you need to see intermediate results and need them averaged in a special order. If you want you can add leading zeroes to your numbers to get them to be processed in order; otherwise, 1 and 10 are next to each other.
Re: Script to average images
I created a script from your code:
saved it as image_averager.sh
gave it execute permission i.e. chmod u+x
when I run the script I get
image_averager.sh: line 5: [: le: unary operator expected
Any ideas why? This is likely a noob question.... I am a noob
Code: Select all
#!/bin/bash
totnum=14
seqnum=5
num=$((totnum-seqnum))
while [ $i -le $num ]; do
i1=$i
i2=$((i+1))
i3=$((i+2))
i4=$((i+3))
i5=$((i+4))
convert $i1.jpg $i2.jpg $i3.jpg $i4.jpg $i5.jpg -evaluate-sequence mean result$i.jpg
i=$((i+1))
done
gave it execute permission i.e. chmod u+x
when I run the script I get
image_averager.sh: line 5: [: le: unary operator expected
Any ideas why? This is likely a noob question.... I am a noob

- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Script to average images
Before the while add
or perhaps i=1 depends on what number you images start at.
Code: Select all
i=0
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
Re: Script to average images
ahhh thank you!
OK, follow on question: can ImageMagick weight the images i.e. the highest numbered contributes say 50%, then the next 25%, then 12.5%, and then 2 lots of 6.25% (i.e. to give 100%) ?
OK, follow on question: can ImageMagick weight the images i.e. the highest numbered contributes say 50%, then the next 25%, then 12.5%, and then 2 lots of 6.25% (i.e. to give 100%) ?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Script to average images
see -evaluate multiply xx http://www.imagemagick.org/script/comma ... 2#evaluate
you will need to precompute for each numbered image what factor you want in some list or array. Then put each image in your average in parens \( x.jpg -evaluate multiply $xfactor \)
see
http://www.imagemagick.org/Usage/basics/#parenthesis
you will need to precompute for each numbered image what factor you want in some list or array. Then put each image in your average in parens \( x.jpg -evaluate multiply $xfactor \)
see
http://www.imagemagick.org/Usage/basics/#parenthesis
Re: Script to average images
The script runs fine on OSX / Lion, trying it on Linux / Ubuntu, but its not working, I get
convert: unrecognized option `-evaluate-sequence' @ convert.c/ConvertImageCommand/1347.
Any ideas?
convert: unrecognized option `-evaluate-sequence' @ convert.c/ConvertImageCommand/1347.
Any ideas?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Script to average images
your Linux version of IM is probably too old for the -evaluate-sequence mean (introduced about 6.6.0-4). try -average on the older systems.
Re: Script to average images
that works - thanks!
BTW In case you are wondering what I'm doing, I will share with you when its working end-end
BTW In case you are wondering what I'm doing, I will share with you when its working end-end
