Skip to content

Instantly share code, notes, and snippets.

@salewski
Last active November 6, 2017 00:24
Show Gist options
  • Save salewski/633de50ea114231cfcdbc76a14d8f71f to your computer and use it in GitHub Desktop.
Save salewski/633de50ea114231cfcdbc76a14d8f71f to your computer and use it in GitHub Desktop.
Very basic awk implementation of drawille, to convert images to unicode graphics.
#!/bin/awk -f
# ▌ ▜ ▌
# ▛▌▐ ▀▌▌▌▌▙▘
# ▙▌▐▖█▌▚▚▘▛▖ - Draw an image using block characters and ImageMagick
#===============================================================================
# USAGE:
# blawk [columns lines] file [imagemagick_arguments]
BEGIN {
if (ARGC <= 3) {
convert = "convert "ARGV[1]" -compress none "ARGV[2] \
" pbm:- | sed -e 's/ //g' -ne '3,$ p'";
} else {
convert = "convert "ARGV[3]" -compress none "ARGV[4] \
" -resize "ARGV[1] * 2"x"ARGV[2] * 4 \
" pbm:- | sed -e 's/ //g' -ne '3,$ p'";
}
# Character number in a binary pattern from top left to bottom right
chmap[0] = 0x0020; chmap[1] = 0x2598;
chmap[2] = 0x259d; chmap[3] = 0x2580;
chmap[4] = 0x2596; chmap[5] = 0x258c;
chmap[6] = 0x259e; chmap[7] = 0x259b;
chmap[8] = 0x2597; chmap[9] = 0x259a;
chmap[10] = 0x2590; chmap[11] = 0x259c;
chmap[12] = 0x2584; chmap[13] = 0x2599;
chmap[14] = 0x259f; chmap[15] = 0x2588;
while ((convert | getline) > 0 ) {
# Get lines by group of 2
line[1] = $0;
# Skip three lines to keep the aspect ratio
convert | getline; convert | getline; convert | getline;
line[2] = $0;
for (i = 1; 2 * i < length(line[1]); i++) {
printf("%c", chmap[ \
1 * substr(line[1], 2 * i , 1) \
+ 2 * substr(line[1], 2 * i + 1, 1) \
+ 4 * substr(line[2], 2 * i , 1) \
+ 8 * substr(line[2], 2 * i + 1, 1) \
]);
}
print ""
}
}
#!/bin/awk -f
# ▌ ▌
# ▛▌▛▘▀▌▌▌▌▙▘
# ▙▌▌ █▌▚▚▘▛▖ - Drawille implementation in awk (just for image conversions).
#===============================================================================
# USAGE:
# blawk [columns lines] file [imagemagick_arguments]
BEGIN {
if (ARGC <= 3) {
convert = "convert "ARGV[1]" -compress none "ARGV[2] \
" pbm:- | sed -e 's/ //g' -ne '3,$ p'";
} else {
convert = "convert "ARGV[3]" -compress none "ARGV[4] \
" -resize "ARGV[1] * 2"x"ARGV[2] * 4 \
" pbm:- | sed -e 's/ //g' -ne '3,$ p'";
}
while ((convert | getline) > 0 ) {
# Get lines by group of 4
line[1] = $0;
convert | getline; line[2] = $0;
convert | getline; line[3] = $0;
convert | getline; line[4] = $0;
# For each character to be drawn
for (i = 0; 2 * i < length(line[1]); i++) {
# Char number = initial offset +
# Sum of characters offsets * 0 or 1 on the grid
printf("%c", 10240 \
+ 1 * substr(line[1], 2 * i , 1) \
+ 8 * substr(line[1], 2 * i + 1, 1) \
+ 2 * substr(line[2], 2 * i , 1) \
+ 16 * substr(line[2], 2 * i + 1, 1) \
+ 4 * substr(line[3], 2 * i , 1) \
+ 32 * substr(line[3], 2 * i + 1, 1) \
+ 64 * substr(line[4], 2 * i , 1) \
+ 128 * substr(line[4], 2 * i + 1, 1));
}
print ""
}
close(cmd)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment