PYTHON HEX-GRID HALFTONER

Short-cut URL for this page: https://chrismolloy.com/halftoner. 'Python Hex-grid Halftoner' by Chris Molloy is licensed under a Creative Commons Attribution Share Alike 4.0 International License.

I couldn't find a Python halftoner that worked on a hexagonal grid pattern (there are several that work on an orthogonal grid), so I wrote my own: GitHub: Python Hex-grid Halftoner.

The code uses OpenCV to do the heavy lifting. I used https://hub.docker.com/r/yoanlin/opencv-python3

Image output formats include anything OpenCV supports (e.g. JPG, PNG), plus SVG.

Works best on contrasty, black-and-white images. To re-colour dots / background, it's probably easier to post-process the SVG (e.g. in Inkscape), rather then mod the code. YMMV.

Under-the-Hood

The basic algorithm involves averaging the grey-scale value of the pixels within each cell of a hexagonal grid overlaid on the source image. When I mentioned this to my friend Jeremy, he pointed out that this is a form of a standard convolution operation, as facilitated by libraries such as OpenCV. I simply needed to create an appropriate kernel, apply it to the source image, then extract the value of the pixel in the centre of each hexagon.

In order to simplify my kernel, I used a circle inscribed within each hexagon.

Python Hex-grid Halftoner