使用ImageMagick(在iPhone上)的C API转换为单色?

时间:2022-06-29 08:58:08

I am using the code referenced in this post, but would like to switch to an ImageMagick C-API based solution, as I want to standardize on a single image manipulation library, and need IM for some other tasks.

我正在使用本文引用的代码,但希望切换到ImageMagick C-API的解决方案,因为我想在单个图像操作库中进行标准化,并且需要IM来完成其他任务。

I can find tons of examples of using the convert command line tool, but none on how to do the monochrome conversion in code.

我可以找到大量使用convert命令行工具的例子,但是没有一个关于如何在代码中进行单色转换的例子。

Any sample code out there?

有任何样本代码吗?

1 个解决方案

#1


6  

You can achieve monochrome conversion, as described here, with MagickQuantizeImage function. I'm not quite familiar with dithering images, but an example may look like the following.

你可以用MagickQuantizeImage函数来实现单色转换。我不太熟悉抖动图像,但是一个例子可能看起来像下面这样。

#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);
  MagickWand*();
  return 0;
}

This can give you a rather speckled image at times.

这可以给你一个很有斑点的图像。

使用ImageMagick(在iPhone上)的C API转换为单色?

Adjusting depth, color-number, and/or disabling dithering may give you results closer to what you would expect from the examples provided.

调整深度、颜色数和/或禁用抖动可能会使您的结果更接近您所期望的示例。

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

Like such... 使用ImageMagick(在iPhone上)的C API转换为单色?

像这样…

For iOS

Not much effort is needed to port the example code to iOS. NextStep/Objective-c methods are compatible with MagickWand library. The following example uses a temporary file to store the monochrome image, but I'm sure there is a better way to pass Magick image-data directly to UImage object.

要将示例代码移植到iOS,不需要做太多的工作。NextStep/Objective-c方法与MagickWand库兼容。下面的示例使用一个临时文件来存储单色图像,但我确信有更好的方法将Magick图像数据直接传递给UImage对象。

// 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);
    MagickWand*();
}

@end

使用ImageMagick(在iPhone上)的C API转换为单色?

#1


6  

You can achieve monochrome conversion, as described here, with MagickQuantizeImage function. I'm not quite familiar with dithering images, but an example may look like the following.

你可以用MagickQuantizeImage函数来实现单色转换。我不太熟悉抖动图像,但是一个例子可能看起来像下面这样。

#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);
  MagickWand*();
  return 0;
}

This can give you a rather speckled image at times.

这可以给你一个很有斑点的图像。

使用ImageMagick(在iPhone上)的C API转换为单色?

Adjusting depth, color-number, and/or disabling dithering may give you results closer to what you would expect from the examples provided.

调整深度、颜色数和/或禁用抖动可能会使您的结果更接近您所期望的示例。

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

Like such... 使用ImageMagick(在iPhone上)的C API转换为单色?

像这样…

For iOS

Not much effort is needed to port the example code to iOS. NextStep/Objective-c methods are compatible with MagickWand library. The following example uses a temporary file to store the monochrome image, but I'm sure there is a better way to pass Magick image-data directly to UImage object.

要将示例代码移植到iOS,不需要做太多的工作。NextStep/Objective-c方法与MagickWand库兼容。下面的示例使用一个临时文件来存储单色图像,但我确信有更好的方法将Magick图像数据直接传递给UImage对象。

// 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);
    MagickWand*();
}

@end

使用ImageMagick(在iPhone上)的C API转换为单色?