Label question: limiting width

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
jessicaC
Posts: 6
Joined: 2017-11-04T04:03:17-07:00
Authentication code: 1152

Label question: limiting width

Post by jessicaC »

Hi everyone

So I have the following code to generate thumbs with filenames/size and dimensions below

Code: Select all

 convert $jpg -set option:mylabel '%f\n%wx%h\n%[size]' -verbose -font Arial -pointsize 15 -geometry 200x200+2+2 -auto-orient -resize 200x200 -fill 'gray' -gravity center label:'%[mylabel]' -append ./n/$jpg
However when the filename is very large, it looks bad. I want to limit the width to 200x and truncate filename, when it's more than that, something like:
Somelongf...jpg

Is it possible? How to change the above code?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Label question: limiting width

Post by fmw42 »

ImageMagick by itself cannot truncate the file names in the middle if too long. You will have to compute the length of the text and truncate it yourself with some other scripting code. You test if your text is too long by adding -debug annotate and parsing the output for the width of the text. You may have to iterate to get the right size. You can then edit your text and try again, or just use a smaller font. I do not see where you have specified the desired width for label:. If you do that and leave off the pointsize, IM will pick the best pointsize to fit the width. See http://www.imagemagick.org/Usage/text/#label
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Label question: limiting width

Post by snibgo »

I can't see an easy way of doing this. You could do it like this, supposing the filename is "A_very_long_name.jpg":

1. Make an image for "A_very_long_name.jpg". If it is within your width limit, job done.

2. Make three images, for "A_very_long_name" and "..." and ".jpg". Suppose the widths are W1, W2 and W3. They need to fit into a total width T.

3. Calculate x = T-W2-W3. This is the new width for the first part of the name. Crop it (chopping off the right side) to this width. Then append the "..." and ".jpg" images.

This will often chop the first part of the name within a character. With more effort, you could crop at a character boundary.

You could do this in a shell script. It might be possible as a pure ImageMagick command. (In fact, I'm sure that is possible, but it might not be pretty.)
snibgo's IM pages: im.snibgo.com
jessicaC
Posts: 6
Joined: 2017-11-04T04:03:17-07:00
Authentication code: 1152

Re: Label question: limiting width

Post by jessicaC »

thanks guys. I see how I can do this but that's pretty complex
What about making the label with imagemagic so that the filename is not written in one line but in two instead?
Let's say
Very
long
name
.jpg
?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Label question: limiting width

Post by fmw42 »

Use can use caption: to write multiple lines. It will wrap automatically if you provide the pointsize and width.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Label question: limiting width

Post by fmw42 »

If you just want two lines and know where you want to split it, then just insert \n at that location in your text string when using label:
Post Reply