Get and save image dimension in memory?

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
Don
Posts: 47
Joined: 2013-05-09T14:10:43-07:00
Authentication code: 6789

Get and save image dimension in memory?

Post by Don »

Hello,

I was wondering if it's possible to get an image dimension and then save it into memory for later use, like the following:

Code: Select all

convert \( -size 400 -background transparent caption:”this is some text“ -write mpr:captionText +delete \) \( mpr:captionText -format "%[fx:h]" INFO: -write mpr:imgWidth +delete \)
When you don't supply the height for -size when using the caption command and just the width, the height is automatically determined for your image/text which is great and really glad it's capable of doing this, but what if I want to find out what the height from the caption command will be without having to write the image to disk but to memory instead so that I can use the height for something else?

Thanks in advance.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Get and save image dimension in memory?

Post by snibgo »

Your bash command has bad syntax. It must end with an output filename (which could be "info:"). Within the command, you can "+write info:" at any time.

To capture the single-line output from a bash command, use backtick, like this:

Code: Select all

$ size=`convert rose: -format %[fx:h] info:`
$ echo $size
46
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Get and save image dimension in memory?

Post by fmw42 »

This should work and is similar to snibgo's suggestion.

Code: Select all

height=`convert -pointsize 36 -size 400x caption:"This is some long text that should wrap to the next line" -format "%h" info:`
echo "$height"
126

Or perhaps you want to save it inline. If so, then

Code: Select all

convert -pointsize 36 -size 400x caption:"This is some long text that should wrap to the next line" -set option:hsize "%h" -format "%[hsize]" info: 
126

Perhaps a better explanation of what you are trying to do later with the height would help us understand what you want.
Post Reply