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

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
Erik Temple
Posts: 4
Joined: 2013-03-18T15:32:10-07:00
Authentication code: 6789

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

Post 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!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
Erik Temple
Posts: 4
Joined: 2013-03-18T15:32:10-07:00
Authentication code: 6789

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

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
Erik Temple
Posts: 4
Joined: 2013-03-18T15:32:10-07:00
Authentication code: 6789

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

Post 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?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

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

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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
snibgo's IM pages: im.snibgo.com
Erik Temple
Posts: 4
Joined: 2013-03-18T15:32:10-07:00
Authentication code: 6789

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

Post 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.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

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

Post by anthony »

I also suggest you always do a +repage after a trim or crop! Otherwise it may effect later operations.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply