Image Aspect Ratio (Width/Height)
convert rose: -format "%[fx:w/h]" info: 1.52174 |
Average RGB Color Of An Image
Note: %% is an escape of %
Method 1 convert rose: -colorspace rgb -scale 1x1 -format "%[pixel:p{0,0}]" info: rgb(146,89,80) |
Method 2 convert rose: -colorspace rgb -scale 1x1 -fx "debug(u)" null:
ROSE[0,0].red: u=0.57142 |
Method 3 convert rose: -colorspace rgb -scale 1x1 -depth 8 txt:
# ImageMagick pixel enumeration: 1,1,255,rgb |
Method 4 convert rose: -colorspace rgb -scale 1x1 -depth 16 txt:
# ImageMagick pixel enumeration: 1,1,65535,rgb |
Method 5 Fraction
convert rose: -colorspace rgb -scale 1x1 \
0.57142,0.350042,0.315557
Percent
convert rose: -colorspace rgb -scale 1x1 \
57.142%,35.0042%,31.5557%
Range 0 to 255
convert rose: -colorspace rgb -scale 1x1 \
145,89,80
Range 0 to QuantumRange (Q16)
convert rose: -colorspace rgb -scale 1x1 \ 37448,22940,22940 |
Grayscale Minimum, Maximum, Mean And Standard Deviation Of An Image (Percent)
Note 1: %% is an escape of %
Note 2: Statistics for any single channel can be extracted by substituting
"-channel xxx" for "-colorspace gray", where xxx can be either red or green or blue.
Method 1 (Any IM Version)
data=`convert rose: -colorspace gray -verbose info:` 14.6151%
max=`echo "$data" | sed -n '/^.*[Mm]ax:.*[(]\([0-9.]*\).*$/{ s//\1/; p; q; }'` 100%
mean=`echo "$data" | sed -n '/^.*[Mm]ean:.*[(]\([0-9.]*\).*$/{ s//\1/; p; q; }'` 41.2302%
std=`echo "$data" | sed -n '/^.*[Ss]tandard.*[(]\([0-9.]*\).*$/{ s//\1/; p; q; }'` 18.581% |
Method 2 (IM Version 6.3.9-1 Or Higher) (note: as of version 6.4.2-6, one can use either "%[standard-deviation]" or "%[standard_deviation]")
min=`convert rose: -colorspace gray -format "%[min]" info:` or convert xc: -format "%[fx:100*$(convert rose: -colorspace gray -format "%[min]" info:)/quantumrange]%%" info: 14.6151%
max=`convert rose: -colorspace gray -format "%[max]" info:` or convert xc: -format "%[fx:100*$(convert rose: -colorspace gray -format "%[max]" info:)/quantumrange]%%" info: 100%
mean=`convert rose: -colorspace gray -format "%[mean]" info:` or convert xc: -format "%[fx:100*$(convert rose: -colorspace gray -format "%[mean]" info:)/quantumrange]%%" info: 41.2302%
std=`convert rose: -colorspace gray -format "%[standard-deviation]" info:` or convert xc: -format "%[fx:100*$(convert rose: -colorspace gray -format "%[standard-deviation]" info:)/quantumrange]%%" info: 18.5811% |
Method 3 (IM Version 6.4.0-11 Or Higher) convert rose: -colorspace gray -format "%[fx:100*image.minima]%%" info: 14.6151% convert rose: -colorspace gray -format "%[fx:100*image.maxima]%%" info: 100% convert rose: -colorspace gray -format "%[fx:100*image.mean]%%" info: 41.2302% convert rose: -colorspace gray -format "%[fx:100*image.standard_deviation]%%" info: 18.5811% |
Method 4 (IM Version 6.4.2-6 Or Higher) convert rose: -colorspace gray -format "%[fx:100*minima]%%" info: 14.6151% convert rose: -colorspace gray -format "%[fx:100*maxima]%%" info: 100% convert rose: -colorspace gray -format "%[fx:100*mean]%%" info: 41.2302% convert rose: -colorspace gray -format "%[fx:100*standard_deviation]%%" info: 18.5811% |
All Channel Minimum, Maximum, Mean And Standard Deviation Of An Image (Percent)
Note: %% is an escape of %
Method 1: IM Version 6.4.0-11 Or Higher (usage: image.minima, image.maxima, image.mean and image.standard_deviation) convert rose: -colorspace rgb -separate -format "%[fx:100*image.minima]%%" info:
13.7255% convert rose: -colorspace rgb -separate -format "%[fx:100*image.maxima]%%" info:
100% convert rose: -colorspace rgb -separate -format "%[fx:100*image.mean]%%" info:
57.142% convert rose: -colorspace rgb -separate -format "%[fx:100*image.standard_deviation]%%" info:
27.1746% |
Method 2: IM Version 6.4.2-6 Or Higher (usage: minima, maxima, mean and standard_deviation) convert rose: -colorspace rgb -separate -format "%[fx:100*minima]%%" info:
13.7255% convert rose: -colorspace rgb -separate -format "%[fx:100*maxima]%%" info:
100% convert rose: -colorspace rgb -separate -format "%[fx:100*mean]%%" info:
57.142% convert rose: -colorspace rgb -separate -format "%[fx:100*standard_deviation]%%" info:
27.1746% |
Method 3: IM Version 6.4.2-6 Or Higher (usage: minima.channel, maxima.channel, mean.channel and standard_deviation.channel)
convert rose: -format \
red: |
Custom Identify With Ticks And Quality
Example 1 Create GIF Animation convert -delay 100 rose: -delay 50 rose: -delay 25 rose: -loop 0 rose_animation.gif identify identify rose_animation.gif
animation.gif[0] GIF 70x46 70x46+0+0 8-bit PseudoClass 256c 10.6kb "custom" identify (showing ticks)
info="%f[%s] %m %Tticks %wx%h %P%O %r %z-bit %Q%%qual %bbytes\n"
animation.gif[0] GIF 100ticks 70x46 70x46+0+0 8-bit PseudoClassRGB 256c 0%qual 10878bytes |
Example 2 Create JPEG Image convert rose: rose.jpg identify (jpeg) identify rose.jpg rose.jpg JPEG 70x46 70x46+0+0 8-bit DirectClass 1.54kb "custom" identify (jpeg showing quality)
info="%f[%s] %m %Tticks %wx%h %P%O %r %z-bit %Q%%qual %bbytes\n" rose.jpg[0] JPEG 0ticks 70x46 70x46+0+0 8-bit DirectClassRGB 85%qual 1580bytes |
Test If Image Is Grayscale Or Color
Method 1 (Any IM Version)
test=`convert rose: -scale 1x1! -format "%[fx:r==g&&r==b?1:0]" info:` Color
test=`convert rose: -colorspace gray -scale 1x1! -format "%[fx:r==g&&r==b?1:0]" info:` Gray |
Method 2 (Any IM Version) convert rose: -verbose info: | sed -n 's/^.*Colorspace: \([^ ]*\).*$/\1/p' RGB convert rose: -colorspace gray -verbose info: | sed -n 's/^.*Colorspace: \([^ ]*\).*$/\1/p' Gray |
Method 3 (IM Version 6.3.9-2 Or Higher) convert rose: -format "%[colorspace]" info: RGB convert rose: -colorspace gray -format "%[colorspace]" info: Gray |
Test If Image Has Alpha Channel
Method 1 (Any IM Version) convert -size 100x100 xc:white white.png
[ "$(identify -verbose white.png | grep 'alpha')" = "" ] && echo "alpha off" || echo "alpha on" alpha off convert white.png -alpha on white_alpha.png
[ "$(identify -verbose white_alpha.png | grep 'alpha')" = "" ] && echo "alpha off" || echo "alpha on" alpha on |
Method 2 (IM Version 6.4.3-7 Or Higher) convert -size 100x100 xc:white white.png convert white.png -format "%A" info: False convert white.png -alpha on white_alpha.png
convert white_alpha.png -format "%A" info: True |
Measure The Average Opacity Or Transparency Of An Image
IM 6.4.3-7 To IM ?.?.?.? -channel o and -channel a mean the same thing. Originally they both referred to opacity. Set 60% Opacity (40% Transparency) convert logo.jpg -alpha on -channel o -evaluate set 60% logo_opacity60.png convert logo_opacity60.png -format "%[fx:u.o]" info: returns opacity of 0.6 convert logo_opacity60.png -format "%[fx:u.a]" info: returns alpha/transparency of 0.4 |
IM ?.?.?.? Or Higher -channel o and -channel a mean the same thing.
Originally they both referred to opacity. Set 60% Transparcy (40% Opacity) convert logo.jpg -alpha on -channel a -evaluate set 60% logo_transparency60.png convert logo_transparency60.png -format "%[fx:u.a]" info: returns alpha/transparency of 0.6 convert logo_transparency60.png -format "%[fx:u.o]" info: returns opacity of 0.4 |
Sending Results From Compare -Metric To A Variable Or Text File
Convert rose: To JPEG And GIF For Comparison
convert rose: rose.jpg
Do Comparison And Save In Variable
str=`compare -metric rmse rose.jpg rose.gif null: 2>&1` 2561.35 (0.0390838) Do Comparison And Save In Text File
compare -metric rmse rose.jpg rose.gif null: 2> compare.txt 2561.35 (0.0390838) |
Forcing An Image Histogram To Be Generated
Images with more than 1024 colors will not have a histogram. To generate a histogram one must reduce the number of colors to 1024 or less.
Due to a slight difference in values, two methods are presented. We will use an example with 10 colors for simplicity. I have highlighted one place where the color values differ slightly. This would appear to be a bug, which has, as of IM 6.4.2-5, been fixed so that the histogram generated by the first method matches that of the second method.
Method 1 convert rose: -colorspace rgb -colors 10 -format "%c" histogram:info:
575: ( 51, 50, 45) #33322D rgb(51,50,45) |
Method 2 convert rose: -colorspace rgb -colors 10 miff:- | convert - -format "%c" histogram:info:
575: ( 51, 51, 45) #33332D rgb(51,51,45) |
Find The Percentage Of Pixels In An Image With A Given Color
Method 1 Select Color From Histogram convert rose: -colorspace rgb -colors 10 -format "%c" histogram:info:
575: ( 51, 50, 45) #33322D rgb(51,50,45) Extract Percent
str=`convert rose: -colorspace rgb -colors 10 -format "%c" histogram:info: | \ 8.60248% |
Method 2 Select Color From Histogram convert rose: -colorspace rgb -colors 10 miff:- | convert - -format "%c" histogram:info:
575: ( 51, 51, 45) #33332D rgb(51,51,45) Extract Percent
color="rgb(110,147,86)"
8.60304% |
Find The Location Of Every Pixel In An Image With A Given Color
Select Color From Histogram convert rose: -colorspace rgb -colors 256 -depth 8 -format "%c" histogram:info:
. Find All (6) X,Y Pixel Locations For Color (138,168,119)
color="( *137, *168, *119)" or
color="(137,168,119)"
48,7 |