Page 1 of 1
% character in caption not appearing
Posted: 2017-02-28T02:42:14-07:00
by BigNoub
I'm using ImageMagick 6.7.7-10 2016-11-29 Q16 on Ubuntu 14.04
I am using this command to add a caption to an image:
Code: Select all
convert -background black -bordercolor black -fill white -size 300x20 -pointsize $myFontsize caption:$title -trim -border 5 -channel A -fx '(lightness/2)+.8' -geometry +$myX+$myY $input +swap -composite $output
but when $title contains a % character, it is not visible in the caption, nor is the space character following the %.
For instance, if $title = "India contributes to 75% of the global spice production", the caption will read "India contributes to 75of the global spice production"
How can I avoid that?
Re: % character in caption not appearing
Posted: 2017-02-28T03:31:27-07:00
by Bonzo
I wonder if the % acting as an escape? First thing I would try is:
Re: % character in caption not appearing
Posted: 2017-02-28T07:29:40-07:00
by BigNoub
no, double quoting does not solve the problem
Re: % character in caption not appearing
Posted: 2017-02-28T08:06:03-07:00
by magick
Try 75%%. Escape a percent with another percent. If that fails, try 75\%.
Re: % character in caption not appearing
Posted: 2017-02-28T10:10:42-07:00
by BigNoub
Escaping the percent with another percent works! Although it is not very practical because my caption is read from a file, and I have lots of captions to write, potentially with lots of percent in them, I can't go throughout the file and change them all.
Is my only option to analyse each $title variable and transform all % characters into %%?
Could there be other characters affected?
Re: % character in caption not appearing
Posted: 2017-02-28T10:39:35-07:00
by fmw42
I can't go throughout the file and change them all.
Many text editors will do a global search and replace allowing you to change all % to %%.
Re: % character in caption not appearing
Posted: 2017-02-28T10:56:49-07:00
by snibgo
Or just filter each input with sed.
Re: % character in caption not appearing
Posted: 2017-02-28T10:58:11-07:00
by BigNoub
yes you're right, what I meant is that the files I use as inputs are generated in real time by users, I can't ask them to write %% as it's really not human-readable, and I can't myself do tens of global searches per day to change that manually, I need everything to be automated.
Is that a bug anyway? If yes it should be fixed in the future? Or is it limited to my version of IM?
Re: % character in caption not appearing
Posted: 2017-02-28T11:05:05-07:00
by snibgo
Not a bug, but a feature.
In "caption:", "%" is a special character, so we can do things like this:
Code: Select all
convert -size 300x300 caption:"Width is %w" x.png
In my view, IM should have a setting to "treat special characters as literals". But it doesn't, so sed or similar can be used on the fly.
EDIT: for example, suppose the file pc.txt contains:
Then use this bash command:
Code: Select all
convert -size 300x300 caption:"`sed -e s/%/%%/g pc.txt`" x.png
... and we get the expected image.
Re: % character in caption not appearing
Posted: 2017-02-28T14:24:00-07:00
by BigNoub
Perfect thanks!