Golang : Image to ASCII art example




This tutorial is just for fun and want to relive my DOS programming days for a while. In this tutorial, we will learn how to convert black and white image to ASCII ART.

The code below with generate the following ASCII ART. If you are running this from a terminal, you will need to adjust the terminal size till you can see the ASCII properly. Happy coding!

golang ASCII art image

The code :

 package main

 import (
 "fmt"
 "github.com/disintegration/imaging"
 "github.com/llgcode/draw2d"
 "image"
 "image/color"
 "image/draw"
 "strings"
 )

 func main() {

 // Create an new image with text data
 // From https://github.com/llgcode/draw2d.samples/tree/master/helloworld
 // Set the global folder for searching fonts
 draw2d.SetFontFolder(".")

 width := 100
 height := 70

 // Initialize the graphic context on an RGBA image
 img := image.NewRGBA(image.Rect(0, 0, width, height))

 // set background to white
 white := color.RGBA{255, 255, 255, 255}
 draw.Draw(img, img.Bounds(), &image.Uniform{white}, image.ZP, draw.Src)

 gc := draw2d.NewGraphicContext(img)

 gc.FillStroke()

 // Set the font Montereymbi.ttf
 gc.SetFontData(draw2d.FontData{"Monterey", draw2d.FontFamilyMono, draw2d.FontStyleBold | draw2d.FontStyleItalic})
 // Set the fill text color to black
 gc.SetFillColor(image.Black)

 gc.SetFontSize(18)

 text := "WE LOVE"
 gc.FillStringAt(text, 0, 20)

 text = "GOLANG!"
 gc.FillStringAt(text, 0, 40)

 // create a new blank image with white background
 newImg := imaging.New(width, height, color.NRGBA{255, 255, 255, 255})

 //paste the text to the new blank image
 newImg = imaging.Paste(newImg, img, image.Pt(10, 20))

 // SKIP THIS

 //err = draw2d.SaveToPngFile("./ean.png", newImg)
 //if err != nil {
 // fmt.Println(err)
 // os.Exit(1)
 //}

 // convert image to ASCII ART

 var str []string

 for y := 0; y < 70; y++ {
 for x := 0; x < 100; x++ {
 r, g, b, _ := newImg.At(x, y).RGBA()
 rgb := fmt.Sprint(r + g + b)

 //fmt.Println(rgb)

 if rgb == "196605" { // use -1677216 for all colors ??
 str = append(str, " ")
 } else {
 str = append(str, "#") // if not blank, put # sign
 }

 if strings.TrimSpace(strings.Join(str, "")) == "" { // continue till end
 continue
 }

 }

 fmt.Println(str)
 }
 }

The next challenge for you is to convert a full colored image to ASCII art with Golang. Try it out !

NOTE : For some odd reason, the luximbi.ttf font from https://github.com/llgcode/draw2d.samples/tree/master/helloworld cannot be used. Therefore, I've replaced it with the Monterey font.

You can get the Monterey font from http://www.1001freefonts.com/monterey.font , unzip the zip file and rename the MontereyFLF.ttf to Montereymbi.ttf with this command and execute the code above in the same directory as the 'Montereymbi.ttf' file.

>cp MontereyFLF.ttf Montereymbi.ttf

References :

https://www.socketloop.com/tutorials/golang-generate-ean-barcode

https://en.wikipedia.org/wiki/ASCII_art

http://stackoverflow.com/questions/7098972/ascii-art-java





By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.


Advertisement