3 回答

TA貢獻(xiàn)2051條經(jīng)驗(yàn) 獲得超10個(gè)贊
下面的程序采用一個(gè)輸入文件名和一個(gè)輸出文件名。它將打開輸入文件,對(duì)其進(jìn)行解碼,將其轉(zhuǎn)換為灰度,然后將其編碼為輸出文件。
該程序并非特定于PNG,但是要支持其他文件格式,您必須導(dǎo)入正確的圖像包。例如,要添加JPEG支持,可以將其添加到導(dǎo)入列表_ "image/jpeg"。
如果你只是想支持PNG,那么你可以使用圖像/ png.Decode,而不是直接image.Decode。
package main
import (
"image"
"image/png" // register the PNG format with the image package
"os"
)
func main() {
infile, err := os.Open(os.Args[1])
if err != nil {
// replace this with real error handling
panic(err.String())
}
defer infile.Close()
// Decode will figure out what type of image is in the file on its own.
// We just have to be sure all the image packages we want are imported.
src, _, err := image.Decode(infile)
if err != nil {
// replace this with real error handling
panic(err.String())
}
// Create a new grayscale image
bounds := src.Bounds()
w, h := bounds.Max.X, bounds.Max.Y
gray := image.NewGray(w, h)
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
oldColor := src.At(x, y)
grayColor := image.GrayColorModel.Convert(oldColor)
gray.Set(x, y, grayColor)
}
}
// Encode the grayscale image to the output file
outfile, err := os.Create(os.Args[2])
if err != nil {
// replace this with real error handling
panic(err.String())
}
defer outfile.Close()
png.Encode(outfile, gray)
}

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個(gè)贊
我自己遇到了這個(gè)問題,并提出了一個(gè)略有不同的解決方案。我介紹了一種新類型Converted,它實(shí)現(xiàn)了image.Image。Converted由原始圖片和組成color.Model。
Converted 每次訪問都會(huì)進(jìn)行轉(zhuǎn)換,這可能會(huì)導(dǎo)致性能稍差,但另一方面,它卻很酷而且很容易組合。
package main
import (
"image"
_ "image/jpeg" // Register JPEG format
"image/png" // Register PNG format
"image/color"
"log"
"os"
)
// Converted implements image.Image, so you can
// pretend that it is the converted image.
type Converted struct {
Img image.Image
Mod color.Model
}
// We return the new color model...
func (c *Converted) ColorModel() color.Model{
return c.Mod
}
// ... but the original bounds
func (c *Converted) Bounds() image.Rectangle{
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{
return c.Mod.Convert(c.Img.At(x,y))
}
func main() {
if len(os.Args) != 3 { log.Fatalln("Needs two arguments")}
infile, err := os.Open(os.Args[1])
if err != nil {
log.Fatalln(err)
}
defer infile.Close()
img, _, err := image.Decode(infile)
if err != nil {
log.Fatalln(err)
}
// Since Converted implements image, this is now a grayscale image
gr := &Converted{img, color.GrayModel}
// Or do something like this to convert it into a black and
// white image.
// bw := []color.Color{color.Black,color.White}
// gr := &Converted{img, color.Palette(bw)}
outfile, err := os.Create(os.Args[2])
if err != nil {
log.Fatalln(err)
}
defer outfile.Close()
png.Encode(outfile,gr)
}

TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超8個(gè)贊
我如下進(jìn)行調(diào)整。可悲的是,它確實(shí)輸出了灰度圖像,但是內(nèi)容雜亂無章,目前我不知道為什么。我在這里提供它供您參考。
package main
import (
"image"
"image/color"
"image/png"
"math"
"os"
)
func main() {
filename := "dir/to/myfile/somefile.png"
infile, err := os.Open(filename)
if err != nil {
// replace this with real error handling
panic(err.Error())
}
defer infile.Close()
// Decode will figure out what type of image is in the file on its own.
// We just have to be sure all the image packages we want are imported.
src, _, err := image.Decode(infile)
if err != nil {
// replace this with real error handling
panic(err.Error())
}
// Create a new grayscale image
bounds := src.Bounds()
w, h := bounds.Max.X, bounds.Max.Y
gray := image.NewGray(image.Rectangle{image.Point{0, 0}, image.Point{w, h}})
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
oldColor := src.At(x, y)
r, g, b, _ := oldColor.RGBA()
avg := 0.2125*float64(r) + 0.7154*float64(g) + 0.0721*float64(b)
grayColor := color.Gray{uint8(math.Ceil(avg))}
gray.Set(x, y, grayColor)
}
}
// Encode the grayscale image to the output file
outfilename := "result.png"
outfile, err := os.Create(outfilename)
if err != nil {
// replace this with real error handling
panic(err.Error())
}
defer outfile.Close()
png.Encode(outfile, gray)
}
順便說一句,golang無法自動(dòng)解碼圖像文件,我們需要直接使用圖像類型的Decode方法。
- 3 回答
- 0 關(guān)注
- 367 瀏覽
添加回答
舉報(bào)