猫史档案馆


【Python作品分享】[去太空,趣编程]【教程贴】

用户:一个程序员一个程序员查看:0 回复:0 评论:0 创建时间:2021-05-01T12:06:10


【作品展示】

center_image

 

【作品介绍】

参与活动

 

【作品源代码】

from PIL import Image, ImageDraw, ImageSequence


def img2car(imgobj):
    scale = 5
    width, height = (int(imgobj.width/scale), int(imgobj.height/scale))

    img_original = imgobj.resize((width, height))
    img = imgobj.convert("L").resize((width, height))

    ASCII_HIGH = '''$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. '''

    img_new = Image.new('RGB', (width * scale, height * scale), 'white')
    draw = ImageDraw.Draw(img_new)
    txt = ""
    for y in range(height):
        for x in range(width):
            pos = (x, y)
            gray = img.getpixel(pos)
            index = int(gray/256*70)
            txt = ASCII_HIGH[index] + ' '
            color = img_original.getpixel(pos)
            draw.text((x * scale, y * scale), txt, fill = color)

    return img_new

def gif2char(imgfile):
    img = Image.open(imgfile)

    frames = ImageSequence.Iterator(img)

    img_colorcharts = []
    for frame in frames:

        colorchar = img2car(frame)
        img_colorcharts.append(colorchar)
    
    img_colorcharts[0].save("bcm_colorchar.gif", 
                            append_images = img_colorcharts[1:], 
                            loop=0, 
                            save_all =True, 
                            )
#gif2char("bcm.gif")
im = Image.open("tupian.png")
img2car(im).save("tupian_2.png")

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页