-
Notifications
You must be signed in to change notification settings - Fork 1
/
create.go
67 lines (58 loc) · 1.14 KB
/
create.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"fmt"
"image/png"
"os"
)
// open a png file
func openImage(fileName string) [][]byte {
file, err := os.Open(fileName)
if err != nil {
fmt.Println(err)
return nil
}
defer file.Close()
// decode the png
img, err := png.Decode(file)
if err != nil {
fmt.Println(err)
return nil
}
// get the width and height of the image
bounds := img.Bounds()
width := bounds.Max.X
height := bounds.Max.Y
// create a 2D array of bytes
var pixels [][]byte
for i := 0; i < height; i++ {
pixels = append(pixels, make([]byte, width))
}
// copy the pixels from the image to the 2D array
for i := 0; i < height; i++ {
for j := 0; j < width; j++ {
r, _, _, _ := img.At(j, i).RGBA()
pixels[i][j] = byte(r)
}
}
return pixels
}
// loop over all the pixels
// for each pixel, print whether the pixel is black
// or white
func printPixels(pixels [][]byte) {
for i := 0; i < len(pixels); i++ {
for j := 0; j < len(pixels[i]); j++ {
if pixels[i][j] == 0 {
fmt.Print(" ")
} else {
fmt.Print(" ")
}
}
fmt.Println()
}
}
func main() {
img := openImage("flag.png")
// fmt.Println(img)
printPixels(img)
}