Using the C API for ImageMagick (on iPhone?) To convert to monochrome?

I use the code indicated in this post , but would like to switch to the solution based on the ImageMagick C-API, since I want to standardize the manipulation library on one image and the need for IM for some other tasks.

I can find many examples of using the command line tool for converting, but none of them describes how to do monochrome conversion in code.

Any sample code?

+7
ios objective-c imagemagick
source share
1 answer

You can achieve monochrome conversion, as described here , MagickQuantizeImage . I am not completely familiar with anti-aliasing images, but an example might look like this.

#include <wand/MagickWand.h> int main(int argc, char **argv) { const size_t number_colors = 2; const size_t treedepth = 1; MagickWandGenesis(); MagickWand *wand = NULL; wand = NewMagickWand(); MagickReadImage(wand,"source.png"); MagickQuantizeImage( wand, // MagickWand number_colors, // Target number colors GRAYColorspace, // Colorspace treedepth, // Optimal depth MagickTrue, // Dither MagickFalse // Quantization error ); MagickWriteImage(wand,"out.png"); if(wand)wand = DestroyMagickWand(wand); MagickWandTerminus(); return 0; } 

This may give you some spotty image.

Monochrome with dither

Setting the depth, color number, and / or disabling anti-aliasing may give you results closer to what you would expect from the above examples.

 MagickQuantizeImage( wand, // MagickWand number_colors, // Target number colors GRAYColorspace, // Colorspace treedepth, // Optimal depth MagickFalse, // No-dither MagickFalse // Quantization error ); 

Like this ... Monochrome without dither

For iOS

It doesn’t take much effort to transfer the sample code to iOS. NextStep / Objective-c methods are compatible with the MagickWand library. The following example uses a temporary file to store a monochrome image, but I am sure that there is a better way to transfer the Magee graphic data directly to a UImage object.

 // MyViewController.h #import <UIKit/UIKit.h> #import <wand/MagickWand.h> @interface MyViewController : UIViewController @property (retain, nonatomic) IBOutlet UIImageView *imageView; @property (retain, nonatomic) MagickWand *wand; @end // MyViewController.m #import "MyViewController.h" @implementation MyViewController - (void)viewDidLoad { [super viewDidLoad]; MagickWandGenesis(); self.wand = NewMagickWand(); [self drawMonochromeImage:@"logo:"]; } -(void)drawMonochromeImage:(NSString *)filePath { // Create temporary file NSString *tempFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"logo.jpg"]; // Read given image with C-string MagickReadImage(self.wand, [filePath cStringUsingEncoding:NSASCIIStringEncoding] ); // Monochrome image MagickQuantizeImage(self.wand,2,GRAYColorspace,1,MagickFalse,MagickFalse); // Write to temporary file MagickWriteImage(self.wand, [tempFilePath cStringUsingEncoding:NSASCIIStringEncoding] ); // Load UIImage from temporary file UIImage *imgObj = [UIImage imageWithContentsOfFile:tempFilePath]; // Display on device [self.imageView setImage:imgObj]; [self.imageView setContentMode:UIViewContentModeScaleAspectFit]; } -(void)viewDidUnload { // Clean-up if (self.wand) self.wand = DestroyMagickWand(self.wand); MagickWandTerminus(); } @end 

iOS Simulator

+6
source share

All Articles