Page 1 of 1

Placing a rotated image into another

Posted: 2016-08-07T08:56:50-07:00
by christian
Hello,
I try to create a collage by inserting several images into one big image. The smaller image are getting translated and rotated.

Here is an example:
The background image is 200x300 pixels big and white. The smaller image is 100x100 pixels big and grey.
The smaller image should be horizontaly centered in the upper third of the image. The center of the grey image should be at position 100, 100 and rotated by 45 degrees.

If I try this:

Code: Select all

convert collage.png "("  grey.png -virtual-pixel transparent +distort SRT 45 ")" -geometry +50+50 -composite  withPlusDistort.png
I receive this:
Image
Here the position is wrong, but the grey image is not beein cut off.

If I try this:

Code: Select all

convert collage.png "("  grey.png -virtual-pixel transparent -distort SRT 45 ")" -geometry +50+50 -composite  withPlusDistort.png
I receive this:
Image
Here the position is correct, but the grey image has been cut off.

Does anybody know, how to archieve the correct result like seen below?
Image

Thank you very much,
Christian

Re: Placing a rotated image into another

Posted: 2016-08-07T09:07:48-07:00
by snibgo
If you want it centred, why do you have a geometry setting? Why not use "-gravity Center"?

Re: Placing a rotated image into another

Posted: 2016-08-07T09:12:25-07:00
by christian
Hello, I want the grey image translated to a specific point in the background image ( here in the upper middle part, but could be everywhere else, too) and the grey image should be rotated around its center.
If I understand that right, gravity center would not help me here.

Re: Placing a rotated image into another

Posted: 2016-08-07T10:04:12-07:00
by snibgo
If you want it centred horizontally, and up 30 pixels from centre vertically, you can use both gravity and geometry, eg:

Code: Select all

%IM%convert -size 200x300 xc:white ( -size 100x100 xc:gray20 -virtual-pixel transparent +distort SRT 45 ")" +write info: -gravity Center -geometry +0-30 -composite g.png
Your original "-geometry +50+50" with gravity NorthWest (the default) would be fine with no rotation. But rotating an image makes it larger (more pixels in each direction).

Re: Placing a rotated image into another

Posted: 2016-08-07T11:18:27-07:00
by christian
ok, thank you, I hoped for an easier solution.
If I understand your solution right, I have to calculate myself before, how much bigger the image will get by rotating and adjust my translation accordingly.

In this case it would be:

Code: Select all

convert -size 200x300 xc:white \
"(" -size 100x100 xc:gray20 -virtual-pixel transparent +distort SRT 45 ")" -gravity northwest -geometry +30+30 -gravity northwest -composite png:- |display
Note, I used gravity northwest, because I have the position of the grey images as xy coordinates from the top left position.

In this case a 100x100 pixel image rotated by 45 degrees gets a new size of 142x142 pixels.
So instead of a translation of +50+50 pixels I have to translate by +29+29 because the rotated image is 42 pixels bigger in each direction.

Am I right?
Or has anybody an even easier solution?

Re: Placing a rotated image into another

Posted: 2016-08-07T11:31:08-07:00
by snibgo
Well, there are many ways of doing this. Another is:

Code: Select all

convert -size 200x300 xc:white -draw "translate 50,50 translate 50,50 rotate 45 fill gray40 rectangle -50,-50,50,50" g.png
If you are happy to do the calculation, you can combine the two translates. The first one is the (50,50) offset you want. The second is the semi-dimensions of the draw rectangle, before it is rotated.

Re: Placing a rotated image into another

Posted: 2016-08-07T13:08:46-07:00
by christian
Thank you for the second idea.
One thing I modified was that I exchanged the drawing of a rectangle with the drawing of an image:

Code: Select all

convert -size 200x300 xc:white \
 -draw "translate 50,150 translate 50,50 rotate 45  image over -50,-50,100,100 'grey.png'" png:- |display
I noted, that the parameter for drawing a rectangle and an image are different.

But I could not adapt your example for non quadratic images.

Could you please rewrite the example with an image of size 100x150 which left top position before rotating should be +20+30?
I ask because I dont understand in the upper example which 50 means what.

Thank you for your patience.

Re: Placing a rotated image into another

Posted: 2016-08-07T13:53:13-07:00
by snibgo
I don't understand what you want. I suggest you read http://www.imagemagick.org/Usage/draw/#transform and play with the options.

Re: Placing a rotated image into another

Posted: 2016-08-07T14:03:44-07:00
by fmw42
I suggest you rotate the image first, then place it using -gravity center and -geometry to put the center of the image at the offset from the background image center specified using -geometry using convert ... -composite.

Example (unix syntax) using the IM internal image rose:, I have placed it 30 pixels up from the center of the background white image.

Code: Select all

convert \( -size 200x200 xc:white \) \
\( rose: -background none -rotate 45 \) \
-gravity center -geometry +0-30 -composite rose_whitebg.gif
What is your IM version and platform. Please always provide that information, since syntax differs.