Labelling in either black or white
Posted: 2011-05-21T14:29:05-07:00
I am trying to label photographs with either white or black letters, depending on the mean brightness of the local background, in order to acchieve the maximum contrast. I already read Anthony's remarks on labelling and reached at the following DOS beatch file:
I hope this is somehow legible to non-Windows users. What I basically do is:
1) Generate an image with the label in white letters
2) Identify its size
3) Cut the according piece from the image that I want to label
4) Identify its mean value (fx gives values 0 -- 1)
5) If this value is less than 0.5 --> white letters, otherwise black letters
6) Do the actual labelling with the correct letter color.
Although the code works, I wonder if there is a more elegant solution to the problem, without creating so many temporal files.
One problem was that -gravity isn't available for Identify. And there seems to be no -identify option for Convert, such that I could try to create an interim result and use it in the same command line. Piping in Windows is limited to simple string values.
Code: Select all
SETLOCAL ENABLEDELAYEDEXPANSION
SET Option=-pointsize 100 -background "#00000000" -fill
convert %Option% white label:"Test" label.miff
FOR /F %%i in ('identify -format %%wx%%h Label.miff') DO ^
convert DSCN9046_r.JPG -gravity northeast -crop %%i+0+0 test.miff
FOR /F %%i IN ('Identify -format "%%[fx:mean]" test.miff') DO ^
IF %%i LEQ 0.5 (
SET Color=white
) Else (
SET Color=black
)
convert %option% !color! label:"Test" DSCN9046_r.JPG -reverse -compose over -gravity northeast -geometry +0+0 -composite DSCN9046_c.jpg
1) Generate an image with the label in white letters
2) Identify its size
3) Cut the according piece from the image that I want to label
4) Identify its mean value (fx gives values 0 -- 1)
5) If this value is less than 0.5 --> white letters, otherwise black letters
6) Do the actual labelling with the correct letter color.
Although the code works, I wonder if there is a more elegant solution to the problem, without creating so many temporal files.
One problem was that -gravity isn't available for Identify. And there seems to be no -identify option for Convert, such that I could try to create an interim result and use it in the same command line. Piping in Windows is limited to simple string values.