Rotating then inserting an image inside another one

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
zoug
Posts: 1
Joined: 2016-05-23T02:20:03-07:00
Authentication code: 1151

Rotating then inserting an image inside another one

Post by zoug »

Hello,

I have this image: http://imgur.com/5CXPTNr (don't know why I can't use \[img\])
I rotate it using a script and then I insert it on top of this one:
Image
However when I rotate the png file

Code: Select all

convert /tmp/input.png -background transparent -rotate $angle /tmp/input2.png
then try to insert it

Code: Select all

convert /tmp/background.jpg /tmp/input2.png -geometry +695+325 -composite /tmp/result.jpg
... the file isn't positionned correctly. It should however always be at the same place (-geometry +695+325), no? Well here are two results I obtain, for example:
Image
and
Image
Can you guys explain me what's going on and how to insert my input2.png always at the same place in the original image, after the rotation?

Thanks a lot
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rotating then inserting an image inside another one

Post by snibgo »

You are positioning the small image by its top-left corner. The problem is that images are rectangular. So when you rotate an image, its dimensions change. The top-left will always be in the same position, but that's not what you want.

Positioning by its centre is far easier. Then the centre of the knob will always be in the same place. The best place seems to be about 16 pixels to the right of the main image, and 5 pixels down.

Code: Select all

convert AXmIJ78.jpg ( "5CXPTNr - Imgur.png" -background None -rotate 90 +write x0.png ) -gravity Center -geometry +16+5 -composite x.png
snibgo's IM pages: im.snibgo.com
User avatar
GeeMack
Posts: 718
Joined: 2015-12-01T22:09:46-07:00
Authentication code: 1151
Location: Central Illinois, USA

Re: Rotating then inserting an image inside another one

Post by GeeMack »

zoug wrote:Can you guys explain me what's going on and how to insert my input2.png always at the same place in the original image, after the rotation?
As snibgo already mentioned, the volume dial in your background image isn't exactly centered in the canvas. You'll be able to easily rotate and center your overlay image if you adjust the center point of that background image and use "-gravity center" to place your composite.

I'm on Windows 7 64 using ImageMagick 6.9.3-9 Q16 x64, but something like this should work on a *nix system as long as you get the escapes and continued lines correct...

Code: Select all

convert -gravity center -background none \
   /tmp/background.jpg \
   -duplicate 1 -geometry +0-8 -composite \
   -duplicate 1 -geometry -14+0 -composite \
   +repage \
   \( /tmp/input2.png -rotate 100 \) -composite \
   /tmp/result.jpg
That sets the gravity to "center". It also sets the background to "none" which will maintain the transparency of your PNG dial image when you rotate and composite it.

Then it calls in your "background.jpg", makes a single copy of it, shifts that copy up 8 pixels, and composites it on the original.

Next it makes a single copy of that, shifts that copy to the left 14 pixels, and composites it on the previous result.

Those "-duplicate" and "-geometry" and "-composite" operations basically shift the center of the dial in the background to the exact center of the working canvas and, in essence, fill the bottom edge and right edge with matching strips of image.

After that you call in the "input2.png" and rotate it as necessary, all inside the parentheses "()" so you aren't rotating the original background, too.

Since the background has been adjusted to center, and you're using "-gravity center", you simply finish by compositing that rotated volume dial onto the background. Give it an output file name, and you're done.

NOTE: Your dial PNG image is about 1 pixel up and to the right of exact center of the canvas. It's a pretty tiny error, but you could get a bit more precise results if you fix that.
Post Reply