Page 1 of 1

Center Gravity not working?

Posted: 2011-02-06T20:55:04-07:00
by b3and1p
I have searched this topic a bunch and I have tried everything suggested, but still for some reason the image does not do a crop with a center gravity. Can anyone shed some light on how to fix this problem?

Code: Select all

- (UIImage *)applyEffect7:(UIImage *)i {
	NSLog(@"applying effect seven");
	MagickWandGenesis();
	MagickWand * magick_wand = NewMagickWand();
	MagickWand * mw_extra = NewMagickWand();
	
	NSLog(@"**** BEFOER: %f x %f", i.size.width, i.size.height);
	UIImage * source = [i scaledImageWithFrame:CGRectMake(0, 0, 250, 250)];
	NSLog(@"**** SOURCE: %f x %f", source.size.width, source.size.height);	
	
	NSData * dataObject = UIImageJPEGRepresentation(source, 100); //UIImagePNGRepresentation([UIImage imageNamed:@"iphone.png"]);
	MagickReadImageBlob(magick_wand, [dataObject bytes], [dataObject length]);
	kGoToClean
	
	// convert ./IMG_0001.jpg -gravity Center -crop 1936x1936 -quality 100 step1.jpg
	MagickSetGravity(magick_wand, CenterGravity);
	MagickCropImage(magick_wand, 250, 250, 0, 0);
	kGoToClean


Re: Center Gravity not working?

Posted: 2011-02-07T09:05:59-07:00
by el_supremo
Use this:
MagickSetImageGravity(magick_wand, CenterGravity);

Pete

Re: Center Gravity not working?

Posted: 2011-02-07T13:48:54-07:00
by b3and1p
It still crops in the upper left corner instead of in the center.

Re: Center Gravity not working?

Posted: 2011-02-08T09:24:33-07:00
by b3and1p
Maybe there is something else I am doing wrong? Can someone help me figure this out? Or maybe I just have to manually offset the crop in X and Y so that it is in the center?

Re: Center Gravity not working?

Posted: 2011-02-08T10:47:10-07:00
by el_supremo
It looks like you'll have to compute the offset yourself.

Pete

Re: Center Gravity not working?

Posted: 2011-02-08T12:05:15-07:00
by magick
Use MagickTransformImage(). It respects gravity. The crop parameter must be a string (e.g. "120x80").

Re: Center Gravity not working?

Posted: 2011-02-08T13:05:17-07:00
by b3and1p
So try using MagickSetImageGravity and MagickTransformImage()?

Does this look right?

Code: Select all

   MagickSetImageGravity(magick_wand, CenterGravity);
   MagickTransformImage( magick_wand, "250x250+0+0", "" );

Re: Center Gravity not working?

Posted: 2011-02-08T13:30:27-07:00
by el_supremo
That should do it. Works for me.

Pete

Re: Center Gravity not working?

Posted: 2011-02-08T16:03:59-07:00
by b3and1p
hehe, now it is squishing the image to fit in 250x250 instead of cropping it.

Re: Center Gravity not working?

Posted: 2011-02-08T18:51:26-07:00
by el_supremo
This:

Code: Select all

	MagickSetImageGravity( m_wand, CenterGravity );
	t_wand = MagickTransformImage( m_wand,"", "150x150+0+0");
squishes the whole image down to 150x150.

But this:

Code: Select all

	MagickSetImageGravity( m_wand, CenterGravity );
	t_wand = MagickTransformImage( m_wand,"150x150+0+0","");
crops from the centre of the image.

Pete

Re: Center Gravity not working?

Posted: 2011-02-09T09:41:17-07:00
by b3and1p
Sorry Pete neither worked. Both squished the image so I gave up. It makes more sense just to crop manually anyway. here is the code I used:

First I resized the image down to a minimum of 250x250. Then I cropped doing this:

MagickCropImage(magick_wand, 250, 250, (source.size.width-250)/2, (source.size.height-250)/2);

Re: Center Gravity not working?

Posted: 2011-02-09T10:08:52-07:00
by el_supremo
That'll do it.
Which version of IM are you using on which OS?

Pete

Re: Center Gravity not working?

Posted: 2011-02-09T10:13:48-07:00
by b3and1p
I will have to check the version when I get home, but this is for the iPhone.