Magick++: Why does image.crop() not respect the new image origin after rotation?

Magick++ is an object-oriented C++ interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick++.
Post Reply
H2SO4
Posts: 2
Joined: 2015-09-25T08:28:47-07:00
Authentication code: 1151

Magick++: Why does image.crop() not respect the new image origin after rotation?

Post by H2SO4 »

Hi,

I am trying to understand why image.crop() does not seem to respect the new origin which should be one of the effects of an image.rotate().

Platform: ImageMagick 6.9.2 Q16 x86 DLL on a Win10 x64 workstation. Code compiled with Visual Studio 2015.

In the sample below, I am creating a 141x141 blue square, then rotating the image through 45 degrees to arrive at "AfterRotation.JPG" which is 200x200. If I then crop() the image using "Geometry(100, 100, 0, 0)" the outcome is not what I expect. "AfterCrop.JPG" has the anticipated 100x100 dimensions, but instead of starting at (0, 0) of the rotated image its definition of the origin seems to correspond to the pre-rotation coordinate system. I do not understand that apparent discrepancy.

Code: Select all

#include <string> 
#include <Magick++.h> 
using namespace std;
using namespace Magick;
#pragma comment( lib, "CORE_RL_Magick++_" )	

void main() {
	InitializeMagick("");
	Image image(Geometry("141x141"), Color("blue"));
	image.rotate(45);
	image.write("AfterRotate.jpg");
	image.crop(Geometry(100, 100, 0, 0));
	image.write("AfterCrop.jpg");
}
When the code is run, this is what "AfterRotate.JPG" looks like:

Image

I expect "AfterCrop.JPG" to start at the origin (upper-left corner) of "AfterRotate.JPG". In other words, I expect "AfterCrop.JPG" to be the top-left quadrant of the "AfterRotate.JPG" image. Instead, "AfterCrop.JPG" looks like this:

Image

Even though it is clearly operating on post-rotation buffer contents (the square edges are at a 45 degree angle), crop()'s definition of the origin does not correspond to the top-left corner of "AfterRotation.JPG".

What am I doing wrong?

Thanks in advance for all info and assistance.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Magick++: Why does image.crop() not respect the new image origin after rotation?

Post by snibgo »

Rotate creates image offsets. AfterRotate.jpg does not contain image offsets. You are not cropping AfterRotate.jpg, but the in-memory structure that was used to make AfterRotate.jpg. That structure does contain offsets.

At the command level, use "+repage" to remove offsets. Sorry, I don't know how this is done in Magick++.
snibgo's IM pages: im.snibgo.com
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Magick++: Why does image.crop() not respect the new image origin after rotation?

Post by dlemstra »

It's called repage:

Code: Select all

image.crop(Geometry(100, 100, 0, 0));
image.repage();
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
H2SO4
Posts: 2
Joined: 2015-09-25T08:28:47-07:00
Authentication code: 1151

Re: Magick++: Why does image.crop() not respect the new image origin after rotation?

Post by H2SO4 »

Thank you both, that works. I figured I was missing something simple.
Post Reply