annotate photo with average brightness

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
prysm1
Posts: 2
Joined: 2016-07-18T06:59:32-07:00
Authentication code: 1151

annotate photo with average brightness

Post by prysm1 »

Hi all

I wrote a script below. It basically splits a photo into a 10x10 image and each components are measured for average brightness and annotated with
the brightness value. the components are then reassembled into a single image. An example output image is shown here https://www.flickr.com/gp/uberclacker/U6BL20

I was wondering if there was a better way to do this. I basically dont feel splitting is the right approach. Maybe there is a way to tell imagemagick to
work only on a certain region of a photo?

regards
prysm


---- start script ----
#!/bin/sh

awklib=/home/fernan/lib/awk
wff="awk -f $awklib/wf.awk"
tmpdir="/tmp"

if test $# -le 0
then
echo "usage: wf [command] [parameters]"
exit 1
fi
if test $1 = "z"
then
convert $2 -crop 10x10@ +repage +adjoin $tmpdir/tile_%02d.tiff
for FILE in $tmpdir/tile_*.tiff
do
d=`convert $FILE -colorspace gray -format %[fx:100*mean] info:`
z=`$wff z $d`
convert $FILE -fill white -undercolor '#00000080' -pointsize 70 -gravity South -annotate +0+5 $z $FILE
done
montage -mode concatenate -tile 10x $tmpdir/tile_*.tiff $3
rm $tmpdir/tile_*.tiff
fi
---- endscript -----
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: annotate photo with average brightness

Post by fmw42 »

You can get the average brightness of blocks of an image by -scale down getting the pixel values (which are the block averages) via txt:, then -scale up
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: annotate photo with average brightness

Post by snibgo »

You don't need to run convert for each image, because you can "-annotate" each of the crops. For example:

Code: Select all

convert wizard: -crop 10x10@ +repage -gravity south -fill lime -pointsize 10 -precision 4 -annotate +0+0 %[fx:mean*100] wiz-%06d.png
If you want the proper grayscale mean, you'll need to use the proper formula.

Sadly, "-annotate" needs the "+repage". If it didn't, you could reassemble the pieces within the same convert command.

I guess that in v7, it can be done somehow in a single command.
snibgo's IM pages: im.snibgo.com
prysm1
Posts: 2
Joined: 2016-07-18T06:59:32-07:00
Authentication code: 1151

Re: annotate photo with average brightness

Post by prysm1 »

thanks snibgo, fmw42

That single convert approach was cool. I will look into it further.
Unfortunately it will not work directly based on how I use the script. I currently annotate the image with a roman numeral based on a range of bightness for example 0 = 0 to 10, I = 11 to 20 etc.

Another thing I tried was using the command below. but it was much slower.
convert image.png -extract 0x0+100+100 -colorspace gray -format %[fx:100*mean] info:
convert image.png -fill white -pointsize 70 -annotate +0+0 ...
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: annotate photo with average brightness

Post by snibgo »

prysm1 wrote:I currently annotate the image with a roman numeral based on a range of bightness for example 0 = 0 to 10, I = 11 to 20 etc.
I assume you mean Arabic numeral (0, 1, 2, 3, 4...) not Roman (0, I, II, III, IV, ...

The fx:mean is from 0.0 to 1.0, so %[fx:int(mean*10)] would do that.
snibgo's IM pages: im.snibgo.com
Post Reply