converting images to indexed 2-bit grayscale BMP

Viewed 28743

First of all, my question is different to How do I convert image to 2-bit per pixel? and unfortunately its solution does not work in my case...

I need to convert images to 2-bit per pixel grayscale BMP format. The sample image has the following properties:

Color Model: RGB
Depth: 4
Is Indexed: 1
Dimension: 800x600
Size: 240,070 bytes (4 bits per pixel but only last 2 bits are used to identify the gray scales as 0/1/2/3 in decimal or 0000/0001/0010/0011 in binary, plus 70 bytes BMP metadata or whatever)

sample

The Hex values of the beginning part of the sample BMP image: head of sample image

The 3s represent white pixels at the beginning of the image. Further down there are some 0s, 1s and 2s representing black, dark gray and light gray: mid-part of sample image

With the command below,

convert pic.png -colorspace gray +matte -depth 2 out.bmp

I can get visually correct 4-level grayscale image, but wrong depth or size per pixel:

Color Model: RGB
Depth: 8 (expect 4)
Dimension: 800x504
Size: 1,209,738 bytes (something like 3 bytes per pixel, plus metadata)
(no mention of indexed colour space)

conversion outcome

Please help...

6 Answers

This works for me in Imagemagick 6.9.10.23 Q16 Mac OSX Sierra

Input: enter image description here

convert logo.png -colorspace gray -depth 2 -type truecolor logo_depth8_gray_rgb.bmp

enter image description here

Adding -type truecolor converts the image to RGB, but in gray tones as per the -colorspace gray. And the depth 2 creates only 4 colors in the histogram.

identify -verbose logo_depth8_gray_rgb.bmp

Image:
  Filename: logo_depth8_gray_rgb.bmp
  Format: BMP (Microsoft Windows bitmap image)
  Class: DirectClass
  Geometry: 640x480+0+0
  Units: PixelsPerCentimeter
  Colorspace: sRGB
  Type: Grayscale
  Base type: Undefined
  Endianness: Undefined
  Depth: 8/2-bit
  Channel depth:
    red: 2-bit
    green: 2-bit
    blue: 2-bit
  Channel statistics:
    Pixels: 307200
    Red:
      min: 0  (0)
      max: 255 (1)
      mean: 228.414 (0.895742)
      standard deviation: 66.9712 (0.262632)
      kurtosis: 4.29925
      skewness: -2.38354
      entropy: 0.417933
    Green:
      min: 0  (0)
      max: 255 (1)
      mean: 228.414 (0.895742)
      standard deviation: 66.9712 (0.262632)
      kurtosis: 4.29925
      skewness: -2.38354
      entropy: 0.417933
    Blue:
      min: 0  (0)
      max: 255 (1)
      mean: 228.414 (0.895742)
      standard deviation: 66.9712 (0.262632)
      kurtosis: 4.29925
      skewness: -2.38354
      entropy: 0.417933
  Image statistics:
    Overall:
      min: 0  (0)
      max: 255 (1)
      mean: 228.414 (0.895742)
      standard deviation: 66.9712 (0.262632)
      kurtosis: 4.29928
      skewness: -2.38355
      entropy: 0.417933
  Colors: 4 <--------
  Histogram: <--------
    12730: (0,0,0) #000000 black
    24146: (85,85,85) #555555 srgb(85,85,85)
    9602: (170,170,170) #AAAAAA srgb(170,170,170)
    260722: (255,255,255) #FFFFFF white

You can just use this:

convert in.jpg -colorspace gray +matte -colors 2 -depth 1 -resize '640x384>' pgm:- > out.bmp**

I too have this epaper display. After alot of trial and error, I was able to correctly convert the images using ImageMagick using the following command:

convert -verbose INPUT.BMP -resize 300x300 -monochrome -colorspace sRGB -colors 2 -depth 1 BMP3:OUTPUT.BMP
Related