Image processing with Go

Programming Snapshot – Go

© Lead Image © Martin Malchev, 123RF.com

© Lead Image © Martin Malchev, 123RF.com

Article from Issue 221/2019
Author(s):

Go comes with an image-processing toolkit right out of the box. In this month's column, Mike Schilli explains how to walk through a photo's pixels to detect the foreground by comparing values against a threshold and shows how to manipulate the original by creating a nice looking silhouette.

Until recently, my headshot at the end of every "Programming Snapshot" article showed a much younger version of myself, dating back 15 years, so I grudgingly shot a new one the other day. While doing this, the idea occurred to me to try out my new favorite language Go's suitability for image processing. How hard could it be to generate an artful silhouette of the person shown in the photo?

Of course, with some Gimp skills this could be done quite quickly. However, what is far more interesting is the question of how an image processing program walks through the pixels of Figure 1 and finds out which of them actually belong to the person (the foreground) and which to the lighter background. When a foreground pixel is found, the algorithm can then go ahead and set it to black, which in the digital world means it has its red, green, and blue channels set to 0,0,0. Easy enough, right?

Finding the Foreground

With a light background and a significantly darker object in the foreground, the program in Listing 1 [1] simply finds all pixels whose brightness lies below a previously defined threshold value and blacks them out completely. The Darken() function accepts a draw.Image type structure from line 9, including the width and height of the image in the width and height parameters in pixels.

[...]

Use Express-Checkout link below to read the full article (PDF).

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Straight to the Point

    With the Fyne framework, Go offers an easy-to-use graphical interface for all popular platforms. As a sample application, Mike uses an algorithm to draw arrows onto images.

  • CLI Image Processing

    Powerful command-line tools offer fast and easy image editing.

  • Perl: Automating Color Correction

    If you have grown tired of manually correcting color-casted images (as described in last month's Perl column), you might appreciate a script that automates this procedure.

  • Free Selection

    The photogrep GUI tool, built using Go and Fyne, is designed to filter photos just like the grep tool filters file names from a pipe.

  • SunnySide Up

    Cell phones often store photos upside down or sideways for efficiency reasons and record the fact in the Exif metadata. However, not all apps can handle this. Mike Schilli turns to Go to make the process foolproof.

comments powered by Disqus
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters

Support Our Work

Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.

Learn More

News