Convert/ Caption- Working out whether to resize text
Convert/ Caption- Working out whether to resize text
Hi,
I have a convert command I am running to write some text using "caption:" . The actual caption to be written, and the font size to be used is fed in via an HTML form which is submitted to ImageMagick. The image size is fixed. The text must always fit in the given image size. If the text overflows the image, then we must automatically 'best-fit' the text.
What I cannot figure out is how I should be handling the decision of whether or not to perform a resize.
The only way I can see is to do a 'test' where we write an image out with the user's desired text, (with fixed width but variable height), then after writing it/ blobbing it, we measure the height of the image. If it is less than the height of the 'fixed' image, we can just do what the user wants (i.e. use their given font size and text, and stick to our set image size). If the image comes out too tall, then we know the text is too big, and we run a different command which will auto- resize the text.
Obviously, the above is very expensive on CPU time... There must be a better way of doing it! Any help greatly appreciated.
Thanks
I have a convert command I am running to write some text using "caption:" . The actual caption to be written, and the font size to be used is fed in via an HTML form which is submitted to ImageMagick. The image size is fixed. The text must always fit in the given image size. If the text overflows the image, then we must automatically 'best-fit' the text.
What I cannot figure out is how I should be handling the decision of whether or not to perform a resize.
The only way I can see is to do a 'test' where we write an image out with the user's desired text, (with fixed width but variable height), then after writing it/ blobbing it, we measure the height of the image. If it is less than the height of the 'fixed' image, we can just do what the user wants (i.e. use their given font size and text, and stick to our set image size). If the image comes out too tall, then we know the text is too big, and we run a different command which will auto- resize the text.
Obviously, the above is very expensive on CPU time... There must be a better way of doing it! Any help greatly appreciated.
Thanks
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert/ Caption- Working out whether to resize text
I think that is just what you have to do.
The only other way is to set the image size for caption: (both width and height) and let IM find the best font size to fit into those dimensions by leaving out -pointsize. Add -gravity center and it should center it the best it can, if I am not mistaken
The only other way is to set the image size for caption: (both width and height) and let IM find the best font size to fit into those dimensions by leaving out -pointsize. Add -gravity center and it should center it the best it can, if I am not mistaken
Last edited by fmw42 on 2010-03-23T15:05:32-07:00, edited 1 time in total.
Re: Convert/ Caption- Working out whether to resize text
Thanks
Unfortunately the user can control the vertical alignment too, so that's a non- starter- thanks though.
Unfortunately the user can control the vertical alignment too, so that's a non- starter- thanks though.
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert/ Caption- Working out whether to resize text
i believe that you can add -geometry to -gravity in order to adjust the alignment. any way just a thought to try. But I may be mistaken and those controls may only work with -annotate.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Convert/ Caption- Working out whether to resize text
You mean, I assume, "-gravity center". Yes, -gravity works with caption.Add -geometry center and it should center it the best it can, if I am not mistaken
"-geometry" doesn't work for me with "caption", but "-extent" does. For example, this places text 100 pixels right and 500 pixels down, compared to the same command without "-extent".i believe that you can add -geometry to -gravity in order to adjust the alignment.
Code: Select all
convert -size 1920x1080 ^
-gravity North -extent -100-500 ^
-background Red -fill "#000080" ^
-strokewidth "2" -stroke white ^
-font Verdana -density 96 -pointsize 56 ^
caption:@capt.txt capt.png
snibgo's IM pages: im.snibgo.com
Re: Convert/ Caption- Working out whether to resize text
The user can adjust the text both vertically and horizontally, and I only want to do something in the event that the user writes text that it too big to fit in the box.
Thanks
Thanks
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert/ Caption- Working out whether to resize text
Yes, thanks. corrected above now.snibgo wrote:You mean, I assume, "-gravity center". Yes, -gravity works with caption.Add -geometry center and it should center it the best it can, if I am not mistaken
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert/ Caption- Working out whether to resize text
As I said before, you can set the image size for caption: (both width and height) and let IM find the best font size to fit into those dimensions by leaving out -pointsize. That is pretty much equivalent to setting the pointsize and then resizing afterwards as the pointsize will get effectively smaller. Not sure why this is not acceptable to you. You can even use -gravity to adjust it north, center, or south aligned.
Otherwise, I cannot suggest anything else to do.
For more elaborate adjustments, you can use -annotate on a second pass after you find out how big your resulting image will be and resize if necessary. The redraw with -annotate
Otherwise, I cannot suggest anything else to do.
For more elaborate adjustments, you can use -annotate on a second pass after you find out how big your resulting image will be and resize if necessary. The redraw with -annotate
Re: Convert/ Caption- Working out whether to resize text
Hi fmw,
I fully understand what you are saying, but if the user can choose the font size, vertical and horz. alignment, but the overall dimensions of the image are fixed (and the text is specified by the user too).
The problem is I want to accommodate the user's chosen font size where possible, but when not possible, I need to invoke an auto resize, to stop the text overflowing. IM does not return anything when the text overflows the image dimensions. If IM could somehow return something if the output overflowed, then there would be no problem, as I could listen for this, capture it, and act on it.
What you are suggesting is simply the solution I want to come up with to invoke a resize- not actually help in making the decision whether to invoke a resize or not.
Even if I could count the number of wraps IM will do on the string, I could work out whether it is likely to overflow and invoke auto- resize, but I can't do this either.
Thanks
I fully understand what you are saying, but if the user can choose the font size, vertical and horz. alignment, but the overall dimensions of the image are fixed (and the text is specified by the user too).
The problem is I want to accommodate the user's chosen font size where possible, but when not possible, I need to invoke an auto resize, to stop the text overflowing. IM does not return anything when the text overflows the image dimensions. If IM could somehow return something if the output overflowed, then there would be no problem, as I could listen for this, capture it, and act on it.
What you are suggesting is simply the solution I want to come up with to invoke a resize- not actually help in making the decision whether to invoke a resize or not.
Even if I could count the number of wraps IM will do on the string, I could work out whether it is likely to overflow and invoke auto- resize, but I can't do this either.
Thanks
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert/ Caption- Working out whether to resize text
I don't think you understand. IM will keep the font size from causing overflow if you specify the image size in width AND height but leave off the pointsize. It will find the pointsize that will just fill the specified size. This is equivalent to specifying the width and pointsize and getting a larger height and then having to resize to get the height as you might want it, which will make the pointsize smaller just as my method would do in the end. If you still need to adjust the position, either make the width and/or height smaller to accomodate your movement or resize. But my way will not overflow.
see http://www.imagemagick.org/Usage/text/#caption_bestfit
see http://www.imagemagick.org/Usage/text/#caption_bestfit
Re: Convert/ Caption- Working out whether to resize text
I fully understand your point, however:
Imagine an HTML form linked to ImageMagick and two extreme scenarios.....
A) If the user can choose point size 36 and then write 50 lines into a box that is 200x100 pixels, it is clearly going to overflow, right?
B) If the user chooses point size 36 and writes the letter 'a' in the same box, it will not overflow, right?
IF I leave the command as- is (so point size 36 is allowed), how do I know when the text is going to overflow (i.e. in the first situation) and remove the pointsize from the ImageMagick command to prevent overflow (and invoke auto- fit text)? ImageMagick outputs nothing to indicate that the text has overflowed.
I know HOW to prevent it overflowing and HOW to not prevent it overflowing, I just cannot find a method to choose WHETHER (not HOW) to put limits on the text overflow.... In scenario A), I need to prevent an overflow by removing the font size, in scenario 2, I do not... How can I tell when that overflow is about to happen?
Thanks
Imagine an HTML form linked to ImageMagick and two extreme scenarios.....
A) If the user can choose point size 36 and then write 50 lines into a box that is 200x100 pixels, it is clearly going to overflow, right?
B) If the user chooses point size 36 and writes the letter 'a' in the same box, it will not overflow, right?
IF I leave the command as- is (so point size 36 is allowed), how do I know when the text is going to overflow (i.e. in the first situation) and remove the pointsize from the ImageMagick command to prevent overflow (and invoke auto- fit text)? ImageMagick outputs nothing to indicate that the text has overflowed.
I know HOW to prevent it overflowing and HOW to not prevent it overflowing, I just cannot find a method to choose WHETHER (not HOW) to put limits on the text overflow.... In scenario A), I need to prevent an overflow by removing the font size, in scenario 2, I do not... How can I tell when that overflow is about to happen?
Thanks
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Convert/ Caption- Working out whether to resize text
This is the only way I know to do it. You have to virtually create the image and test its height.
Note don't specify height in caption command.
This is unix. If on windows, see http://www.imagemagick.org/Usage/windows/
width=100
height=100
psize=24
hh=`convert -background blue -fill white -font Helvetica -size ${width}x \
-pointsize $psize caption:"this is a test message that might overflow" miff:- |\
convert - -format "%h" info:`
[ $hh -gt $height ] && echo "overflow" || echo "no overflow"
Note don't specify height in caption command.
This is unix. If on windows, see http://www.imagemagick.org/Usage/windows/
width=100
height=100
psize=24
hh=`convert -background blue -fill white -font Helvetica -size ${width}x \
-pointsize $psize caption:"this is a test message that might overflow" miff:- |\
convert - -format "%h" info:`
[ $hh -gt $height ] && echo "overflow" || echo "no overflow"
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Convert/ Caption- Working out whether to resize text
There might be a shortcut available by calling MagicWand from a custom program, but I can't see a quicker way using commands. It needs two passes: first to try at the user-requested pointsize, then (if necessary) let IM choose a smaller size.
I don't think there is any way to tell the first pass to give up as soon as it determines it needs more than a pre-determined height, or to tell it to use the largest pointsize it can, but capped at the user-defined size.
I don't think there is any way to tell the first pass to give up as soon as it determines it needs more than a pre-determined height, or to tell it to use the largest pointsize it can, but capped at the user-defined size.
snibgo's IM pages: im.snibgo.com