SenatorForLife
SenatorForLife

Reputation: 45

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

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 canvas area, and then append this it 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:

Mockup of desired output

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:

Actual result

I think that what I need to do is to turn off the -trim option somehow, but I'm not sure how to do that. +trim is not a valid option, and +repage doesn't do it.

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

(The trim option is needed to get the height down to 25px--the shadow operation generates too large a vertical extension otherwise. EDIT: And I guess I was wrong above--even w/o the -trim anywhere in the command, the fixed-size image I'm hoping for doesn't work out.)

Upvotes: 0

Views: 338

Answers (1)

SenatorForLife
SenatorForLife

Reputation: 45

Hm, looks like there isn't much of a community for ImageMagick left here. I posted to the ImageMagick phpBB board and was able to put together an answer. Briefly:

  • The -trim option is not an option that affects every image after it is invoked. Instead, it is essentially a command that executes immediately.
  • The -extent option can be used to increase or decrease an image to a desired size. Apparently the -size command, which I used in my original code, can only be used to set the initial size of an image, not to change the size of an image that already exists.

Here is the final working command:

convert \
\( -background transparent -extent 100x25 -gravity west \
   \( -fill lavender -font 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 MyriadPro- Semibold.otf \
      -pointsize 15 label:'Long legend for x' -gravity west \) \
   +append -strip legend_test.png

Upvotes: 1

Related Questions