3

My programming platform is:

  • Ubuntu 3.19
  • ImageMagick 6.7.7
  • Programming language: php

My code:

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

Then image_output is still 24-bit bmp image.

The thing I would like is convert the 24-bit bmp image to the 8-bit bmp image. Thanks.

Cuongvn08
  • 85
  • 9
  • Please post an example 8-bit image of the type you want to get out of ImageMagick - it seems your idea of an 8-bit image doesn't to match mine :-) – Mark Setchell Jan 11 '17 at 09:19

2 Answers2

1

The setImageDepth just isn't enough. You need to quanitize the image.

Example:Test Script

$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');
NID
  • 3,238
  • 1
  • 17
  • 28
1

You can force an 8-bit palettised BMP image (which is what I presume you mean) like this:

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

That will have a palette of 256 colours (each one 8-bit Red, plus 8-bit Green, plus 8-bit Blue), and each pixel will be represented by its index into that palette.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432