Page 1 of 1

Appending a label of variable width to one with a fixed size

Posted: 2013-03-18T15:39:26-07:00
by Erik Temple
I am trying to use an Imagemagick command to create a single-letter text label, give it a shadow, place it on the left side of a fixed-size image/canvas, and then append this to another label of fixed height but unknown width. So, the desired result is a single letter on the left side of the final transparent PNG, and another label set about 100px to the right of the origin, e.g. this mockup:

Image

I have all of this working in the following command, except that the shadowed text label is not in a fixed size box (should be 100px by 25px). Here's the result of the command:

Image

Code: Select all

convert \
\( -background transparent \
   \( -gravity west -fill lavender -font Constantia.ttf \
      -pointsize 12 label:'x' -trim \
      \( +clone -background black -shadow 100x3+0+0 -channel A -level 0,50% \
         +channel \) \
      +swap +repage -gravity center -composite \) \
-size 100x25 -gravity west \) \
   \( -size x25 -fill black -background transparent -font MyriadPro-Semibold.otf \
      -pointsize 15 label:'Long legend for x' -gravity west \) \
   +append -strip legend_test.png
Any help you can provide would be much appreciated!

Re: Appending a label of variable width to one with a fixed

Posted: 2013-03-18T17:31:18-07:00
by snibgo
You are explicitly trimming, and the "-size 100x25 -gravity west" is oddly placed.

By removing the trim, and moving "-size 100x25 -gravity west" to immediately before "-background transparent" but after the parenthesis, I seem to get what you want.

Re: Appending a label of variable width to one with a fixed

Posted: 2013-03-18T18:05:54-07:00
by Erik Temple
Thanks, snibgo. I thought I'd tried the -size and -gravity parameters in that position w/o result, but I guess not!

However, that doesn't quite do it, because trimming is necessary to get the desired fixed height of 25px. Is there a way to explicitly turn off trim? (+trim is not a valid option, unfortunately.) Or an alternate way to achieve trim that can be done per image rather than "globally"?

Or do I just have to do this in two steps? That would be much less convenient, since I am setting up a script to do large batches of these.

Re: Appending a label of variable width to one with a fixed

Posted: 2013-03-18T18:28:54-07:00
by snibgo
I'm not sure I understand you. By default, no trimming is done. It only trims if you ask for it, with "-trim".

The problem is the shadow, which adds height. If you don't have the shadow, you get a height of 25 without trimming. You could crop it, of course.

Re: Appending a label of variable width to one with a fixed

Posted: 2013-03-18T18:43:01-07:00
by Erik Temple
Most other options can be turned off or on per image in the stack: You can use -clip-mask with one image in the stack, for example, and then use +clip-mask to turn it off for the next. So, how would you do that with -trim, or is that a feature that still needs to be added to IM?

I looked at -crop, but it doesn't seem to do what -trim does. It seems that I would have to provide pretty explicit directions about what pixels to crop, whereas I want IM to remove all of the transparent pixels, and then add padding (if necessary) to get back up to 25px. I will be using varied fonts and shadows on the left, and while all of them should fit in 25px, there's no guarantee that the same specific crop values will work for all of them. Or possibly there is something I'm not getting about crop, which someone could show me?

Re: Appending a label of variable width to one with a fixed

Posted: 2013-03-18T19:37:15-07:00
by anthony
You could do the trim, but then use -extent to expand/clip that result to the specific size you want with appropriate gravity.

Re: Appending a label of variable width to one with a fixed

Posted: 2013-03-18T19:52:21-07:00
by snibgo
Broadly speaking, IM has settings and commands. A setting (eg "clip-mask", "compose") remains in effect until it is changed, and generally doesn't change images. A command (eg "trim", "composite") is executed once, generally changes images, and does nothing else.

Some commands (eg "contrast" and "level-colors") have both "-" and "+" forms. The + and - forms generally do the opposite of each other, in some sense.

The "trim" command has no "+" form. Neither does the "border" command although, in a sense, they are opposites of each other.

For IM options, both settings and commands, see http://www.imagemagick.org/script/comma ... ptions.php

Re: Appending a label of variable width to one with a fixed

Posted: 2013-03-18T20:35:15-07:00
by Erik Temple
Thanks, anthony! It took some trial and error for me to get it right, and for some reason I need to set the extent 3 times to avoid various problems, but the final command looks like this:

Code: Select all

convert \
\( -background transparent -extent 100x25 -gravity west \
   \( -fill lavender -font /Library/Fonts/Microsoft/Constantia.ttf \
      -pointsize 12 label:'x'  -trim -extent 100x25 -gravity west \
      \( +clone -background black -shadow 100x3+0+0 -channel A -level 0,50% \
         +channel \) \
      +swap +repage -gravity center -composite \) \
-background transparent -extent x25 \) \
   \( -size x25 -fill black -background transparent -font /Library/Fonts/MyriadPro-Semibold.otf \
      -pointsize 15 label:'Long legend for x' -gravity west \) \
   +append -strip legend_test.png
Notes: the first -extent can also be either a -size or a -page, while the third -extent cannot specify the width or the left part of the shadow will be cut off.

Re: Appending a label of variable width to one with a fixed

Posted: 2013-03-18T21:39:12-07:00
by anthony
I also suggest you always do a +repage after a trim or crop! Otherwise it may effect later operations.