mosaic
Go package for creating a mosaic of an image using Truchet tiles.
![mosaic detail](https://github.com/ybeaudoin/go-mosaic/raw/9c2c3c662e45/images/detail.gif)
Detail of the demo mosaic using 16x16 tiles on Ada Lovelace's portrait
Install
To install the package and the demo:
go get -u github.com/ybeaudoin/go-mosaic
The package imports nfnt's resize package which can be installed as follows:
go get -u github.com/nfnt/resize
At a glance
The package exports the following:
- Function:
Truchet(inFile, outFile string, tileSide uint)
Creates a mosaic of an image using Truchet tiles.
Truchet's Arguments
Argument |
Description |
inFile |
path of the image file to be processed |
outFile |
path for the resulting mosaic file |
tileSide |
side length in pixels for the square area occupied by a tile |
Father Sebastian Truchet (1657-1729) was a French Dominican priest "active in areas such as mathematics, hydraulics, graphics,
typography, and [responsible] for many inventions"[1].
In 1704, he "considered all possible patterns formed by tilings of right triangles oriented at the four corners of a square"
[2]:
![](https://github.com/ybeaudoin/go-mosaic/raw/9c2c3c662e45/images/tiles1.png)
For our purposes, the tiles do not have a common background. The background colors are simply set as the average color of the
square source subarea in which a tile lies. As for the tile itself, the same scheme applies but only for the underlying area
covered by the actual tile.
A tile's interior boundary is readily established using the parametric equation for a line segment, namely,
x = x1 + λ(x2-x1),
y = y1 + λ(y2-y1),
with λ∈[0,1] and where (x1,y1) and (x2,y2) are the coordinates of the
end points.
Given that the algorithm proceeds in a top-down, left-to-right fashion, the (x1,y1) and
(x2,y2) end points are set to represent the top and bottom opposite corners respectively of the subarea.
The value of λ is then driven by the row value yrow so that
λ = (yrow-y1) / (y2-y1).
Substituting this value into the x-equation yields the x-ordinate of the corresponding boundary
point. Determining whether a given source pixel on the row lies in the background area is then just a matter of checking whether
its x value is to the left or right of the boundary x-value for a given tile type.
Which leaves the question of which tile to use? We opted to pick a tile based on the largest color gradient from the
center of the source subarea to one of its corners. In the eyes of this beholder, the method appears to soften the transition
between lighter and darker areas. For example, in the above image, the tiles along her hair line all point in the same direction
and add an intermediate color between adjacent subareas.
Package mosaic supports GIF, JPEG and PNG files. The image format for the resulting mosaic is governed by the specified
case-insensitive extension of the output file., i.e., ".gif", ".jpg", ".jpeg", or ".png". The encodings are
lossless but keep in mind that dithering occurs with the GIF format when the number of colors exceeds 256 colors.
Reference
- https://en.wikipedia.org/wiki/S%C3%A9bastien_Truchet
- http://mathworld.wolfram.com/TruchetTiling.html
MIT License
Copyright (c) 2016 Yves Beaudoin webpraxis@gmail.com
See the file LICENSE for copying permission.