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 »

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 »

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 »

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 »

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 »

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: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Center Gravity not working?

Post by magick »

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 »

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 »

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 »

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 »

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 »

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 »

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 »

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