-
Notifications
You must be signed in to change notification settings - Fork 0
/
img_colors.go
51 lines (42 loc) · 980 Bytes
/
img_colors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package Img_colors
import (
"fmt"
"image"
_ "image/jpeg"
_ "image/png" // import this package to decode PNGs
"os"
)
// adding comments
func ImgColors() {
reader, err := os.Open("colors.jpg")
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
defer reader.Close()
img, _, err := image.Decode(reader)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}
bounds := img.Bounds()
width, height := bounds.Max.X, bounds.Max.Y
// Open output file for writing
file, errs := os.Create("colors_pixel_counts.txt")
if errs != nil {
fmt.Println("Failed to create file:", errs)
return
}
defer file.Close()
fmt.Println("Processing Pixel Colors")
// Prints out results of the decoder
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
color := img.At(x, y)
r, g, b, _ := color.RGBA()
values := fmt.Sprintf("Pixel at (%d, %d) - R: %d, G: %d, B: %d\n", x, y, r>>8, g>>8, b>>8)
file.WriteString(values)
values = ""
}
}
}