Placing a rotated image into another

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
christian
Posts: 4
Joined: 2016-08-07T08:31:53-07:00
Authentication code: 1151

Placing a rotated image into another

Post 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
Last edited by christian on 2016-08-07T09:13:21-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Placing a rotated image into another

Post by snibgo »

If you want it centred, why do you have a geometry setting? Why not use "-gravity Center"?
snibgo's IM pages: im.snibgo.com
christian
Posts: 4
Joined: 2016-08-07T08:31:53-07:00
Authentication code: 1151

Re: Placing a rotated image into another

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Placing a rotated image into another

Post 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).
snibgo's IM pages: im.snibgo.com
christian
Posts: 4
Joined: 2016-08-07T08:31:53-07:00
Authentication code: 1151

Re: Placing a rotated image into another

Post 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?
Last edited by christian on 2016-08-07T11:33:05-07:00, edited 1 time in total.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Placing a rotated image into another

Post 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.
snibgo's IM pages: im.snibgo.com
christian
Posts: 4
Joined: 2016-08-07T08:31:53-07:00
Authentication code: 1151

Re: Placing a rotated image into another

Post 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.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Placing a rotated image into another

Post 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.
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: Placing a rotated image into another

Post 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.
Post Reply