Auto-Size problem

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
gorby
Posts: 2
Joined: 2017-06-20T18:19:42-07:00
Authentication code: 1151

Auto-Size problem

Post by gorby »

This is probably a really nab question, so appologies in advance. I have searched for auto-size threads but they dont seem to be dealing with the same issue.

Im trying to write text from a file into an autosized box as per http://www.imagemagick.org/Usage/annotating/ (the auto sized caption example)

Code: Select all

"width='identify -format %w test.png';convert -background '#0008' -fill white -gravity center -size ${width}x30 annotate text.txt test.png +swap -gravity North -geometry +0-3 -composite fort.jpg"
returns "convert: invalid argument for option `-size': identify @ error/convert.c/ConvertImageCommand/2791."

IF theres a simpler way to achieve this Id obviously just do that, Im just keep to use the auto size feature as Id like to cat a file on pictures in a script at a later point.

Thanks in advance.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Auto-Size problem

Post by fmw42 »

What you need to do is use label: or caption: in "best fit" mode to create an overlay image that has text on a transparent background. Then composite that over your background image at the location you want. See

http://www.imagemagick.org/Usage/text/
http://www.imagemagick.org/Usage/layers/#convert
http://www.imagemagick.org/Usage/basics/#parenthesis

Here is an example:

Unix

Code: Select all

convert logo: \( -size 300x -background none -fill red -font arial label:"This Is Some Text" \) -gravity center -compose over -composite logo_with_text.jpg
Windows

Code: Select all

convert logo: ( -size 300x -background none -fill red -font arial label:"This Is Some Text" ) -gravity center -compose over -composite logo_with_text.jpg
Please always provide your IM version and platform/OS, since syntax differs.
gorby
Posts: 2
Joined: 2017-06-20T18:19:42-07:00
Authentication code: 1151

Re: Auto-Size problem

Post by gorby »

Thanks a lot for the reply. I re-read the stuff you linked with a view to doing what you suggested. Understanding the stuff about the parenthesis is helpful too, thanks. It took me a while to realise that "logo" in your example was the picture of the wizard dude, as I wasn't sure where it was coming from :D

Code: Select all

convert test.png \( -size 400x60 -background grey -fill red -font arial label:@/home/gorby/text \) -gravity northwest -compose over -composite logo_with_text.jpg

This is working well for me as I can just use sed to alter the text file contents so it fits nicely in the box.
Ive probably failed to understand some stuff, so might ask another question about stuff later if thats ok :) Thanks for the help.
Im using $ convert.exe Version: ImageMagick 6.9.5-7 Q16 x86_64 2016-12-23 http://www.imagemagick.org in cygwin v 2.880 64
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Auto-Size problem

Post by snibgo »

gorby wrote:width='identify -format %w test.png'
The page you linked to has:
Anthony wrote:width=`identify -format %w dragon.gif`
Can you see the difference? You set the variable to a string that starts with "identify", so when this is used in "-size", you get the error:

Code: Select all

invalid argument for option `-size': identify
You need to use backquotes, aka backticks, so your variable is set to the result of the identify command.
snibgo's IM pages: im.snibgo.com
Post Reply