生成 1 位深度的 BMP 图像

时间:2021-01-25 11:45:33

标签: image go bitmap bmp

我需要创建一个颜色深度为1位的单色位图图像,例如:

$ file exampleMono.bmp 
> exampleMono.bmp: PC bitmap, Windows 3.x format, 640 x 1000 x 1

在 Go 中,我可以使用以下内容生成一个简单的 bmp 图像:

tempPal := color.Palette([]color.Color{
    color.Black, color.White,
})
img := image.NewPaletted(image.Rect(640, 1000,0,0), tempPal)
filename := fmt.Sprintf("img-%d.bmp", time.Now().Unix())
file, _ := os.Create(filename)
bmp.Encode(file, img)
file.close

但是,生成的 bmp 文件的颜色深度为 8 位。

$ file img-1611574755.bmp 
> img-1611574755.bmp: PC bitmap, Windows 3.x format, 640 x 1000 x 8

我已经浏览了 Go 中的 bmp、图像和绘图包,但没有发现任何可以将颜色深度设置为 1 位的功能。是否可以在 Go 中实现我需要的功能?如果可以,如何实现?

1 个答案:

答案 0 :(得分:1)

正如你从 here 向下看到的; golang.org/x/image/bmpEncode() 在任何图像模式下都不支持 bpp=1

Encode() 中取出片段并编写您自己的单一用途编码函数作为 BMP 文件格式应该很容易,并且它的所有变体都有很好的文档记录。 BMP 文件格式规范:WikipediaFileFormat

相关问题