Skip to content

Instantly share code, notes, and snippets.

@pcostesi
Created May 21, 2012 03:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcostesi/2760420 to your computer and use it in GitHub Desktop.
Save pcostesi/2760420 to your computer and use it in GitHub Desktop.
QRCode cli printer
import qrcode.image.base
BLACK = "\033[40m"
WHITE = "\033[107m"
DEFAULT = "\033[49m"
SPACER = " "
class CliImage(qrcode.image.base.BaseImage):
"""Cli image builder."""
def __init__(self, border, width, box_size):
super(CliImage, self).__init__(border, width, box_size)
size = (self.width + border * 2)
self.border = border
self.width = width
self.lines = [[WHITE] * (size + 1) for i in xrange(size)]
def drawrect(self, row, col):
self.lines[row + self.border][col + self.border] = BLACK
def save(self, stream, kind=None):
stream.write(self.to_string())
def to_string(self):
return ('\n'.join(SPACER.join(l) + DEFAULT for l in self.lines))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment