Page 1 of 1

Label question: limiting width

Posted: 2017-11-21T08:05:06-07:00
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?

Re: Label question: limiting width

Posted: 2017-11-21T10:36:32-07:00
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

Re: Label question: limiting width

Posted: 2017-11-21T11:20:14-07:00
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.)

Re: Label question: limiting width

Posted: 2017-11-21T12:22:40-07:00
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
?

Re: Label question: limiting width

Posted: 2017-11-21T12:51:22-07:00
by fmw42
Use can use caption: to write multiple lines. It will wrap automatically if you provide the pointsize and width.

Re: Label question: limiting width

Posted: 2018-01-07T15:46:25-07:00
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: