Last active
December 8, 2020 21:09
-
-
Save AlanBell/6c2c1b1d443fdfb88734e0bddebfcdbe to your computer and use it in GitHub Desktop.
silly thing to render QR codes with unicode block characters
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
!#/usr/bin/python3 | |
import pyqrcode | |
#first create a code, and convert it to text as a pattern of 0 and 1 | |
code = pyqrcode.create("westbrit.ie") | |
text=code.text() | |
def blockify(block): | |
#this converts a representation of a 2x2 block into a unicode character | |
bricks={ | |
"0000":" ", | |
"0001":"▗", | |
"0010":"▖", | |
"0011":"▄", | |
"0100":"▘", | |
"0101":"▐", | |
"0110":"▞", | |
"0111":"▟", | |
"1000":"▘", | |
"1001":"▚", | |
"1010":"▌", | |
"1011":"▙", | |
"1100":"▀", | |
"1101":"▜", | |
"1110":"▛", | |
"1111":"█", | |
} | |
return bricks[block] | |
#now we loop through the qr text generating bricks | |
result='' | |
lines=text.split() | |
#loop through two lines at a time, and for each line loop through two characters at a time | |
for x in range(1,len(lines),2): | |
for y in range(1,len(lines[x]),2): | |
#get the 2x2 block | |
#print(x,y) | |
#print(lines[x][y:y+2]) | |
#print(lines[x+1][y:y+2]) | |
block=lines[x][y:y+2] + lines[x+1][y:y+2] #assemble a 4 character string for each 2x2 block | |
result=result+blockify(block) #add the character to our result | |
result=result+"\n" #newline for each line in the QR | |
print(result) #output our masterpiece. It is padded all round by spaces which might eat too many characters. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment