Writing an Image Manipulation Library in Go — Part 2

Koray Göçmen
2 min readOct 14, 2020

Part 2 — Grayscaling images

How I did grayscaling for images: There are a few different algorithms to change an image to grayscale. Averaging, Luma and Desaturation are the ones I decided to focus on. But there are more. You can read about them on Tanner Helland’s blog: Tanner Helland — Seven grayscale conversion algorithms

Side note: I chose to make these functions associated with my Image type. After creating an Image type from an image file, I can call these functions on the objects. This is the “object-oriented” method of doing things in Go. (well, not really: go is not an object-oriented language).

// Grayscale turns the images to grayscale.
func (img *Image) Grayscale(algorithm int) *Image {
var wg sync.WaitGroup

for rowIndex := 0; rowIndex < img.Height; rowIndex++ {
wg.Add(1)

go (func(rowIndex int, img *Image) {
for colIndex := 0; colIndex < img.Width; colIndex++ {
pixel := img.Pixels[rowIndex][colIndex]

var gray int
if algorithm == GrayscaleLuma {
gray = int(float32(pixel.R)*0.2126 + float32(pixel.G)*0.7152 + float32(pixel.B)*0.0722)
} else if algorithm == GrayscaleDesaturation {
gray = int((max(pixel.R, pixel.G, pixel.B) + min(pixel.R, pixel.G, pixel.B)) / 2)
} else {
gray = int((pixel.R + pixel.G + pixel.B) / 3)
}
pixel.Set("R", gray)
pixel.Set("G", gray)
pixel.Set("B", gray)

img.Pixels[rowIndex][colIndex] = pixel
}
wg.Done()
})(rowIndex, img)
}

wg.Wait()
return img
}

This function actually manipulates all rows in parallel. By using `sync.WaitGroup` and `go` keyword, I am able to do things concurrently. I actually wrote a blog post about concurrency in go. Concurrency and mutex locks in Go

The main grayscaling algorithm is very simple. For example, when using Averaging algorithm, to change a pixel to a grayscale pixel, I can take the average of the R, G, B values and assign this value to all 3 pixels. When this is done on all pixels, the image will now be a grayscale (black and white) image.

For other algorithms:

Desaturation
gray = (max(R, G, B) + min(R, G, B)) / 2
assign gray to all 3 pixels.

Luma
gray = R*0.2126 + G*0.7152 + B*0.0722
assign gray to all 3 pixels.

That’s pretty much it for grayscaling. I am going to talk about filtering next:

--

--

Koray Göçmen

University of Toronto, Computer Engineering, architected and implemented reliable infrastructures and worked as the lead developer for multiple startups.