1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
| from typing import Literal
def tupper_self_referential_formula(k: int, width: int = 17, height: int = 106): """ Tupper self referential formula函数
Parameters: k - 用于迭代图像的k值 width - 图像的宽 height - 图像的高 Returns: 类型是numpy.ndarray矩阵,里面只有0和1 """ import numpy as np rst = np.zeros((width, height)) def f(x: int, y: int) -> int: y += k a1 = 2 ** (width * x + y % width) a2 = (y // width) // a1 return 1 if (a2 & 1) == 1 else 0 for y in range(width): for x in range(height): rst[y, x] = f(x, y) return rst[:, ::-1]
def tupper_to_str(k: int, width: int = 17, height: int = 106, show_black: str = "■", show_white: str = "□") -> str: """ 生成Tupper self referential formula函数的字符画
Parameters: k - 用于迭代图像的k值 width - 图像的宽 height - 图像的高 show_black - 字符画中的黑色方块 show_white - 字符画中的白色方块 Returns: 字符画字符串 """ mat = tupper_self_referential_formula(k, width, height) rst = '' for y in range(width): for x in range(height): if mat[y, x] == 1: rst += show_black else: rst += show_white rst += '\n' return rst
def tupper_to_img(k: int, width: int = 17, height: int = 106, filename: str = 'tupper.png', origin: Literal['upper', 'lower'] = 'upper'): """ 生成Tupper self referential formula函数的字符画
Parameters: k - 用于迭代图像的k值 width - 图像的宽 height - 图像的高 filename - 输出图像的文件名 origin - 输出图像的方式(upper或者lower) Returns: 类型是numpy.ndarray矩阵,里面只有0和1 """ import matplotlib.pyplot as plt rst = tupper_self_referential_formula(k, width, height) plt.figure(figsize=(15, 10)) plt.imshow(rst, origin=origin) plt.savefig(filename) return rst
k = 92898203278702907929705938676672021500394791427205757369123489204565300324859717082409892641951206664564991991489354661871425872649524078000948199832659815275909285198829276929014694628110159824930931595166203271443269827449505707655085842563682060910813942504507936625555735585913273575050118552353192682955310220323463465408645422334101446471078933149287336241772448338428740302833855616421538520769267636119285948674549756604384946996184385407505456168240123319785800909933214695711828013483981731933773017336944656397583872267126767778549745087854794302808950100966582558761224454242018467578959766617176016660101690140279961968740323327369347164623746391335756442566959352876706364265509834319910419399748338894746638758652286771979896573695823608678008814861640308571256880794312652055957150464513950305355055495262375870102898500643010471425931450046440860841589302890250456138060738689526283389256801969190204127358098408264204643882520969704221896973544620102494391269663693407573658064279947688509910028257209987991480259150865283245150325813888942058
rst = tupper_to_str(k, 22, 160) print(rst) tupper_to_img(k, 22, 160)
|