美文网首页
python实现字符画

python实现字符画

作者: sunnnnnnnnnny | 来源:发表于2020-02-14 08:48 被阅读0次

    代码实现

    闲着无聊,看见抖音上的字符画,想试一下。通过百度,利用python3.7用中文汉字实现彩色字符画。不难,下面直接看代码
    img2txt.py

    #-*- coding:utf-8 -*-
    #wlj@2020/2/13 21:17 
    #用中文汉字填充,实现彩色字符画
    #用法 python img2txt.py [源文件] [目标文件]
    #如 python img2txt.py 1.jpeg 1 会输入一个1.png文件,目标文件不需要带后缀名
    from PIL import Image
    from PIL import ImageDraw
    from PIL import ImageFont
    import sys
    
    txt = '我的中国人我爱中国' #填充的汉字
    font_size = 10 #字体的大小
    font = ImageFont.truetype('simsun.ttc',font_size)  #设置字体
    im_path = sys.argv[1]  #原图路径
    im = Image.open(im_path)
    width, height = im.size 
    newImg = Image.new("RGBA",(width, height),(10,10,10))  #背景色rgb,偏黑显示好一些
    x=0
    for i in range(0,height,font_size):   #需要与字体大小一致
        for j in range(0,width,font_size): #需要与字体大小一致
            a,b,c=im.getpixel((j,i))
            draw = ImageDraw.Draw(newImg)
            draw.text((j,i), txt[x%len(txt)], fill=(a,b,c),font=font)
            x+=1
            del draw
    newImg.save("%s.png" % sys.argv[2],'PNG')
    

    比如原画为


    原图

    输出结果为


    1.png

    是不是很有意思

    参考资料

    https://blog.csdn.net/wait_nothing_alone/article/details/52901531

    https://blog.csdn.net/qq_39802740/article/details/80603955

    https://blog.csdn.net/coberup/article/details/82890356

    https://zhuanlan.zhihu.com/p/48941293

    https://www.jianshu.com/p/fca56d635091

    字符画——从入门到不屑
    https://zhuanlan.zhihu.com/p/48941293

    用Python给头像加上圣诞帽
    https://zhuanlan.zhihu.com/p/32283641

    【洪老板Coding时间】诸葛字符骂死字符王朗——字符画视频是怎么做到的?
    https://zhuanlan.zhihu.com/p/23154470

    超不清视频播放器-用Python将视频转成字符
    https://zhuanlan.zhihu.com/p/53881072

    https://blog.csdn.net/weixin_42943114/article/details/91406499

    matlab利用字符作画(汉字绘画)
    https://blog.csdn.net/weixin_42943114/article/details/91406499

    python生成汉字图片字库

    字符画生成工具
    https://www.cnblogs.com/appium/p/11205123.html

    使用Pillow(PIL)库实现中文字符画

    相关文章

      网友评论

          本文标题:python实现字符画

          本文链接:https://www.haomeiwen.com/subject/ozjvthtx.html