Calculate the average colour of an area in an image

后端 未结 3 702
面向向阳花
面向向阳花 2021-02-04 12:56

The goal is to lay a caption on top of an image and set the text colour to one that will contrast with the background it is positioned on top of. To that end, I wish to calculat

3条回答
  •  时光取名叫无心
    2021-02-04 13:36

    Here is a command that handles both cropping and color detection, and also produces output in a consistent R,G,B format:

     convert image.gif -crop 6x7+8+9 -resize 1x1\! -format "%[fx:int(255*r+.5)],%[fx:int(255*g+.5)],%[fx:int(255*b+.5)]" info:-
    

    where, in 6x7+8+9 :

     6: image width (pixels)
     7: image height (pixels)
     8: x-coordinate of top left corner
     9: y-coordinate of top left corner
    

    Returns

     176,191,67
    

    Adapted from https://stackoverflow.com/a/25488429/3124680

提交回复
热议问题