From 4f3abc94465df9676657b258fceccebaf449646e Mon Sep 17 00:00:00 2001 From: Gardel Date: Sun, 26 Sep 2021 14:04:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BB=88=E7=AB=AF=E4=BA=8C?= =?UTF-8?q?=E7=BB=B4=E7=A0=81=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Gardel --- py12306/helpers/qrcode.py | 72 +++++++++++++++++++++++---------------- 1 file changed, 42 insertions(+), 30 deletions(-) diff --git a/py12306/helpers/qrcode.py b/py12306/helpers/qrcode.py index ba83dec..3b91b76 100644 --- a/py12306/helpers/qrcode.py +++ b/py12306/helpers/qrcode.py @@ -1,6 +1,5 @@ # -*- coding: utf-8 -*- -import os import png @@ -16,6 +15,9 @@ def print_qrcode(path): width, height, rows, info = reader.read() lines = list(rows) + planes = info['planes'] # 通道数 + threshold = (2 ** info['bitdepth']) / 2 # 色彩阈值 + # 识别二维码尺寸 x_flag = -1 # x 边距标志 y_flag = -1 # y 边距标志 @@ -29,12 +31,11 @@ def print_qrcode(path): j = x_flag while j < width: total = 0 - for k in range(info['planes']): - px = lines[i][j * info['planes'] + k] + for k in range(planes): + px = lines[i][j * planes + k] total += px - avg = total / info['planes'] - mid = (2 ** info['bitdepth']) / 2 - black = avg < mid + avg = total / planes + black = avg < threshold if y_white > 0 and x_white > 0: break if x_flag > 0 > x_white and not black: @@ -56,39 +57,50 @@ def print_qrcode(path): assert width - x_flag == height - y_flag module_count = int((width - x_flag * 2) / scale) - if os.name == 'nt': - white_block = '▇▇' - black_block = ' ' - new_line = '\n' - else: - white_block = '\033[0;37;47m ' - black_block = '\033[0;37;40m ' - new_line = '\033[0m\n' + whole_white = '█' + whole_black = ' ' + down_black = '▀' + up_black = '▄' + dual_flag = False + last_line = [] output = '\n' for i in range(module_count + 2): - output += white_block - output += new_line + output += up_black + output += '\n' i = y_flag while i < height - y_flag: - output += white_block + if dual_flag: + output += whole_white + t = 0 j = x_flag while j < width - x_flag: total = 0 - for k in range(info['planes']): - px = lines[i][j * info['planes'] + k] + for k in range(planes): + px = lines[i][j * planes + k] total += px - avg = total / info['planes'] - mid = (2 ** info['bitdepth']) / 2 - black = avg < mid - if black: - output += black_block + avg = total / planes + black = avg < threshold + if dual_flag: + last_black = last_line[t] + if black and last_black: + output += whole_black + elif black and not last_black: + output += down_black + elif not black and last_black: + output += up_black + elif not black and not last_black: + output += whole_white else: - output += white_block + last_line[t:t+1] = [black] + t = t + 1 j += scale - output += white_block + new_line + if dual_flag: + output += whole_white + '\n' + dual_flag = not dual_flag i += scale - for i in range(module_count + 2): - output += white_block - output += new_line - print(output, end='', flush=True) + output += whole_white + for i in range(module_count): + output += up_black if last_line[i] else whole_white + output += whole_white + '\n' + print(output, flush=True)