Page 1 of 1

Need to save monochrome image as grayscale bmp

Posted: 2008-11-13T13:36:54-07:00
by christian
I hope someone can help with this problem.

My program creates an image using black and white colors only. Now I want to save as grayscale bmp, i.e. with 8 bit color depth. Whatever I do, the file will be saved as bilevel, with 1 bit colordepth only.
(The target application will not accept this file.)

The sequence is:
-take a grayscale image as input
-apply "threshold" to make it black and white
-save

Thanks for helping!

Christian

Re: Need to save monochrome image as grayscale bmp

Posted: 2008-11-13T14:02:58-07:00
by el_supremo
Try adding the equivalent of

Code: Select all

-depth 8 -colors 256
before you write the file

Pete

Re: Need to save monochrome image as grayscale bmp

Posted: 2008-11-13T16:51:24-07:00
by christian
I tried this already, it doesn't seem to be that easy. Here is my sample script.

Code: Select all

use Image::Magick;
my $img = Image::Magick -> new();
$img -> Read("original.bmp"); # has 8 bits
print "spa: ", $img ->Get("colorspace"), "\n"; # returns RGB 
print "col: ", $img ->Get("colors"), "\n"; # returns 243
print "typ: ", $img ->Get("type"), "\n"; # returns Grayscale
print "dep: ", $img ->Get("depth"), "\n"; # returns 8
$img->Threshold("50%");
print "spa: ", $img ->Get("colorspace"), "\n"; # returns RGB
print "col: ", $img ->Get("colors"), "\n"; # returns 2
print "typ: ", $img ->Get("type"), "\n"; # returns Bilevel
print "dep: ", $img ->Get("depth"), "\n"; # returns 1
$img -> Write(filename => "threshold.bmp"); # has 24 bits
$img->Set(depth => 8, colors => 8, type => "Grayscale");
print "spa: ", $img ->Get("colorspace"), "\n"; # returns RGB 
print "col: ", $img ->Get("colors"), "\n"; # returns 2
print "typ: ", $img ->Get("type"), "\n"; # returns Bilevel
print "dep: ", $img ->Get("depth"), "\n"; # returns 1
$img -> Write(filename => "set_depth_colors.bmp"); # has 24 bits
$img -> Quantize();
print "spa: ", $img ->Get("colorspace"), "\n"; #returns Gray
print "col: ", $img ->Get("colors"), "\n"; # returns 2
print "typ: ", $img ->Get("type"), "\n"; # returns Bilevel
print "dep: ", $img ->Get("depth"), "\n"; # returns 1
$img -> Write(filename => "quantize.bmp"); # has 1 bit
The call of Set() doesnt have any effect, the files threshold.bmp and set_depth_colors.bmp are identical.
Only after calling Quantize() at least the colorspace changes , the color depth does not.

Christian

Re: Need to save monochrome image as grayscale bmp

Posted: 2008-11-25T04:01:48-07:00
by christian
To close this topic:

I am applying a silly workaround now: save a temporary file as tif, then open it again and save as bmp. No clue why this works, but it does.

Christian