% character in caption not appearing

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
BigNoub
Posts: 33
Joined: 2015-12-29T14:49:52-07:00
Authentication code: 1151

% character in caption not appearing

Post 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?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: % character in caption not appearing

Post by Bonzo »

I wonder if the % acting as an escape? First thing I would try is:

Code: Select all

caption:"$title" 
BigNoub
Posts: 33
Joined: 2015-12-29T14:49:52-07:00
Authentication code: 1151

Re: % character in caption not appearing

Post by BigNoub »

no, double quoting does not solve the problem
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: % character in caption not appearing

Post by magick »

Try 75%%. Escape a percent with another percent. If that fails, try 75\%.
BigNoub
Posts: 33
Joined: 2015-12-29T14:49:52-07:00
Authentication code: 1151

Re: % character in caption not appearing

Post 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?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: % character in caption not appearing

Post 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 %%.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: % character in caption not appearing

Post by snibgo »

Or just filter each input with sed.
snibgo's IM pages: im.snibgo.com
BigNoub
Posts: 33
Joined: 2015-12-29T14:49:52-07:00
Authentication code: 1151

Re: % character in caption not appearing

Post 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?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: % character in caption not appearing

Post 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:

Code: Select all

75% interest
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.
snibgo's IM pages: im.snibgo.com
BigNoub
Posts: 33
Joined: 2015-12-29T14:49:52-07:00
Authentication code: 1151

Re: % character in caption not appearing

Post by BigNoub »

Perfect thanks!
Post Reply