1. 程式人生 > >圖片轉字元

圖片轉字元

from PIL import Image

import matplotlib.pyplot as plt
args = Image.open("Koala.jpg") #根目錄下儲存

#pixel = args.load()
#print(pixel[100,100])


IMG = "Koala.jpg"
w = input("output width:")
h = input("output height")

#ascii_char = list("[email protected]%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. ")
ascii_char = list(" 1")# 將256灰度對映到70個字元上 def get_char(r,g,b,alpha = 256): if alpha == 0: return ' ' length = len(ascii_char) gray = int(0.2126 * r + 0.7152 * g + 0.0722 * b) unit = (256.0 + 1)/length return ascii_char[int(gray/unit)] if __name__ == '__main__': im = Image.
open(IMG) im = im.resize((int(w),int(h))) # im = im.resize((WIDTH,HEIGHT), Image.NEAREST) WIDTH = im.width HEIGHT = im.height txt = "" for i in range(HEIGHT): for j in range(WIDTH): txt += get_char(*im.getpixel((j,i))) txt += '\n' print(txt)
with open("output.txt",'w') as f: f.write(txt) '''