How do I convert image to 2-bit per pixel?

允我心安 提交于 2020-01-13 19:36:26

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!