A detail about -format

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
pitosalas

A detail about -format

Post by pitosalas »

Hi

(Sorry that I am posting so many little questions, I hope you don't mind. I do diligently try to answer them myself before posting but your answers have been very helpful time savers!)

$ convert revers3.tif -scale 1x1 -format "%[pixel:p{0,0}.r]" info:-

outputs:

rgb(227,227,227)

Because of the .r in -format "%[pixel:p{0,0}.r" I would have expected the output to be

227

Thanks mucho!

Pito
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: A detail about -format

Post by fmw42 »

use this instead

convert revers3.tif -scale 1x1! -format "%[fx:floor(255*u.r)]" info:

for example with the IM special image rose:

convert rose: -scale 1x1! -format "%[fx:floor(255*u.r)]" info:
145
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: A detail about -format

Post by anthony »

%[pixel:...] returns a color, based on the fx pixel selection.

%[fx:...] returns a single number.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
pitosalas

Re: A detail about -format

Post by pitosalas »

Follow ups:

(1) What's better (faster etc?) if I want to have a series of integers corresponding to colors:

convert rose: -scale 10x1! txt:-

or

convert rose: -scale 1x1! -format "%[fx:floor(255*u.r)]" info:-

(2) How come those two commands give different answers for red?

$ convert rose: -scale 1x1! -format "%[fx:floor(255*u.r)]" info:-
145

convert rose: -scale 1x1! txt:-
# ImageMagick pixel enumeration: 1,1,255,rgb
0,0: (146, 89, 80) #925950 rgb(146,89,80)


and (3)

How come this produces just one number and not 10:

convert rose: -scale 10x1! -format "%[fx:floor(255*u.r)]" info:-
82


Thanks!!

-- Pito
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: A detail about -format

Post by fmw42 »

remove the floor() and you get a float (145.712) as u varies from 0 to 1. so you can then round, int, floor, ceil however you want (see http://www.imagemagick.org/script/fx.php). i am not sure what method is used by txt: to get its value. I used floor, but perhaps round() would be better.


convert rose: -scale 10x1! -format "%[fx:floor(255*u.r)]" info:-

takes only one pixel (not sure which), but you can tell it which one using u.p{i,0} where i=0 to 9
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: A detail about -format

Post by anthony »

txt: just output integers occording to the current -depth. Typically defaulting to the original images
depth, unless it was transofrmed by resize, scale etc into the compile time quantium depth (typically Q16)

fx 'u' has a value from 0.0 to 1.0 so the %[fx:..] formula tries to return a integer from 0 to 255

As FX is interpreted it is slow, BUT as it is a single once off that shoud be quite quick in this case. As such I would do a test of some kind to see which is faster. I don't think it matters though as the -scale will be a lot slower than either txt:, %[fx:..]
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply