Page 1 of 1

ImageMagick Guru? / RMagick Help

Posted: 2011-10-27T10:53:29-07:00
by leehUk
Hi Guys,

I’ve only recently discovered ImageMagick whist looking for a solution to a problem I’m trying to solve involving a bit of image processing - however I’m not the best programmer and don’t have any experience using ImageMagick so I’m looking for a guru (or even someone who knows roughly how to use it). I’m sorry I don’t mean to treat this like a job board but just didn’t know where else to look...

What I’m trying to achieve:
1. Load ‘Background Image’ - a consistent fixed size
2. Overlay ‘Logo Image’ - this will be the same logo / size / position every time
3. Load ‘Other Image’ - it is an arbitrary size
4. Scale ‘Other Image’ using a specified scale factor
5. Rotate ‘Other Image’ using a specified degree of rotation
6. Position ‘Other Image’ using specified x and y values
7. Combine all 3 results and save as a PDF

I’ve attached an image to try and illustrate what I mean:

Image

Ideally it could be incorporated into the rails application running on Heroku. So I’m not sure of the best way to do this, possibly using one of the ImageMagick ruby interfaces such as RMagick?

Feedback much appreciated and please shout at me if this is the wrong place to post something like this!

Thanks,
Lee

Re: ImageMagick Guru?

Posted: 2011-10-27T12:32:19-07:00
by fmw42
see
http://www.imagemagick.org/Usage/layers/#convert
http://www.imagemagick.org/Usage/basics/#parenthesis

something like

convert backgroundimage logoimage -gravity southeast -geometry +X+Y -compose over -composite \
\( otherimage -rotate A \) -gravity northwest -geometry +XX+YY -compose over -composite \
outputimage

Re: ImageMagick Guru?

Posted: 2011-10-27T20:05:37-07:00
by anthony
Also see
Layers of Images
http://www.imagemagick.org/Usage/layers/

Essentually what Fred (fwm42) has detailed will do what you want.
Set background image,
the read and modify each new image in parenthesis, before compositing it on that background.

Here is another example,

Replace the 'monaliza' in the wizard image (download from net)
with another image (of any size), (also downloaded from net)
then do a final resize before saving.

Code: Select all

points="0,0 506,471  %w,0 643,487  %w,%h 571,617  0,%h 440,591"
convert http://magick.imagemagick.org/image/wizard.png \
        \( http://www.imagemagick.org/Usage/images/logo.gif \\
           -bordercolor white -border 0 -alpha set \
           -virtual-pixel transparent +distort perspective "$points" \
        \) -flatten  -resize x800          ImageMagick_Wizardary.jpg
Notes
  • The use of '%w' and '%h' in the control 'points' list allows the input image to be any size.
  • the border operator is used to remove any transparency, then the image is distorted and moved into place.
  • Also 'flatten' is used for the composition to preserve the virtual image offsets generated by +distort.
  • The aspect ratio of the second replacement image is not preserved, though with some more work it could be.
Image

Re: ImageMagick Guru?

Posted: 2011-10-28T02:42:12-07:00
by leehUk
Thanks so much for you replays guys!

I assume that I need to just need to choose a ruby wrapper such as QuickMagick (http://quickmagick.rubyforge.org/quick_magick) and try to replicate the commands? I have tried to use it previously but found it didn't support some functions, parentheses, or I just couldn't find them / use them properly.

I'll give it a go, hopefully I can figure it out. What you've provided is a great guide as to what I need to achieve it :)

One other thing, how can I save the resulting image into a PDF of a specified size?

Thanks again,
Lee

Re: ImageMagick Guru?

Posted: 2011-10-29T20:49:35-07:00
by anthony
Parenthesis is a Command line syntax. The CLI API also has the policy of always replacing images with there modified versions, unless cloned first.

In other API's you more typically use a separate 'wand' or 'image sequence's. But the in other API's you typically keep all images completely separate until you are ready to compose, or layer them.

CLI does not have that luxury, so it uses one 'current' image sequence, and uses parenthesis as a method of generating a stack of image sequences.

Re: ImageMagick Guru?

Posted: 2011-11-02T10:20:45-07:00
by leehUk
Does anyone know how this can be achieved using a Ruby wrapper such as RMagick?

I've been reading through the documentation here (http://www.imagemagick.org/RMagick/doc) and trying to figure it out how to even start going about recreating the 'convert' command you guys kindly provided above for the past 4 hours with no luck :(

Any pointers would be much appreciated,
Lee

Re: ImageMagick Guru?

Posted: 2011-11-02T18:58:40-07:00
by anthony
leehUk wrote:Does anyone know how this can be achieved using a Ruby wrapper such as RMagick?
With RMagick, and more of the other API's you will not be able to use the percent escapes. That is something done by the CLI as part of its string to floating point list handling. Basically it means you need to handle the image size lookups and insertion yourself.

The APIs will typically take a direct array of floating point numbers, and optionally (depending language implementation) the number of elements in that floating point array.

Looking at the documentation link provided, I can only see actual documentation for the older "image warping" transforms. Skew, Rotate, Affine Matrix. I see no information for the newer General Image Distortion function.

The only thing I have see was a '-distort' equivalent...
Image#distort(method, arguments, bestfit)
The method is Perspective, bestfit is true (+distort), and teh arguments a floating point array.
Like perl, ruby can automatically determine the number of arguments from the array, which in this case should be 16.

Remember you need to substitute the percent escapes yourself.

Let us know how you go. A ruby equivalent to the above would be nice to publish here.