塔伯自我指涉由图片生成K值python程序

import matplotlib.image as mpimg
import numpy as np


def rgb2gray(rgb):
    return np.dot(rgb[..., :3], [0.2989, 0.5870, 0.1140])


img = mpimg.imread("in.png")  # 输入文件名

H = img.shape[0]
W = img.shape[1]
print(f"H = {H}, W = {W}")
img = rgb2gray(img)
threshold = 0.5  # 阈值,0表示纯黑,1表示纯白,用于黑白化图片
K = 0
for x in reversed(range(W)):
    for y in range(H):
        K = (K << 1) + int(img[y, x] < threshold)
K *= H
print(K)

样例

输入图片in.png

输出结果:

H = 17, W = 106
7732976080303657178450278534515545953721691814147533396385378515089514321412513402545421375117579981371482452840580788702200589467998016752192433975084223850171463216577990190222625558829816705774713175142624997893397311119169088047377804572616598140125557066157243777474327577549172021746170650767594397179529901385386121610472318392330362185272131140291220890569772667378408360058487300270037068960274089417160464881353145903925802568200831976051484240342927128191427075003633328171454983396758112849394920257608196936104830566400
原文地址:https://www.cnblogs.com/1024th/p/14420197.html