Rotate image around it's center

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
attMihail
Posts: 4
Joined: 2016-01-13T07:18:10-07:00
Authentication code: 1151

Rotate image around it's center

Post by attMihail »

Hi All!

- I need some help on a problem: I want to rotate an image around it's center but as far as I have seen I can't do this using Image methods. Actually it has a method "rotate(degree)", but it does a rotate like in this photo that I have attached (which is not Ok fo me).
- Is there a way to do this?

Thank you very much,
Mihail.

PS: I use Visual Studio C++.

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

Re: Rotate image around it's center

Post by snibgo »

I don't know what you mean by "rotate an image around it's center".

When we rotate a rectangular image, we usually want the entire image at a different angle, and the output will be larger than the input, with added triangles.

Or we want the same size output as the input, with corners chopped off.

IM can do either way.

Do you want something different?
snibgo's IM pages: im.snibgo.com
attMihail
Posts: 4
Joined: 2016-01-13T07:18:10-07:00
Authentication code: 1151

Re: Rotate image around it's center

Post by attMihail »

Sorry for not being clear, my english is not very good,

I mean that when I rotate an image, the center of image should not be moved to another location, even if it does not fit in that place. This means that original image and rotated image should have the same center.

This link shows you how an image is rotated around it's center: https://docs.rainmeter.net/tips/rotate- ... ts-center/

Thank you!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rotate image around it's center

Post by snibgo »

So, you want to place one image over another, with the top image rotated about its centre, with the centre at a fixed position on the bottom image.

For example, the bottom image is 1500x1000 red pixels, and the top is the built-in "logo", and we want the centre to always be at (200,200) on the bottom image.

Windows BAT syntax:

Code: Select all

%IM%convert ^
  -size 1500x1000 xc:red ^
  ( -virtual-pixel None logo: +distort SRT 1,0 -repage "+200+200^!" ) ^
  -layers merge m0.png

%IM%convert ^
  -size 1500x1000 xc:red ^
  ( -virtual-pixel None logo: +distort SRT 1,30 -repage "+200+200^!" ) ^
  -layers merge m1.png

%IM%convert ^
  -size 1500x1000 xc:red ^
  ( -virtual-pixel None logo: +distort SRT 1,60 -repage "+200+200^!" ) ^
  -layers merge m2.png
Set the angle (0, 30, 60) to whatever you want.
snibgo's IM pages: im.snibgo.com
attMihail
Posts: 4
Joined: 2016-01-13T07:18:10-07:00
Authentication code: 1151

Re: Rotate image around it's center

Post by attMihail »

@snibgo,

I appreciate your effort, but I need a C++ style solution.

For instance, here is a piece of code made by me that load, rotate and show an image in C++ (using Magick++, of course) :

Code: Select all


#include <Magick++.h>
using namespace Magick;

void CMFCApplicationView::OnDraw(CDC* pDC)
{
	CMFCApplicationDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	if (!pDoc)
		return;

	InitializeMagick("C:/Program Files/GraphicsMagick-1.3.23-Q8");

	Image image;

	
	image.read("myImage.jpg");


                image.rotate(45); // Here, rotate doesn't work as I would expected.


	if (pDC != NULL && image.isValid())
	{

		BITMAPINFOHEADER bmi;

		bmi.biSize = sizeof(BITMAPINFOHEADER);   
		bmi.biWidth = image.columns();         
		bmi.biHeight = (-1)*image.rows();       
		bmi.biPlanes = 1;                         
		bmi.biBitCount = 32;                      
		bmi.biCompression = BI_RGB;               
		bmi.biSizeImage = 0;                      
		bmi.biXPelsPerMeter = 0;                  
		bmi.biYPelsPerMeter = 0;                  
		bmi.biClrUsed = 0;                        
		bmi.biClrImportant = 0;                   


		PixelPacket *pPixels = image.getPixels(0, 0, image.columns(), image.rows());


		StretchDIBits(pDC->m_hDC,
			0,
			0,
			image.columns(),
			image.rows(),
			0,
			0,
			image.columns(),
			image.rows(),
			pPixels,
			(BITMAPINFO *)&bmi,
			DIB_RGB_COLORS,
			SRCCOPY);
	}
}
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rotate image around it's center

Post by snibgo »

You have only one image. You are not placing it over another image. So, when you rotate it, do you want it to become larger (because empty triangles are added) or stay the same size (with triangles chopped off?

"resize" will make the image larger.

"distort SRT" can make either version.
snibgo's IM pages: im.snibgo.com
attMihail
Posts: 4
Joined: 2016-01-13T07:18:10-07:00
Authentication code: 1151

Re: Rotate image around it's center

Post by attMihail »

When I rotate, image should have the same length/width, so, it should not resize, even if the image will be chopped off.
Thx.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Rotate image around it's center

Post by snibgo »

I don't know the C++ interface. In C, we would use DistortImage(). You could do what you do now, and crop to the original size.
snibgo's IM pages: im.snibgo.com
Post Reply