Rotation and Compsite

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
Capt. Hack'em
Posts: 2
Joined: 2014-04-25T07:59:00-07:00
Authentication code: 6789

Rotation and Compsite

Post by Capt. Hack'em »

I am attempting to rotate an image around a certain point and composite it directly onto a larger image. I cannot figure out how to do this. I've come close with distort, but nothing is working.

For example: if I have an image (red.png) that is a red box 75x50, I want to composite it at 50,50 on a 200x200 blank image with a 20 degree rotation. This is what I want:

Image

This was as close as I could come:

convert blank.png red.png -background none -distort SRT "50,50 20" -composite blank.png

But that gave me:

Image

Please help. This is driving me nuts!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rotation and Compsite

Post by snibgo »

Code: Select all

convert -size 200x200 xc:none blank.png

convert -size 75x50 xc:red red.png

convert blank.png ( red.png -virtual-pixel None +distort SRT "0,0 1,20 50,50" ) -layers merge +repage blank4.png
Some problems with your version:

- "distort" is applied to both images, but you want it applied to only one. Parentheses cure this.
- "-distort" clips to the original size of red.png. You want to extend it, so use "+distort" ie plus.
- The default virtual method is "edge", so pixels smear as the image turns.
- "-composite" ignores offsets created by "distort". "-layers merge" takes them into account.
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: Rotation and Compsite

Post by fmw42 »

Here are two ways to do it.

Code: Select all

convert -size 75x50 xc:red -background none -rotate 20 -background none -extent 200x200-50-50 result.png

Code: Select all

convert \( -size 200x200 xc:none \) \( -size 75x50 xc:red -background none -rotate 20 \) -geometry +50+50 -composite result.png

If on windows, remove all the \

See
http://www.imagemagick.org/Usage/canvas/#solid
http://www.imagemagick.org/Usage/basics/#parenthesis
http://www.imagemagick.org/Usage/layers/#convert
http://www.imagemagick.org/Usage/crop/#extent

Also in general
http://www.imagemagick.org/Usage/reference.html
http://www.imagemagick.org/Usage/

Also it is a good idea to always provide your IM version and platform, since syntax varies.

___________

Sorry snibgo, we were posting at the same time as often happens.
Capt. Hack'em
Posts: 2
Joined: 2014-04-25T07:59:00-07:00
Authentication code: 6789

Re: Rotation and Compsite

Post by Capt. Hack'em »

Thank you both. Those worked nicely.

I only have 1 remaining problem. After the rotate, I need the image to be the same size. In other words, if part of the newly rotated image goes outside the bounds, I want it to crop that off not expand the final image size. How would I do that?

Brady

PS...I'm uxing linux centos with ImageMagick 6.8.9-0 Q16 x86_64 2014-04-26 built from source.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Rotation and Compsite

Post by fmw42 »

In other words, if part of the newly rotated image goes outside the bounds, I want it to crop that off not expand the final image size.
My composite command will do that. -composite clips to the bounds of the base image. The default gravity is northwest and the -geometry arguments are then from the top left corner of the base image to the top left corner of the background image. If your geometry arguments are negative or too large (near the bottom or right side), then some of the red will be clipped.

If you set -gravity center, then the geometry arguments are relative to the center of both images, so that the rotated red rectangle will be clipped to the bounds of the background image, if you shift the center to the top left corner of the base image.

Code: Select all

convert \( -size 200x200 xc:none \) \( -size 75x50 xc:red -background none -rotate 45 \) \
-gravity center -geometry -100-100 -composite result.png
see

http://www.imagemagick.org/Usage/layers/#convert
http://www.imagemagick.org/Usage/layers/#layers
Post Reply