ImageMagick:无法将 24 位 bmp 图像转换为 8 位 bmp 图像



我的编程平台是:

  • 乌班图 3.19
  • 图像魔术 6.7.7
  • 编程语言:php

我的代码:

$img = new Imagick($image_input_24bit_bmp);
$img->setimagedepth(8);
$img->writeImage($image_output);

那么image_output仍然是 24 位 bmp 映像。

我想要的是将 24 位 bmp 图像转换为 8 位 bmp 图像。谢谢。

setImageDepth是不够的

。您需要quanitize图像。

示例:测试脚本

$im = new imagick('stupid.png'); //an image of mine
$im->setImageFormat('PNG8');
$colors = min(255, $im->getImageColors());
$im->quantizeImage($colors, Imagick::COLORSPACE_RGB, 0, false, false );
$im->setImageDepth(8 /* bits */);
$im->writeImage('stupid8.png');

您可以像这样强制使用 8 位 palettized BMP 图像(这就是我假设您的意思):

$im = new imagick('input.bmp');
$im->quantizeImage(256,Imagick::COLORSPACE_RGB,0,false,false);
$im->writeImage('result.bmp');

这将有一个 256 种颜色的调色板(每种颜色为 8 位红色,加上 8 位绿色,加上 8 位蓝色),每个像素将由其索引表示到该调色板中。

最新更新