'Copy a pixel color (ImageMagick)

How can I specify a color from an given pixel to -fill ?

convert -gravity center -background rgb\(222,97,56\) -fill "#%[hex:u.p{10,10}/2]" -size 120x60 -pointsize 42 label:Test -depth 8 out.png

Error : convert: unable to open image '#%[hex:u.p{10,10}/2]': No such file or directory

-format '%[hex:p{10,10}/2]\n' info:

returns "6F311C" and -fill "#6F311C" works though.



Solution 1:[1]

Updated Answer

Try this if you want to use label: rather than -annotate:

magick -gravity center -size 100x100 xc:yellow  \
   -fill black -draw "rectangle 10,10 100,100" \
   -fill '%[pixel:p{0,0}]' -background magenta -size 100x80 -pointsize 42 label:"Label" -append result.png

enter image description here

Original Answer

I think you want this:

magick -gravity center -size 100x100 xc:yellow  \
    -fill black -draw "rectangle 10,10 100,100" \
    -fill '%[pixel:p{0,0}]' -annotate 0 "text" result.png

enter image description here

The first 2 lines create a yellow canvas with a black rectangle overlaying the bottom-right part leaving the top-left pixel yellow. The fill colour is then taken from pixel (0,0) and used for the text annotation.

Solution 2:[2]

To avoid issue in different OS platforms in Imagemagick, you should use double quotes.

The following should return the hex value for blue:

magick xc:red xc:blue xc:green1 +append -depth 8 -format "%[hex:u.p{1,0}]" info:

0000FFF

If you really want to do division, then

magick xc:red xc:blue xc:green1 +append -depth 8 -format "%[hex:u.p{1,0}/2]" info:

000080

So to put that into a label,

# create image
magick xc:red xc:blue xc:green1 +append rgb.png

# put color into label
magick rgb.png -set label "%[hex:u.p{1,0}/2]" rgb.png

# check label
magick rgb.png -format "%l" info:

000080

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 fmw42