Need to save monochrome image as grayscale bmp

PerlMagick is an object-oriented Perl interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning PerlMagick.
Post Reply
christian

Need to save monochrome image as grayscale bmp

Post 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
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Need to save monochrome image as grayscale bmp

Post by el_supremo »

Try adding the equivalent of

Code: Select all

-depth 8 -colors 256
before you write the file

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
christian

Re: Need to save monochrome image as grayscale bmp

Post 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
christian

Re: Need to save monochrome image as grayscale bmp

Post 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
Post Reply