问题
Is there a quick tool to convert some image to 2-bits per pixel raw bitmap data?
回答1:
you can use Imagemagick:
if you only want a grayscale channel:
convert image.png -depth 2 data.gray
If you want 2 bits for each red green and blue channels:
convert image.png -depth 2 data.rgb
回答2:
In addition to David's command, I recommend that you set the -size
as well as either of:
convert -depth 2 -size 1x<number-pixels> f.png f.gray
convert -depth 2 -size <number-pixels>x1 f.png f.gray
since these are the simplest outputs to use afterwards.
The problem is that bits are not addressable, so they must be organized into bytes somehow.
I have not found the documentation for this, but a simple example shows the pattern:
printf "%10s" | sed 's/ /\xFF\x00/g' > f.gray
convert -depth 8 -size 1x20 f.gray -depth 2 g.gray
hd g.gray
convert -depth 8 -size 2x10 f.gray -depth 2 g.gray
hd g.gray
convert -depth 8 -size 10x2 f.gray -depth 2 g.gray
hd g.gray
convert -depth 8 -size 20x1 f.gray -depth 2 g.gray
hd g.gray
We've created an input file with bits 1 0
repeated 10 times. The conversion to 2-bit depth gives:
00000000 c0 00 c0 00 c0 00 c0 00 c0 00 c0 00 c0 00 c0 00 |................|
*
00000014
00000000 c0 c0 c0 c0 c0 c0 c0 c0 c0 c0 |..........|
0000000a
00000000 cc cc c0 cc cc c0 |......|
00000006
00000000 cc cc cc cc cc |.....|
00000005
So it seems that if depth < 8, then ImageMagick operates line-wise, and 0 pads missing bits.
I have tested with 1-bit per pixel, and this theory was consistent.
Tested with ImageMagick 6.7.7-10, Ubuntu 14.04.
来源:https://stackoverflow.com/questions/10155092/how-do-i-convert-image-to-2-bit-per-pixel