Skip to content

Instantly share code, notes, and snippets.

@borgle
Last active May 1, 2017 16:52
Show Gist options
  • Save borgle/1aa93874d2890b5a35cf836a3b1864f4 to your computer and use it in GitHub Desktop.
Save borgle/1aa93874d2890b5a35cf836a3b1864f4 to your computer and use it in GitHub Desktop.
命令行输出二维码内容
#coding: utf-8
import io, os, sys, platform
import requests, wcwidth
from PIL import Image
def PrintCmdQrcode(qrText):
'''print qrcode in cmd
see also https://github.com/xrdavies/qqbot/commit/21a91099db23b62898fbbcbd7a2e0d5af89ad0d8
'''
try:
b = u'\u2588'
sys.stdout.write(b + '\r')
sys.stdout.flush()
except UnicodeEncodeError:
white = 'MM'
else:
white = b
black=' '
osName = platform.system()
if osName == 'Windows':
white = '@@'
blockCount = int(2/wcwidth.wcswidth(white))
white *= abs(blockCount)
sys.stdout.write(' '*50 + '\r')
sys.stdout.flush()
qr = qrText.replace('0', white).replace('1', black)
if osName != 'Windows':
qr = '\033[37m\033[40m\n' + qr + '\033[0m\n' # force to use white/black.
sys.stdout.write(qr)
sys.stdout.flush()
# A space-saving text QRCode
if osName != 'Windows':
charlist = [u' ', u'\u2598', u'\u259D', u'\u2580', u'\u2596', u'\u258C', u'\u259E', u'\u259B',
u'\u2597', u'\u259A', u'\u2590', u'\u259C', u'\u2584', u'\u2599', u'\u259F', u'\u2588']
qrarray = map(lambda x: map(lambda y: y, x), qrtext.split('\n'))
qrtext = ''
for rr in range(0, size + padding * 2, 2):
for cc in range(0, size + padding * 2, 2):
index = int(''.join([x for row in qrarray[rr:rr+2] for x in (row + ['0'])[cc:cc+2]][::-1]), 2)
qrtext += hex(15 - index)[-1]
qrtext += '\n'
qr = ''.join(map(lambda x: charlist[int(x, 16)] if x != '\n' else x, qrtext))
qr = '\033[37m\033[40m\n' + qr + '\033[0m\n' # force to use white/black.
sys.stdout.write(qr)
sys.stdout.flush()
def get_binarization_text():
url = 'https://ssl.ptlogin2.qq.com/ptqrshow?appid=501004106&e=0&l=M&s=5&d=72&v=4&t=0.115'
r = requests.get(url)
data = r.content
r.close()
size=33
padding=5
rgb=Image.open(io.BytesIO(data)).resize((size, size)).convert('RGB')
qrtext = ''
for i in range(padding):
qrtext += '0' * (size + padding * 2) + '\n'
for rr in range(size):
qrtext += '0' * padding
for cc in range(size):
r,g,b = rgb.getpixel((cc, rr))
if (r > 127 or g > 127 or b > 127):
qrtext += '0'
else:
qrtext += '1'
qrtext += '0'*padding
qrtext += '\n'
for i in range(padding):
qrtext += '0' * (size + padding * 2) + '\n'
return qrtext
if __name__ == '__main__':
qrtext = get_binarization_text()
PrintCmdQrcode(qrtext)
print '\n' * 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment