BugKu 1和0的故事

  1. 方法一(Excel)
  2. 方法二(PIL)

附件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
book = xlwt.Workbook()
# 创建一个样式 (黑色填充,无边界)
Style = xlwt.easyxf('pattern: pattern solid, fore_colour black; font: height 250')
# 添加一个表单,允许覆盖
table = book.add_sheet('flag_code', cell_overwrite_ok=True)

with open('1.txt', 'r') as f:
    s = "".join(f.read().splitlines())

MAX = 25  #二维码边长,若大于256需要用xlsxwriter模块

i = 0
for y in range(0, MAX):
    for x in range(0, MAX):
        table.col(x).width = 256*3  #256为衡量单位,3表示3个字符宽度
        if(s[i] == '1'):  #如果是1则在Excel上涂黑
            table.write(y, x, '', style=Style)
        i += 1

book.save('1和0的故事.xls')

加入二维码定位符

# 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):
            pos1 = locator[i][j]
            if pos1 == 1:  #如果是1则在Excel上涂黑
                table.write(y+i, x+j, '', style=Style)

draw_locator(0, 0)  #左上角
draw_locator(0, 18)  #右上角
draw_locator(18, 0)  #左下角

book.save('1和0的故事.xls')
image-20231014110419730
image-20231014110617274

方法二(PIL)

from PIL import Image

with open('1.txt', 'r') as f:
    s = "".join(f.read().splitlines())

MAX = 25  #二维码边长
pic = Image.new("1", (MAX, MAX))

i = 0
for y in range(0, MAX):
    for x in range(0, MAX):
        if(s[i] == '1'):
            pic.putpixel([x, y], 0)
        else:
            pic.putpixel([x, y], 1)
        i += 1

pic.show()
pic.save("1和0的故事.png")

加入二维码定位符

# 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):
            pos1 = locator[i][j]
            if(pos1 == 1):
                pic.putpixel([x+i, y+j], 0)
            else:
                pic.putpixel([x+i, y+j], 1)

draw_locator(0, 0)  #左上角
draw_locator(0, 18)  #右上角
draw_locator(18, 0)  #左下角

pic.show()
pic.save("1和0的故事.png")
image-20231014110859750
image-20231014110943899

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以邮件至 skatexu@qq.com

文章标题:BugKu 1和0的故事

字数:596

本文作者:skateXu

发布时间:2023-10-14, 10:48:41

最后更新:2023-11-21, 13:20:39

原始链接:http://example.com/2023/10/14/BugKu-1%E5%92%8C0%E7%9A%84%E6%95%85%E4%BA%8B/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。