Page 1 of 1

Need help with recreate canvas from multiple images + coords

Posted: 2013-08-09T02:15:56-07:00
by christiaan
Hello,

I've been struggling with this issue for two days now, I hope you guys can help me out.

I want to combine several images onto one fixed size image, where each item has it's own position (gravity northwest) and rotation (from center). I tried multiple options with "-page", "-composite", "-rotate" and "-distort SRT", but I get lost with all those options and settings where one is for the latest image, the other for all following images and so on... I can't get it to work. Everytime I think I have a break-through, something else isn't like the way it's supposed to be... I did search the forums and stackoverflow.com, but can't find a similar problem. Many questions for "collage" point to users we need a montage, but that is not my solution I think.

These are my input images (examples):
  • 1.jpg (scaled to 825x516px, angle 330˚)
  • 2.jpg:
    • z1: (scaled to 320x240px, angle 350˚)
    • z2: (scaled to 400x300px, angle 330˚)
    • z3: (scaled to 320x240px, angle 30˚)
  • 3.jpg (scaled to 800x600px, angle 330˚)
Let's say I have a "canvas" with a white background of 900x400px with those three images composed onto it. All images have their own positions/rotations and it's possible images are partly visible on the canvas (like all images in this example) and some images (2.jpg) can be used multiple times.

Image

I would really like to know how I can produce this result with the least of commands. Hope you guys can help me out or at least point me in the right direction. Thanks in advance!

Code: Select all

Platform: OSX 10.8.4
Version: ImageMagick 6.8.6-6 2013-08-05 Q16 http://www.imagemagick.org

Re: Need help with recreate canvas from multiple images + co

Posted: 2013-08-09T06:40:07-07:00
by snibgo
Many approaches are possible. Here's one (Windows script):

Code: Select all

convert ^
  -size 900x400 xc:White ^
  ( 1.jpg -resize 824x516 -virtual-pixel None +distort SRT "330" -geometry -270-270 ) ^
  -composite ^
  ( 2.jpg -resize 320x240 -virtual-pixel None +distort SRT "350" -geometry -30+200 ) ^
  -composite ^
  ( 2.jpg -resize 400x300 -virtual-pixel None +distort SRT "330" -geometry +100-30 ) ^
  -composite ^
  ( 2.jpg -resize 320x240 -virtual-pixel None +distort SRT "30" -geometry +500-100 ) ^
  -composite ^
  ( 3.jpg -resize 800x600 -virtual-pixel None +distort SRT "330" -geometry +200-50 ) ^
  -composite ^
  x.png

Re: Need help with recreate canvas from multiple images + co

Posted: 2013-08-09T07:00:48-07:00
by christiaan
Thanks snibgo! It works!

You said there are many approaches, I really would like to know the main differences between them and why you chose this one. I'm willing to learn and in my opinion the docs are not a good starting point for a beginner. I was struggling with -geometry and -distort, but it seems simple "(" ")" stop it from altering other images.

Maybe you have some good starting points to learn more about ImageMagick? Particularly working with compositions, texts and filters...

Re: Need help with recreate canvas from multiple images + co

Posted: 2013-08-09T07:31:31-07:00
by GreenKoopa
christiaan wrote: You said there are many approaches, I really would like to know the main differences between them and why you chose this one.
I think snibgo's approach gives a good result, is easy to understand, and is easy to modify as you need. -distort is capable scaling without the needing a separate -resize. It with give slightly better results slightly quicker, but it's not worth it. Or -distort SRT could have been replaced with -rotate.

Another option is to change the mode of -composite. This changes how overlap areas are handled. The default is Over, just like you overlapped real photos. Many blending options exist.
christiaan wrote: I'm willing to learn and in my opinion the docs are not a good starting point for a beginner.
IM is a flexible and complex tool. It took me months of rereading the examples and forums before I found it natural to use. To be fair, parenthesis are discussed comprehensively on the first usage page.

Re: Need help with recreate canvas from multiple images + co

Posted: 2013-08-09T09:12:55-07:00
by snibgo
For the overall structure, I chose to use a series of "-composite" commands. An alternative would be a single "-layers". I had two reasons for this. First, your sample picture indicates that you might want to blend the images in some way, and this might be conveniently done with "-compose blend -composite". Secondly and more prosaically, because I am far more familiar with compose than layers. Most of my work with photos and video is done with composite.

IM is very powerful and capable but allows many complexities. For example, I don't know precisely what you mean by "scaled to 825x516px" etc. I took the easy way out, which resizes the image to fit within a box that is 825x516. If the input image doesn't have exactly this aspect ratio, the result will be smaller than requested on one of the dimensions. For your purposes this might be the correct choice, but other possibilities are available.

I also read the file 2.jpg three times. This is inefficient, but may be okay for your needs, and a more efficient command is also more complex.

The trick with parentheses "()" is to forget about their use in ordinary arithmetic. For example, x=2*(3+4) means do the addition before the multiplication. Not so for IM. In IM, operations are done in the order they are given in the command. The parenthesis limits the scope of each operation to the image(s) within the parenthesis. An operation within a parenthesis can use a result created before the parenthesis, with the keyword "clone". This would be one way of ensuring 2.jpg was read only once.

For learning IM, you might start with:

http://www.imagemagick.org/Usage/
http://www.imagemagick.org/Usage/basics/
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/script/comma ... ptions.php
http://www.imagemagick.org/script/comma ... essing.php
http://www.imagemagick.org/Usage/compose/
http://www.imagemagick.org/Usage/text/
and these forums!

The excellent web pages are mostly written by Anthony, one of the developers and a real expert (unlike me). I feel an alternative tuition could be written, perhaps focused more on photography/video, and perhaps more oriented towards goals instead of commands. I'm vaguely thinking about this, but it's a back-boiler project.

Re: Need help with recreate canvas from multiple images + co

Posted: 2013-08-10T03:00:34-07:00
by christiaan
GreenKoopa wrote:I think snibgo's approach gives a good result, is easy to understand, and is easy to modify as you need. -distort is capable scaling without the needing a separate -resize. It with give slightly better results slightly quicker, but it's not worth it. Or -distort SRT could have been replaced with -rotate.

Another option is to change the mode of -composite. This changes how overlap areas are handled. The default is Over, just like you overlapped real photos. Many blending options exist.
I saw the rotation with distort was more precise than with -rotate, so that's perfect now. But I'm glad I have a good starting point now where the fundamentals are working, I couldn't get that done by myself.
GreenKoopa wrote:IM is a flexible and complex tool. It took me months of rereading the examples and forums before I found it natural to use. To be fair, parenthesis are discussed comprehensively on the first usage page.
I understand it a very powerful tool in many ways and I know there's a lot of work done on the docs. But for a beginner there are too many details. I got flooded by all the information and didn't know where to get started without reading al those details of how it can be done too... As a beginner you need some best practices, from there on we can experiment with other ways of getting results.
snibgo wrote:The parenthesis limits the scope of each operation to the image(s) within the parenthesis. An operation within a parenthesis can use a result created before the parenthesis, with the keyword "clone". This would be one way of ensuring 2.jpg was read only once.
Can you show me an example of how to get this done? There's a chance I may need this. Would it be something like this?

Code: Select all

convert ^
  -size 900x400 xc:White ^
  ( 1.jpg -resize 824x516 -virtual-pixel None +distort SRT "330" -geometry -270-270 ) ^
  -composite ^
  ( 2.jpg -resize 320x240 -virtual-pixel None +distort SRT "350" -geometry -30+200 ) ^
  -composite ^
  ( -clone 1 -resize 400x300 -virtual-pixel None +distort SRT "330" -geometry +100-30 ) ^
  -composite ^
  ( -clone 1 -resize 320x240 -virtual-pixel None +distort SRT "30" -geometry +500-100 ) ^
  -composite ^
  ( 3.jpg -resize 800x600 -virtual-pixel None +distort SRT "330" -geometry +200-50 ) ^
  -composite ^
  x.png
snibgo wrote: For learning IM, you might start with:
I will! Thanks for the tips!

Re: Need help with recreate canvas from multiple images + co

Posted: 2013-08-10T07:22:50-07:00
by snibgo
In your attempt, by the time we get to the clone, the original of 2.jpg is no longer available to be cloned. With more thought, I can't see a simple way to use clone in my script. The difficulty is that composite uses two input files but, if three are available, it will use the third as a mask. This messes up the natural way of using clones.

There is another method of preventing the re-read of 2.jpg: save it (with "-write") into a temporary memory area.

Code: Select all

convert ^
  -size 900x400 xc:White ^
  ( 1.jpg -resize 824x516 -virtual-pixel None +distort SRT "330" -geometry -270-270 ) ^
  -composite ^
  ( 2.jpg -write mpr:saved2 -resize 320x240 -virtual-pixel None +distort SRT "350" -geometry -30+200 ) ^
  -composite ^
  ( mpr:saved2 -resize 400x300 -virtual-pixel None +distort SRT "330" -geometry +100-30 ) ^
  -composite ^
  ( mpr:saved2 -resize 320x240 -virtual-pixel None +distort SRT "30" -geometry +500-100 ) ^
  -composite ^
  ( 3.jpg -resize 800x600 -virtual-pixel None +distort SRT "330" -geometry +200-50 ) ^
  -composite ^
  x1.png
Here is the equivalent command using "-layers". Note that each "-geometry" needs to change to be "-repage".

Code: Select all

convert ^
  -size 900x400 xc:White ^
  -gravity Center ^
  ( 1.jpg -resize 824x516 -virtual-pixel None +distort SRT "330" -repage -270-270 ) ^
  ( 2.jpg -resize 320x240 -virtual-pixel None +distort SRT "350" -repage -30+200 ) ^
  ( 2.jpg -resize 400x300 -virtual-pixel None +distort SRT "330" -repage +100-30 ) ^
  ( 2.jpg -resize 320x240 -virtual-pixel None +distort SRT "30" -repage +500-100 ) ^
  ( 3.jpg -resize 800x600 -virtual-pixel None +distort SRT "330" -repage +200-50 ) ^
  -layers flatten ^
  y.png
This method creates six images, then combines them together. So it uses more memory than the composite method which creates two images, combines them, adds another image and combines it, etc.

And we can easily use clone with "-layers".

Code: Select all

convert ^
  -size 900x400 xc:White ^
  -gravity Center ^
  ( 1.jpg -resize 824x516 -virtual-pixel None +distort SRT "330" -repage -270-270 ) ^
  2.jpg ^
  ( -clone 2 -resize 320x240 -virtual-pixel None +distort SRT "350" -repage -30+200 ) ^
  ( -clone 2 -resize 400x300 -virtual-pixel None +distort SRT "330" -repage +100-30 ) ^
  ( -clone 2 -resize 320x240 -virtual-pixel None +distort SRT "30" -repage +500-100 ) ^
  -delete 2 ^
  ( 3.jpg -resize 800x600 -virtual-pixel None +distort SRT "330" -repage +200-50 ) ^
  -layers flatten ^
  y2.png
This uses seven images, one of which is 2.jpg, unchanged. This is cloned three times. We don't want the unchanged 2.jpg in the final result, so we delete it.

"-clone" counts from zero. So "-clone 0" would be the white canvas, "-clone 1" would be the changed 1.jpg, and "-clone 2" is 2.jpg unchanged.

Re: Need help with recreate canvas from multiple images + co

Posted: 2013-08-13T02:50:47-07:00
by christiaan
Thanks for your thorough explanaition and examples, I will look into it and see what option suits me my needs the most.

You guys helped me big time, appreciate it!

Re: Need help with recreate canvas from multiple images + co

Posted: 2013-09-29T19:41:55-07:00
by anthony
The most basic example for parenthesis the first examples in "Basics"

http://www.imagemagick.org/Usage/basics/#parenthesis

Think of parenthesis as being a separate "convert" command that dumps it results into the main one.
Just remember, any settings are preserved not reset by parenthesis.

IMv7 has a separate set of parenthesis '[' ... ']' to save and restore 'settings'.