• Stars
    star
    2,038
  • Rank 22,701 (Top 0.5 %)
  • Language
    Go
  • License
    MIT License
  • Created about 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Convert images to computer generated art using delaunay triangulation.

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

  • First the image is blured out to smoth out the sharp pixel edges. The more blured an image is the more diffused the generated output will be.
  • Second the resulted image is converted to grayscale mode.
  • Then a sobel filter operator is applied on the grayscaled image to obtain the image edges. An optional threshold value is applied to filter out the representative pixels of the resulted image.
  • A convolution filter operator is applied over the image data in order to adjust its final aspect prior running the delaunay triangulation process.
  • Lastly the delaunay algorithm is applied on the pixels obtained from the previous step.

Features

  • Can process recursively whole directories and subdirectories concurrently.
  • Supports various image types.
  • There is no need to specify the file type, the CLI tool can recognize automatically the input and output file type.
  • Can accept image URL as parameter for the -in flag.
  • Possibility to save the generated image as an SVG file.
  • The generated SVG file can be accessed from the Web browser directly.
  • Clean and intuitive API. The API not only that accepts image files but can also work with image data. This means that the Draw method can be invoked even on data streams. Check this demo for reference.
  • Support for pipe names (possibility to pipe in and pipe out the source and destination image).

TODO

  • Standalone and native GUI application

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/[email protected] 

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:

Flag Default Description
in n/a Source image
out n/a Destination image
bl 2 Blur radius
nf 0 Noise factor
bf 1 Blur factor
ef 6 Edge factor
pr 0.075 Point rate
pth 10 Points threshold
pts 2500 Maximum number of points
so 10 Sobel filter threshold
sl false Use solid stroke color (yes/no)
wf 0 Wireframe mode (0: without stroke, 1: with stroke, 2: stroke only)
st 1 Stroke width
gr false Output in grayscale mode
web false Open the SVG file in the web browser
bg ' ' Background color (specified as hex value)
cw system 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

Triangle1 Triangle2 Triangle3

License

Copyright Β© 2018 Endre Simo

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

More Repositories

1

caire

Content aware image resize library
Go
10,356
star
2

pigo

Fast face detection, pupil/eyes localization and facial landmark points detection library in pure Go.
Go
4,386
star
3

diagram

CLI app to convert ASCII arts into hand drawn diagrams.
Go
835
star
4

stackblur-go

A fast, almost Gaussian Blur implementation in Go
Go
256
star
5

dithergo

Various dithering algorithms implemented in Go
Go
167
star
6

forensic

Copy-move image forgery detection library.
Go
136
star
7

gobrot

Mandelbrot image renderer in Go
Go
105
star
8

gogu

A comprehensive, reusable and efficient concurrent-safe generics utility functions and data structures library.
Go
97
star
9

colorquant

Go library for color quantization and dithering
Go
85
star
10

legoizer

A tool to convert images to Lego bricks.
Go
80
star
11

ascii-fluid

Terminal based ASCII fluid simulation controlled by your webcam. 🌊
Go
66
star
12

colidr

Coherent Line Drawing implementation in Go.
Go
55
star
13

pigo-wasm-demos

Webassembly demos showcasing the Pigo face detection library.
Go
53
star
14

gifter

Gif image renderer running in terminal.
Go
44
star
15

gospline

Implementing b-spline curves in Go
Go
37
star
16

cloth-physics

Desktop application for cloth physics simulation using Gio GUI.
Go
37
star
17

triangle-app

Desktop application for Triangle.
JavaScript
35
star
18

pigo-face-tracking

Play games with your head. A face tracking application using the Pigo library.
Go
27
star
19

asciibrot

ASCII mandelbrot fractal running in terminal
Go
22
star
20

facemask

Overlay a mask over a person's face
Go
17
star
21

pigo-openfaas-faceblur

OpenFaaS faceblur function using the Pigo face detector library. (https://github.com/esimov/pigo)
Go
17
star
22

minecraft.js

Simplex noise based minecraft map generator
JavaScript
17
star
23

caire-openfaas

OpenFaaS function for Caire, the content aware image resize library. (https://github.com/esimov/caire)
Go
13
star
24

pigo-openfaas

OpenFaaS function for face detection using the Pigo library. (https://github.com/esimov/pigo)
Go
12
star
25

openfaas-coherent-line-drawing

Coherent Line Drawing OpenFaaS function based on https://github.com/esimov/colidr
Go
11
star
26

gomp

Alpha compositing operations and blending modes in Go.
Go
10
star
27

pigo-gocv-benchmark

Pigo vs GoCV face detection benchmark comparison
Go
4
star
28

simplexnoise.js

Javascript simplex noise implementation based on Stefan Gustavson paper: http://webstaff.itn.liu.se/~stegu/simplexnoise/simplexnoise.pdf
JavaScript
3
star
29

homebrew-triangle

Brew formula for Triangle.
Ruby
2
star
30

go-arena

Testing and benchmarking the new experimental Go memory arenas.
Go
2
star
31

talks

Talks I have given
TeX
1
star
32

flash-experiments

Old Flash (ActionScript3) experiments
ActionScript
1
star