附件1.txt如下
0000000001110010000000000
0000000000011110100000000
0000000001110001000000000
0000000010111100000000000
0000000010101010000000000
0000000001100010100000000
0000000010101010100000000
0000000001000001100000000
1100011101110110100011000
0001000010110010010010100
0100111101000011101110011
0011110100101011001001001
1000001001100001001101000
1111000111111011100101000
1011011111001101111110111
1000110110010010101101100
1000111100111111111110111
0000000010110001100010100
0000000010010100101010001
0000000010101010100011001
0000000000100111111110010
0000000000011001011110111
0000000001001100100100001
0000000011000011011011001
0000000011010000101110101
附件内容是一个25*25的01矩阵。用1表示黑色方块,0表示白色方块,猜测这是一个缺少定位符的二维码。
方法一(Excel)
import xlwt
# 创建一个xls
= xlwt.Workbook()
book # 创建一个样式 (黑色填充,无边界)
= xlwt.easyxf('pattern: pattern solid, fore_colour black; font: height 250')
Style # 添加一个表单,允许覆盖
= book.add_sheet('flag_code', cell_overwrite_ok=True)
table
with open('1.txt', 'r') as f:
= "".join(f.read().splitlines())
s
= 25 #二维码边长,若大于256需要用xlsxwriter模块
MAX
= 0
i for y in range(0, MAX):
for x in range(0, MAX):
= 256*3 #256为衡量单位,3表示3个字符宽度
table.col(x).width if(s[i] == '1'): #如果是1则在Excel上涂黑
'', style=Style)
table.write(y, x, += 1
i
'1和0的故事.xls') book.save(
加入二维码定位符
# 7*7的二维码定位符
= [
locator 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 0, 1],
[1, 0, 1, 1, 1, 0, 1],
[1, 0, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1]
[
]
def draw_locator(y, x):
for i in range(7):
for j in range(7):
= locator[i][j]
pos1 if pos1 == 1: #如果是1则在Excel上涂黑
+i, x+j, '', style=Style)
table.write(y
0, 0) #左上角
draw_locator(0, 18) #右上角
draw_locator(18, 0) #左下角
draw_locator(
'1和0的故事.xls') book.save(
方法二(PIL)
from PIL import Image
with open('1.txt', 'r') as f:
= "".join(f.read().splitlines())
s
= 25 #二维码边长
MAX = Image.new("1", (MAX, MAX))
pic
= 0
i for y in range(0, MAX):
for x in range(0, MAX):
if(s[i] == '1'):
0)
pic.putpixel([x, y], else:
1)
pic.putpixel([x, y], += 1
i
pic.show()"1和0的故事.png") pic.save(
加入二维码定位符
# 7*7的二维码定位符
= [
locator 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 1],
[1, 0, 1, 1, 1, 0, 1],
[1, 0, 1, 1, 1, 0, 1],
[1, 0, 1, 1, 1, 0, 1],
[1, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1]
[
]
def draw_locator(y, x):
for i in range(7):
for j in range(7):
= locator[i][j]
pos1 if(pos1 == 1):
+i, y+j], 0)
pic.putpixel([xelse:
+i, y+j], 1)
pic.putpixel([x
0, 0) #左上角
draw_locator(0, 18) #右上角
draw_locator(18, 0) #左下角
draw_locator(
pic.show()"1和0的故事.png") pic.save(
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以邮件至 skatexu@qq.com