Created
May 30, 2022 15:23
-
-
Save DavideGalilei/2d2924d59adc1bf3bc958317677bd299 to your computer and use it in GitHub Desktop.
Image to braille (without dithering)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import std / [os, unicode] | |
import pkg / pixie | |
const Root = currentSourcePath().parentDir | |
var im = readImage(Root / "bird512.png") | |
im = im.resize(80, 80) | |
im = im.resize(im.width - (im.width mod 2), im.height - (im.height mod 4)) | |
const start = 0x2800 # "⠀" base braille char codepoint | |
proc getBlackWhite(px: ColorRGBX): int = | |
return ord((0.299 * float(px.r) + 0.587 * float(px.g) + 0.114 * float(px.b)) * (float(px.a) / float(uint8.high)) > 0.5) | |
template getChar(a, b, c, d, e, f, g, h: ColorRGBX): Rune = | |
( | |
Rune(start + getBlackWhite(a) + | |
(getBlackWhite(b) shl 1) + | |
(getBlackWhite(c) shl 2) + | |
(getBlackWhite(d) shl 3) + | |
(getBlackWhite(e) shl 4) + | |
(getBlackWhite(f) shl 5) + | |
(getBlackWhite(g) shl 6) + | |
(getBlackWhite(h) shl 7)) | |
) | |
template gpx(im: Image, x, y: int): ColorRGBX = | |
im.data[y * im.width + x] | |
var output: string | |
for y in countup(0, im.width - 4, 4): | |
# var y = y * 4 | |
for x in countup(0, im.height - 2, 2): | |
# var x = x * 2 | |
output.add(getChar(im.gpx(x, y), im.gpx(x, y+1), im.gpx(x, y+2), im.gpx(x+1, y), im.gpx(x+1, y+2), im.gpx(x+1, y+3), im.gpx(x, y+3), im.gpx(x+1, y+3))) | |
output.add('\n') | |
var f = open(Root / "output.txt", fmWrite) | |
f.write(output) | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment