my net house


Read Image RGB values using Golang

Using Go-Routine to call image reading multiple times.

package main

import (

// Package image/jpeg is not used explicitly in the code below,
// but is imported for its initialization side-effect, which allows
// image.Decode to understand JPEG formatted images. Uncomment these
// two lines to also understand GIF and PNG images:
// _ “image/gif”
// _ “image/png”
_ “image/jpeg”

func image_reader(c chan int) {

reader, err := os.Open(“interceptor.jpg”)
if err != nil {
defer reader.Close()

// reader := base64.NewDecoder(base64.StdEncoding, strings.NewReader(data))
m, _, err := image.Decode(reader)
if err != nil {
bounds := m.Bounds()

var histogram [16][4]int
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x >12][0]++

// Print the results.
fmt.Printf(“%-14s %6s %6s %6s %6s\n”, “bin”, “red”, “green”, “blue”, “alpha”)
for i, x := range histogram {
c <- i
fmt.Printf("0x%04x-0x%04x: %6d %6d %6d %6d\n", i<<12, (i+1)<<12-1, x[0], x[1], x[2], x[3])


func main() {
c := make(chan int)
go image_reader(c)
x := <-c // receive from c
fmt.Println("all done from here as well")


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: