我正在嘗試編寫一個簡短的文件,它將讀取一個PNG文件,并與另一個(R,G,B)交換一個通道是可能的選擇。但是我不知道如何從image.At(x,y)返回的color.Color對象中提取整數。一旦我可以使用交換的通道構造新的RGBA顏色,使用image.Set(x,y,color)寫回它可能會更容易。我現在在這里(您幾乎可以跳到最后一個循環):package mainimport ("flag""fmt"//"image""image/color""image/png""os")type Choice struct {value stringvalid bool}func (c *Choice) validate() {goodchoices := []string{"R", "G", "B"}for _, v := range goodchoices { if c.value == v { c.valid = true }}}func main() {var fname stringvar c1 Choicevar c2 Choiceflag.StringVar(&c1.value, "c1", "", "The color channel to swap - R or G or B ")flag.StringVar(&c2.value, "c2", "", "The color channel to swap with - R or G or B ")flag.StringVar(&fname, "f", "", "A .png image (normal map)")flag.Parse()c1.validate()c2.validate()if c1.valid == true && c2.valid == true { fmt.Println("We could proceed..") fmt.Println("Swapping channels:", c1.value, "<->", c2.value, "In", fname) //for testing} else { fmt.Println("Invalid channel... Please use R, G or B.") return}file, err := os.Open(fname)if err != nil { fmt.Println(err) return}defer file.Close()pic, err := png.Decode(file)if err != nil { fmt.Fprintf(os.Stderr, "%s: %v\n", fname, err) return}b := pic.Bounds()for y := b.Min.Y; y < b.Max.Y; y++ { for x := b.Min.X; x < b.Max.X; x++ { col := pic.At(x, y) ???? How do I swap the channels in col ???? }}}我真的是Go語言和編程技術的新手,所以請在回答中考慮一下。謝謝你。
- 1 回答
- 0 關注
- 351 瀏覽
添加回答
舉報
0/150
提交
取消