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!
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
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
Tutorials
+11.6k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+5.3k PHP : Convert CSV to JSON with YQL example
+4.2k Golang : Valued expressions and functions example
+22.8k Golang : Randomly pick an item from a slice/array example
+10.5k Golang : Get currencies exchange rates example
+6.9k Golang : Validate credit card example
+10.3k Golang : ISO8601 Duration Parser example
+30.1k Get client IP Address in Go
+9.9k Golang : How to get quoted string into another string?
+19.5k Golang : Archive directory with tar and gzip
+5.2k Golang : What is StructTag and how to get StructTag's value?
+5.7k PHP : Get client IP address