Home

Awesome

Triangle logo

build Go Report Card Go Reference license release homebrew

▲ Triangle is a tool for generating triangulated image using delaunay triangulation. It takes a source image and converts it to an abstract image composed of tiles of triangles.

Sample image

The process

Features

TODO

Head over to this subtopic to get a better understanding of the supported features.

Installation and usage

$ go install github.com/esimov/triangle/v2/cmd/triangle@v2.0.0 

You can also download the binary file from the releases folder.

MacOS (Brew) install

The library can be installed via Homebrew too.

$ brew install triangle

API usage

proc := &triangle.Processor{
	MaxPoints:  2500,
	BlurRadius: 2,
	PointRate:  0.75,
	BlurFactor: 1,
	EdgeFactor: 6,
}

img := &triangle.Image{
	Processor: *proc,
}

input, err := os.Open("input.jpg")
if err != nil {
	log.Fatalf("error opening the source file: %v", err)
}

// decode image
src, err := img.DecodeImage(input)
if err != nil {
	log.Fatalf("error decoding the image: %v", err)
}
res, _, _, err := img.Draw(src, *proc, func() {})
if err != nil {
	log.Fatalf("error generating the triangles: %v", err)
}

output, err := os.Create("output.png")
if err != nil {
	log.Fatalf("error opening the destination file: %v", err)
}

// encode image
png.Encode(output, res)

Supported commands

$ triangle --help

The following flags are supported:

FlagDefaultDescription
inn/aSource image
outn/aDestination image
bl2Blur radius
nf0Noise factor
bf1Blur factor
ef6Edge factor
pr0.075Point rate
pth10Points threshold
pts2500Maximum number of points
so10Sobel filter threshold
slfalseUse solid stroke color (yes/no)
wf0Wireframe mode (0: without stroke, 1: with stroke, 2: stroke only)
st1Stroke width
grfalseOutput in grayscale mode
webfalseOpen the SVG file in the web browser
bg' 'Background color (specified as hex value)
cwsystem spec.Number of files to process concurrently

Key features

Process multiple images from a directory concurrently

The CLI tool also let you process multiple images from a directory concurrently. You only need to provide the source and the destination folder by using the -in and -out flags.

$ triangle -in <input_folder> -out <output-folder>

You can provide also an image file URL for the -in flag.

$ triangle -in <image_url> -out <output-folder>

Pipe names

The CLI tool accepts also pipe names, which means you can use stdin and stdout without the need of providing a value for the -in and -out flag directly since these defaults to -. For this reason it's possible to use curl for example for downloading an image from the internet and invoke the triangulation process over it directly without the need of getting the image first and calling ▲ Triangle afterwards.

Here are some examples using pipe names:

$ curl -s <image_url> | triangle > out.jpg
$ cat input/source.jpg | triangle > out.jpg
$ triangle -in input/source.jpg > out.jpg
$ cat input/source.jpg | triangle -out out.jpg
$ triangle -out out.jpg < input/source.jpg

Background color

You can specify a background color in case of transparent background images (.png) by using the -bg flag. This flag accepts a hexadecimal string value. For example setting the flag to -bg=#ffffff00 will set the alpha channel of the resulted image transparent.

Output as image or SVG

By default the output is saved to an image file, but you can export the resulted vertices even to an SVG file. The CLI tool can recognize the output type directly from the file extension. This is a handy addition for those who wish to generate large images without guality loss.

$ triangle -in samples/input.jpg -out output.svg

Using with -web flag you can access the generated svg file directly on the web browser.

$ triangle -in samples/input.jpg -out output.svg -web=true

Supported output types

The following output file types are supported: .jpg, .jpeg, .png, .bmp, .svg.

Tweaks

Setting a lower points threshold, the resulted image will be more like a cubic painting. You can even add a noise factor, generating a more artistic, grainy image.

Here are some examples you can experiment with:

$ triangle -in samples/input.jpg -out output.png -wf=0 -pts=3500 -st=2 -bl=2
$ triangle -in samples/input.jpg -out output.png -wf=2 -pts=5500 -st=1 -bl=10

Examples

<a href="https://github.com/esimov/triangle/blob/master/output/sample_3.png"><img src="https://github.com/esimov/triangle/blob/master/output/sample_3.png" width=410/></a> <a href="https://github.com/esimov/triangle/blob/master/output/sample_4.png"><img src="https://github.com/esimov/triangle/blob/master/output/sample_4.png" width=410/></a> <a href="https://github.com/esimov/triangle/blob/master/output/sample_5.png"><img src="https://github.com/esimov/triangle/blob/master/output/sample_5.png" width=410/></a> <a href="https://github.com/esimov/triangle/blob/master/output/sample_6.png"><img src="https://github.com/esimov/triangle/blob/master/output/sample_6.png" width=410/></a> <a href="https://github.com/esimov/triangle/blob/master/output/sample_8.png"><img src="https://github.com/esimov/triangle/blob/master/output/sample_8.png" width=410/></a> <a href="https://github.com/esimov/triangle/blob/master/output/sample_9.png"><img src="https://github.com/esimov/triangle/blob/master/output/sample_9.png" width=410/></a> Triangle1 Triangle2 Triangle3

License

Copyright © 2018 Endre Simo

This project is under the MIT License. See the LICENSE file for the full license text.