字符画——圣诞快乐

效果:


字符画——圣诞快乐_第1张图片
merry.gif

用的还是上次字符画的程序(https://www.jianshu.com/p/fbb6471b03cd),只是稍作修改。
文件结构:

字符画——圣诞快乐_第2张图片
文件结构

生成字符的代码:

# http://blog.csdn.net/u014746368/article/details/50879235
from PIL import Image
import argparse
import os

IMGPATH = os.getcwd()+'\\txt\\'
#can shu chu li
parser = argparse.ArgumentParser()

parser.add_argument('file')
parser.add_argument('-o','--output')
parser.add_argument('--width',type = int ,default = 80)
parser.add_argument('--height',type = int, default = 80)

args = parser.parse_args()

IMG = args.file
#WIDTH = args.width
#HEIGHT = args.height
#OUTPUT = args.output
NUM = 8

ascii_char = list(" .*")

def get_char(r,b,g,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)]

def get_txt(im,k):
    im = im.resize((WIDTH,HEIGHT),Image.NEAREST)
    txt = ""
    for i in range(HEIGHT):
        for j in range(WIDTH):
            txt += get_char(*im.getpixel((j,i)))
        txt += '\n'
    print(txt)
    return txt

if __name__ == '__main__':
    im = Image.open(IMG+".jpg")
    k = NUM
    while k>=1:
        f = open( IMGPATH+IMG+str(k)+'.txt','w')
        HEIGHT = k*10
        WIDTH = k*10
        txt = get_txt(im,k)
        f.write(txt)
        f.close()
        k=k-1

播放的代码,这段写得不好,中间可以用函数的,代码复用率低了:

from sys import argv
import os
import time
import tkinter as tk 

PATH = os.getcwd()
TXTPATH = PATH+'\\txt\\'
#WIDTH = 80
#HEIGHT = 60
IMGNUM = 8
FRAMETIME = 60#80


window = tk.Tk()  
window.title('Merry Christmas Player') 
window.geometry('800x800')  
#window.geometry('1000x850')  
#window['width'] = window.winfo_screenwidth()
#window['height'] = window.winfo_screenheight()
#window.overrideredirect(True)

x = 1
y = 1
txtpage = ""
player = tk.Label(window,text = txtpage,width=800,height=600,font=('Consolas',8),justify="left")  
player.pack()  
#time1 = time.time()
#print str(time1)



def trickit():
    global x 
    global y
    global time1
    txtpage = ""
    print(str(x)+" "+str(y)+"\n")
    if y == 1:
        if x <= IMGNUM:
            f = open(TXTPATH+'sheng'+str(x)+'.txt','r')
            for line in f:
                txtpage = txtpage + line
            player.config(text = txtpage)
            window.update()
            f.close()
            x += 1 
            player.after(FRAMETIME, trickit)
        else :
            y = y + 1
            x = IMGNUM
#           player.after(FRAMETIME, trickit)
    if y == 2:
        if x >= 1:
            f = open(TXTPATH+'sheng'+str(x)+'.txt','r')
            for line in f:
                txtpage = txtpage + line
            player.config(text = txtpage)
            window.update()
            f.close()
            x = x-1 
            player.after(FRAMETIME, trickit)
        else :
            x = 1
            y = y + 1
#           player.after(FRAMETIME, trickit)
    if y == 3:
        if x <= IMGNUM:
            f = open(TXTPATH+'dan'+str(x)+'.txt','r')
            for line in f:
                txtpage = txtpage + line
            player.config(text = txtpage)
            window.update()
            f.close()
            x += 1 
            player.after(FRAMETIME, trickit)
        else :
            y = y + 1
            x = IMGNUM
#           player.after(FRAMETIME, trickit)
    if y == 4:
        if x >= 1:
            f = open(TXTPATH+'dan'+str(x)+'.txt','r')
            for line in f:
                txtpage = txtpage + line
            player.config(text = txtpage)
            window.update()
            f.close()
            x = x-1 
            player.after(FRAMETIME, trickit)
        else :
            x = 1
            y = y + 1
#           player.after(FRAMETIME, trickit)
    if y == 5:
        if x <= IMGNUM:
            f = open(TXTPATH+'kuai'+str(x)+'.txt','r')
            for line in f:
                txtpage = txtpage + line
            player.config(text = txtpage)
            window.update()
            f.close()
            x += 1 
            player.after(FRAMETIME, trickit)
        else :
            y = y + 1
            x = IMGNUM
#           player.after(FRAMETIME, trickit)
    if y == 6:
        if x >= 1:
            f = open(TXTPATH+'kuai'+str(x)+'.txt','r')
            for line in f:
                txtpage = txtpage + line
            player.config(text = txtpage)
            window.update()
            f.close()
            x = x-1 
            player.after(FRAMETIME, trickit)
        else :
            x = 1
            y = y + 1
#           player.after(FRAMETIME, trickit)
    if y == 7:
        if x <= IMGNUM:
            f = open(TXTPATH+'le'+str(x)+'.txt','r')
            for line in f:
                txtpage = txtpage + line
            player.config(text = txtpage)
            window.update()
            f.close()
            x += 1 
            player.after(FRAMETIME, trickit)
        else :
            y = y + 1
            x = IMGNUM
#           player.after(FRAMETIME, trickit)
    if y == 8:
        if x >= 1:
            f = open(TXTPATH+'le'+str(x)+'.txt','r')
            for line in f:
                txtpage = txtpage + line
            player.config(text = txtpage)
            window.update()
            f.close()
            x = x-1 
            player.after(FRAMETIME, trickit)
        else :
            x = 1
            y = y + 1   
            player.after(FRAMETIME, trickit)
    if y == 9:
        f = open(PATH+'\\tree.txt','r')
        for line in f:
            txtpage = txtpage + line
        player.config(text = txtpage)
        window.update()
#\      f.close()


def mouse(event):
    player.after(1000, trickit)
player.bind("", mouse)
player.pack()
    
window.mainloop()
字符画——圣诞快乐_第3张图片
merry2.gif

你可能感兴趣的:(字符画——圣诞快乐)