2014-04-16 58 views
0

我想下面的命令ImageMagick轉換成Objective-C的:翻譯ImageMagick的命令到iOS

convert photo.png -posterize 6 photo2.png 

基本上,我只是想採取一些UIImage並應用簡單的色調分離效果吧。我編寫了下面的代碼,它對圖像沒有任何影響。它不會拋出任何例外或失敗。我有一個令人失望的問題,我要麼使用命令參數或圖像輸入/輸出。有沒有人有任何建議如何解決這個問題?

MagickWandGenesis(); 
MagickWand *wand = NewMagickWand(); 
NSData *data = UIImagePNGRepresentation(self.originalImage); 
MagickReadImageBlob(wand, [data bytes], [data length]); 

int arg_count = 2; 
char *args[] = { "-posterize", "6", NULL}; 

ImageInfo *image_info = AcquireImageInfo(); 
ExceptionInfo *exception = AcquireExceptionInfo(); 

MagickBooleanType status = ConvertImageCommand(image_info, arg_count, args, NULL, exception); 

if (exception->severity != UndefinedException) 
{ 
    status = MagickTrue; 
    CatchException(exception); 
} 

if (status == MagickFalse) 
{ 
    NSLog(@"FAIL"); 
} 

self.imageView.image = self.originalImage; 

image_info=DestroyImageInfo(image_info); 
exception=DestroyExceptionInfo(exception); 
DestroyMagickWand(wand); 
MagickWandTerminus(); 

回答

1

我相信你在尋找Imagemagick的MagickPosterizeImage

MagickWandGenesis(); 
MagickWand *wand = NewMagickWand(); 
NSData *data = UIImagePNGRepresentation(self.originalImage); 
MagickReadImageBlob(wand, [data bytes], [data length]); 

MagickBooleanType = status; 

status = MagickPosterizeImage(wand,6,MagickFalse); 
if (status == MagickFalse) 
{ 
    NSLog(@"FAIL"); 
} 

// Convert wand back to UIImage 
unsigned char * c_blob; 
size_t data_length; 
c_blob = MagickGetImageBlob(wand,&data_length); 
data = [NSData dataWithBytes:c_blob length:data_length]; 
self.imageView.image = [UIImage imageWithData:data]; 

DestroyMagickWand(wand); 
MagickWandTerminus(); 

MagickPosterizeImage

還有MagickOrderedPosterizeImage & MagickOrderedPosterizeImageChannel微調抖動閾值和目標顏色通道。

MagickOrderedPosterizeImageChannel

+0

謝謝你,完美的工作。你碰巧知道爲什麼ConvertImageCommand可能無法工作?我仍然需要使用這個函數來轉換其他一些命令,我​​不相信直接API調用像posterize。 –

+1

您需要添加'MagickCommandGenesis'來使用'ConvertImageCommand'。但是對於性能和複雜性的爭論,直接使用MagickWand/Core方法。這很簡單。 – emcconville

+0

再次感謝,真的很感激! –