展示 代码
# DrawCharImage.py
from PIL import Image
ascii_char = list('"$%_&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-/+@<>i!;:,\^`.')
def get_char(r, b, g, alpha=256):
if alpha == 0:
return ' '
gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b)
unit = 256 / len(ascii_char)
return ascii_char[int(gray//unit)]
def main():
im = Image.open('kobe.jpg')
# im = Image.open('astro.png')
scale = 0.5
WIDTH, HEIGHT = int(555*scale), int(654*scale)
# WIDTH, HEIGHT = 800, 600
im = im.resize((WIDTH, HEIGHT))
im.show()
txt = ""
for i in range(HEIGHT):
for j in range(WIDTH):
txt += get_char(*im.getpixel((j, i)))
txt += '\n'
fo = open("THIS IS FOR KOBE.txt","w")
fo.write(txt)
fo.close()
main()