如何使用Go编程语言读取彩色的png文件并输出为灰度?

如何使用Go编程语言读取彩色.png文件,并将其输出为8位灰度图像?


吃鸡游戏
浏览 317回答 3
3回答

侃侃无极

下面的程序采用一个输入文件名和一个输出文件名。它将打开输入文件,对其进行解码,将其转换为灰度,然后将其编码为输出文件。该程序并非特定于PNG,但是要支持其他文件格式,您必须导入正确的图像包。例如,要添加JPEG支持,可以将其添加到导入列表_ "image/jpeg"。如果你只是想支持PNG,那么你可以使用图像/ png.Decode,而不是直接image.Decode。package mainimport (&nbsp; &nbsp; "image"&nbsp; &nbsp; "image/png" // register the PNG format with the image package&nbsp; &nbsp; "os")func main() {&nbsp; &nbsp; infile, err := os.Open(os.Args[1])&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; // replace this with real error handling&nbsp; &nbsp; &nbsp; &nbsp; panic(err.String())&nbsp; &nbsp; }&nbsp; &nbsp; defer infile.Close()&nbsp; &nbsp; // Decode will figure out what type of image is in the file on its own.&nbsp; &nbsp; // We just have to be sure all the image packages we want are imported.&nbsp; &nbsp; src, _, err := image.Decode(infile)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; // replace this with real error handling&nbsp; &nbsp; &nbsp; &nbsp; panic(err.String())&nbsp; &nbsp; }&nbsp; &nbsp; // Create a new grayscale image&nbsp; &nbsp; bounds := src.Bounds()&nbsp; &nbsp; w, h := bounds.Max.X, bounds.Max.Y&nbsp; &nbsp; gray := image.NewGray(w, h)&nbsp; &nbsp; for x := 0; x < w; x++ {&nbsp; &nbsp; &nbsp; &nbsp; for y := 0; y < h; y++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oldColor := src.At(x, y)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grayColor := image.GrayColorModel.Convert(oldColor)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gray.Set(x, y, grayColor)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Encode the grayscale image to the output file&nbsp; &nbsp; outfile, err := os.Create(os.Args[2])&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; // replace this with real error handling&nbsp; &nbsp; &nbsp; &nbsp; panic(err.String())&nbsp; &nbsp; }&nbsp; &nbsp; defer outfile.Close()&nbsp; &nbsp; png.Encode(outfile, gray)}

慕婉清6462132

我自己遇到了这个问题,并提出了一个略有不同的解决方案。我介绍了一种新类型Converted,它实现了image.Image。Converted由原始图片和组成color.Model。Converted 每次访问都会进行转换,这可能会导致性能稍差,但另一方面,它却很酷而且很容易组合。package mainimport (&nbsp; &nbsp; "image"&nbsp; &nbsp; _ "image/jpeg" // Register JPEG format&nbsp; &nbsp; "image/png"&nbsp; &nbsp; // Register PNG&nbsp; format&nbsp; &nbsp; "image/color"&nbsp; &nbsp; "log"&nbsp; &nbsp; "os")// Converted implements image.Image, so you can// pretend that it is the converted image.type Converted struct {&nbsp; &nbsp; Img image.Image&nbsp; &nbsp; Mod color.Model}// We return the new color model...func (c *Converted) ColorModel() color.Model{&nbsp; &nbsp; return c.Mod}// ... but the original boundsfunc (c *Converted) Bounds() image.Rectangle{&nbsp; &nbsp; return c.Img.Bounds()}// At forwards the call to the original image and// then asks the color model to convert it.func (c *Converted) At(x, y int) color.Color{&nbsp; &nbsp; return c.Mod.Convert(c.Img.At(x,y))}func main() {&nbsp; &nbsp; if len(os.Args) != 3 { log.Fatalln("Needs two arguments")}&nbsp; &nbsp; infile, err := os.Open(os.Args[1])&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatalln(err)&nbsp; &nbsp; }&nbsp; &nbsp; defer infile.Close()&nbsp; &nbsp; img, _, err := image.Decode(infile)&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatalln(err)&nbsp; &nbsp; }&nbsp; &nbsp; // Since Converted implements image, this is now a grayscale image&nbsp; &nbsp; gr := &Converted{img, color.GrayModel}&nbsp; &nbsp; // Or do something like this to convert it into a black and&nbsp; &nbsp; // white image.&nbsp; &nbsp; // bw := []color.Color{color.Black,color.White}&nbsp; &nbsp; // gr := &Converted{img, color.Palette(bw)}&nbsp; &nbsp; outfile, err := os.Create(os.Args[2])&nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; log.Fatalln(err)&nbsp; &nbsp; }&nbsp; &nbsp; defer outfile.Close()&nbsp; &nbsp; png.Encode(outfile,gr)}

拉丁的传说

我如下进行调整。可悲的是,它确实输出了灰度图像,但是内容杂乱无章,目前我不知道为什么。我在这里提供它供您参考。&nbsp; &nbsp; package main&nbsp; &nbsp; import (&nbsp; &nbsp; &nbsp; &nbsp; "image"&nbsp; &nbsp; &nbsp; &nbsp; "image/color"&nbsp; &nbsp; &nbsp; &nbsp; "image/png"&nbsp; &nbsp; &nbsp; &nbsp; "math"&nbsp; &nbsp; &nbsp; &nbsp; "os"&nbsp; &nbsp; )&nbsp; &nbsp; func main() {&nbsp; &nbsp; &nbsp; &nbsp; filename := "dir/to/myfile/somefile.png"&nbsp; &nbsp; &nbsp; &nbsp; infile, err := os.Open(filename)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // replace this with real error handling&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panic(err.Error())&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; defer infile.Close()&nbsp; &nbsp; &nbsp; &nbsp; // Decode will figure out what type of image is in the file on its own.&nbsp; &nbsp; &nbsp; &nbsp; // We just have to be sure all the image packages we want are imported.&nbsp; &nbsp; &nbsp; &nbsp; src, _, err := image.Decode(infile)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // replace this with real error handling&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panic(err.Error())&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Create a new grayscale image&nbsp; &nbsp; &nbsp; &nbsp; bounds := src.Bounds()&nbsp; &nbsp; &nbsp; &nbsp; w, h := bounds.Max.X, bounds.Max.Y&nbsp; &nbsp; &nbsp; &nbsp; gray := image.NewGray(image.Rectangle{image.Point{0, 0}, image.Point{w, h}})&nbsp; &nbsp; &nbsp; &nbsp; for x := 0; x < w; x++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for y := 0; y < h; y++ {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; oldColor := src.At(x, y)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; r, g, b, _ := oldColor.RGBA()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; avg := 0.2125*float64(r) + 0.7154*float64(g) + 0.0721*float64(b)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grayColor := color.Gray{uint8(math.Ceil(avg))}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; gray.Set(x, y, grayColor)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Encode the grayscale image to the output file&nbsp; &nbsp; &nbsp; &nbsp; outfilename := "result.png"&nbsp; &nbsp; &nbsp; &nbsp; outfile, err := os.Create(outfilename)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // replace this with real error handling&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panic(err.Error())&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; defer outfile.Close()&nbsp; &nbsp; &nbsp; &nbsp; png.Encode(outfile, gray)&nbsp; &nbsp; }顺便说一句,golang无法自动解码图像文件,我们需要直接使用图像类型的Decode方法。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go