题目:
一个字符的 ASCII码有 8 位,图像中的每两个黑色色块间的路程也为 8 步。
从左上角的色块出发,向右为 0,向下为 1,连起来就是对应字符的 ASCII 码。
import numpy as np
from PIL import Image
# 判断下一步往哪走
def search(arr, x, y):
if y + 1 < arr.shape[1] and (arr[x, y + 1, :] != 255).all():
return x, y + 1, 0
elif x + 1 < arr.shape[0]:
return x + 1, y, 1
else:
return -1, -1, -1
if __name__ == "__main__":
image = Image.open("Dancing Line.bmp")
array = np.array(image)
x = y = 0
while True:
asc = 0
# 每八步拼接成一个字符的 ASCII 码
for _ in range(8):
x, y, v = search(array, x, y)
if v < 0:
exit()
asc <<= 1
asc |= v
print(chr(asc), end = "")
白给!