Programmatically convert RGB image to 1-bit Grayscale bitmap

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
DrussSnaga
Posts: 1
Joined: 2012-11-17T12:17:51-07:00
Authentication code: 6789

Programmatically convert RGB image to 1-bit Grayscale bitmap

Post by DrussSnaga »

I'm trying to programmatically convert a bitmap RGB image to 1-bit Grayscale (b/w) bitmap image using PHP (version: 5.2.13) and ImageMagick (version: 6.7.8-7-Q16). The input image is bitmap and is generated by means of the ImageMagick function:

Code: Select all

bool Imagick::setFormat ( string $format )
where $format = 'bmp2'

The following code used to work in the past (older version of ImageMagick... don't remember which one), but it's not working anymore in the current environment:

Code: Select all

private function monochrome() {
    if (isset($this->image)) {
        try {
            // reduce image colors to 2 (black and white)
            $this->image->quantizeImage(2, Imagick::COLORSPACE_GRAY, 5, false, true);

            // reduce image depth to 1 bit per pixel
            $this->image->setImageDepth(1);
            $this->image->setImageChannelDepth(Imagick::CHANNEL_ALL, 1);

            // set image type to monochrome (2 colors: black and white only)
            $this->image->setImageType(Imagick::IMGTYPE_BILEVEL);
        }
        catch (ImagickException $ie) {
            throw $ie;
        }
    }
    else {
        throw new Exception("No image object");
    }
}
The problem is that the image produced is still in the RGB color space.

I also tried:

Code: Select all

$this->image->setImageColorSpace(Imagick::COLORSPACE_GRAY);
but the result does not change.

My goal is to generate the smallest possible, black and white, bitmap image for a signature capture application. I know there are better image formats than bitmap, but the generated image has to be compatible with the old Access 97. That is why 'bmp2' is the choice for the image format.

Any ideas? Thanks!
Post Reply