Difficulties with merging commands

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
tenlethar

Difficulties with merging commands

Post by tenlethar »

Hello,

i have a problem understanding ImageMagick in the following case:

Basically, what i do is to take a blank image of a certain size, then composite various copies of the same source image onto it. Each of those images can end up on the blank background in different sizes, with different cropping and rotation.

I had it all working before, but it was very slow, because i first created a blank image with a convert command. Then i used another convert command to resize/crop/rotate each of the images that should be placed onto the blank image. And then i used a third convert command to place the resized/cropped/rotated image onto the blank image.

Obviously this had to be changed. I ended up finding the -composite option for convert and decided to use it. The following example is the command that my tool has generated for layering two images onto a blank background.

Code: Select all

convert -size 1063x1500 -density 300x300 xc:white \
"files/test/test.jpg" -geometry 508x661^^+12+791 "files/test__processed/test.jpg" -composite \
"files/test/test.jpg" -geometry 508x661^^+543+791 "files/test__processed/test.jpg" -composite 
It works fine so far. However, the images are not cropped and not rotated. I wanted to add my old crop + rotate command (i assign them to PHP vars which i then include in my IM call):

Code: Select all

$crop = " -crop " . $width. "x" . $height . "+0+0 ";
$rotate = " -rotate 90 ";
But wherever in my syntax i place these commands, either before -geometry, or after it, either before the first file, or after it - no matter where i place it, i end up with the wrong result. In one case imagemagick seemed to have rotated the very base image (the one i create with xc:white).

I ran out of options here.

Maybe someone can help?

Thank you very much!

Edit: I am using IM 6.4.2 Q16 on Windows XP SP2
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Difficulties with merging commands

Post by anthony »

-geometry is a special command who's prime purpose is to set the gravity effected offset for two image alpha composition.

The 'resize' capability of -geometry is special in that it only resizes the last image in the current image sequence, and was arrange this way for backwards compatibility with older IM v5 usage.

It is the ONLY operator to do this, making it the only command that is both a operator (for resize last image) and setting (set composition offset). It is a special legacy command.

ALL other operators either work on each image, in the image sequence, individually, or applies to the whole image sequence,
usually compressing multiple images to one image, or with previous images effecting the handling of later images.

This includes -crop and -resize!

There are however many ways to compose multiple images, all of which are exampled in IM examples, Layers of Multiple Images.
http://www.imagemagick.org/Usage/layers/

I suggest three methods that may be useful for you..

First in-line with your one-command attempt to compose
use parenthesis to work on JUST the one image). I assume you are working in DOS, as you doubled the hat '^' symbol. So I'll use the same symbol to escape end-of-line characters.

Code: Select all

convert -size 1063x1500 -density 300x300 xc:white ^
   (  "files/test/test.jpg" -resize  508x661^^   ^
        -gravity center  -extent 508x661     ) ^
   +gravity -geometry +12+791  -composite ^
   (  "files/test/test.jpg" -resize  508x661^^   ^
        -gravity center  -extent 508x661     ) ^
   +gravity -geometry +543+791  -composite ^
   "files/test/result.jpg"
The parenthesis limits the -resize and -extent to just the one image.
http://www.imagemagick.org/Usage/basics/#parenthesis


The second method is to ignore the canvas at the start, just read in and process each image, then 'flatten' it all onto a final canvas.

Code: Select all

convert -density 300x300 ^
   (  "files/test/test.jpg" -resize  508x661^^   ^
        -gravity center  -extent 508x661   -repage  +12+791 ) ^
   (  "files/test/test.jpg" -resize  508x661^^   ^
        -gravity center  -extent 508x661  -repage  +543+791  ) ^
   -repage 1063x1500  -background white -flatten  ^
   "files/test/result.jpg"
The third and final method may not however work for you as it involves a PIPE line. I will not give an example, but instead refer you to the examples for Layer Merge Examples
http://www.imagemagick.org/Usage/layers/#examples

This still uses separate convert commands to pre-process each image, and even set its placement offset, but the feeds each image via a pipe-line to a final command to overlay all the separate images into one final canvas image.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
tenlethar

Re: Difficulties with merging commands

Post by tenlethar »

Thank you, Anthony! I will try this out in a few days!
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Difficulties with merging commands

Post by Bonzo »

You also mention php - how are you actually running this code ?

Anyway another method using php and saving the image into the memory only ( crops the image and rotates first so all copys of the image are cropped and rotated ):

Code: Select all

<?php

$crop = " -crop " . $width. "x" . $height . "+0+0 ";
$rotate = " -rotate 90 ";

$cmd = "cow.jpg $crop $rotate -write mpr:image +delete ".
  " -size 1063x1500 -density 300x300 xc:white ".
  " ( mpr:image -resize 508x661^ -gravity center -extent 508x661 ) ".
  " +gravity -geometry +12+791 -composite ".
  " ( mpr:image -resize 508x661^ -gravity center -extent 508x661 ) ".
  " +gravity -geometry +543+791 -composite ";

exec("convert $cmd output.jpg");

?> 
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Difficulties with merging commands

Post by anthony »

For more info on MPR see, IM Examples, File handling, MPR
http://www.imagemagick.org/Usage/files/#mpr

MPR is very useful, It not only can save an image (though you can not 're-save'
to an existing MPR image, this may be a bug) but a whole image sequence.

That is it can be used as a type of multi-image -clone.

I use that to good effect for doing multi-image layer alpha compositions
(merging to separate image sequences) all in a single command.
http://www.imagemagick.org/Usage/anim_mods/#composite
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
tenlethar

Re: Difficulties with merging commands

Post by tenlethar »

Alright. I have tried out the first solution of using brackets in order to group the commands - then using -composite to finish up.

It worked like a charm! Thank you very much, Anthony!

Bonzo: Thanks for the tip! I am doing nothing too special to run the code - i just loop through a set of images with target coordinates and dimensions defined in a template, then i place all the images onto a blank image of a certain size.

Kind regards!
Post Reply