Skip to content

Instantly share code, notes, and snippets.

@the5fire
Created August 3, 2016 03:41
Show Gist options
  • Save the5fire/63096ffa6e09a80277b26a08712f3511 to your computer and use it in GitHub Desktop.
Save the5fire/63096ffa6e09a80277b26a08712f3511 to your computer and use it in GitHub Desktop.
# coding:utf-8
# @author: the5fire
import sys
from io import BytesIO
import requests
from PIL import Image
FEATURE = ''.join(map(str, [0, 0, 0, 0, 0])) # 二维码回字边
def _list2str(_list):
return ''.join(map(str, _list))
def is_qrcode(img_url):
response = requests.get(img_url, verify=False)
qrcode_image = Image.open(BytesIO(response.content))
matrix = convert_to_matrix(qrcode_image)
start_x = 0
start_y = 0
is_find = False
# log_matrix(matrix)
for y, line in enumerate(matrix):
line = _list2str(line)
if FEATURE in line:
start_y = y
start_x = line.index(FEATURE)
if len(matrix) > y and _list2str(matrix[y+1][start_x:start_x+len(FEATURE)]) == FEATURE:
is_find = True
break
if is_find:
print '查找末尾标记'
end_x = find_endx(start_x, start_y, matrix)
print start_y, ':', start_x, end_x
if double_verify(start_y, start_x, end_x, matrix):
print '开始判断是否为二维码'
return is_real_qr(start_y, start_x, end_x, matrix)
return False
def double_verify(start_y, start_x, end_x, matrix):
""" 二次校验
endx位置须小于长度的一半
在同一x轴上可以找到相同长度的0
"""
if end_x > len(matrix[start_y]) / 2:
return False
feature = _list2str(matrix[start_y][start_x:end_x])
remain_line = _list2str(matrix[start_y][end_x:])
if feature not in remain_line:
return False
index = remain_line.index(feature)
second_x = end_x + index
if second_x < len(matrix[start_y]) / 2:
return False
print '二次校验通过'
return True
def is_real_qr(start_y, start_x, end_x, matrix):
"""
从y网下遍历,至少应该有三次一样的边,均为00000...
并且每两条边之间的间距应该一致
"""
first_y = start_y
second_y = 0
third_y = 0
fourth_y = 0
min_dis = 10
feature = _list2str(matrix[start_y][start_x:end_x])
y = start_y
while y < len(matrix) - 1:
y += 1
if min_dis < (y - start_y) and _list2str(matrix[y][start_x:end_x]) == feature:
print y
start_y = y # 重置起始点
if second_y == 0:
second_y = y
elif third_y == 0:
third_y = y
elif fourth_y == 0:
fourth_y = y
else:
print '太多了'
# print first_y, second_y, third_y, fourth_y
if all([first_y, second_y, third_y, fourth_y]) and abs((second_y - first_y) - (fourth_y - third_y)) < 3:
print '找到了,他就是二维码'
return True
return False
def find_endx(start_x, start_y, matrix):
index = start_x
x_matrix = matrix[start_y]
while index < len(x_matrix) - 1:
index += 1
if x_matrix[index] != 0:
break
return index
def convert_to_matrix(image):
# 读取图像
im = image
im.thumbnail((200, 200))
im = im.convert('L')
im = im.convert('RGB')
width, height = im.size
two_dimension = []
for i in range(0, width):
horizontal = []
for j in range(0, height):
current_pix = im.getpixel((i, j))
all_sum = sum(current_pix)
horizontal.append(1 if all_sum >= 355 else 0)
two_dimension.append(horizontal)
return two_dimension
def log_matrix(matrix):
with open('result.txt', 'wb') as f:
for line in matrix:
line = _list2str(line)
f.write(line)
f.write('\n')
if __name__ == '__main__':
image_path = sys.argv[1]
# is_qrimage(image_path)
is_qrcode(image_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment