Center Gravity not working?

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
b3and1p
Posts: 25
Joined: 2010-12-07T17:25:08-07:00
Authentication code: 8675308

Center Gravity not working?

Post by b3and1p » 2011-02-06T20:55:04-07:00

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


el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Center Gravity not working?

Post by el_supremo » 2011-02-07T09:05:59-07:00

Use this:
MagickSetImageGravity(magick_wand, CenterGravity);

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.

b3and1p
Posts: 25
Joined: 2010-12-07T17:25:08-07:00
Authentication code: 8675308

Re: Center Gravity not working?

Post by b3and1p » 2011-02-07T13:48:54-07:00

It still crops in the upper left corner instead of in the center.

b3and1p
Posts: 25
Joined: 2010-12-07T17:25:08-07:00
Authentication code: 8675308

Re: Center Gravity not working?

Post by b3and1p » 2011-02-08T09:24:33-07:00

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?

el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Center Gravity not working?

Post by el_supremo » 2011-02-08T10:47:10-07:00

It looks like you'll have to compute the offset yourself.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.

User avatar
magick
Site Admin
Posts: 10996
Joined: 2003-05-31T11:32:55-07:00

Re: Center Gravity not working?

Post by magick » 2011-02-08T12:05:15-07:00

Use MagickTransformImage(). It respects gravity. The crop parameter must be a string (e.g. "120x80").

b3and1p
Posts: 25
Joined: 2010-12-07T17:25:08-07:00
Authentication code: 8675308

Re: Center Gravity not working?

Post by b3and1p » 2011-02-08T13:05:17-07:00

So try using MagickSetImageGravity and MagickTransformImage()?

Does this look right?

Code: Select all

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

el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Center Gravity not working?

Post by el_supremo » 2011-02-08T13:30:27-07:00

That should do it. Works for me.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.

b3and1p
Posts: 25
Joined: 2010-12-07T17:25:08-07:00
Authentication code: 8675308

Re: Center Gravity not working?

Post by b3and1p » 2011-02-08T16:03:59-07:00

hehe, now it is squishing the image to fit in 250x250 instead of cropping it.

el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Center Gravity not working?

Post by el_supremo » 2011-02-08T18:51:26-07:00

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
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.

b3and1p
Posts: 25
Joined: 2010-12-07T17:25:08-07:00
Authentication code: 8675308

Re: Center Gravity not working?

Post by b3and1p » 2011-02-09T09:41:17-07:00

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);

el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Center Gravity not working?

Post by el_supremo » 2011-02-09T10:08:52-07:00

That'll do it.
Which version of IM are you using on which OS?

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.

b3and1p
Posts: 25
Joined: 2010-12-07T17:25:08-07:00
Authentication code: 8675308

Re: Center Gravity not working?

Post by b3and1p » 2011-02-09T10:13:48-07:00

I will have to check the version when I get home, but this is for the iPhone.

Post Reply