from PIL import Image

def get_char(r, g, b, alpha=256):
    ascii_char = '''$@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<>i!lI;:,\"^`'. '''
    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_img(img: str):
    im = Image.open(img)
    height = int(im.size[1]/1.5)
    width = int(im.size[0]/1.5)

    im = im.resize((width, height), Image.NEAREST)
    txt = ""
    for h in range(height):
        for w in range(width):
            txt += get_char(*im.getpixel((w, h)))
        txt += "\n"
    return txt

def main(image_name: str, art_name: str):
    txt = get_img(image_name)
    with open(art_name, "w", encoding="utf-8") as f:
        f.write(txt)

if __name__ == '__main__':
    main(r"C:\Users\Administrator\Desktop\1.png", r"C:\Users\Administrator\Desktop\1.txt")

  

Python实现图片转字符画的更多相关文章

  1. 20个python项目--图片转字符画

    转自实验楼:https://www.shiyanlou.com/courses/370/learning/?id=1191 代码: # -*- coding:utf-8 -*- from PIL im ...

  2. Python 【图片转字符画】

    一.安装的第三方模块 $ sudo pip3 install --upgrade pip $ sudo pip3 install pillow //window pip3 install pillow ...

  3. 通过python将图片生成字符画

    基础知识: 1.python基础知识   快速学习链接:https://www.shiyanlou.com/courses/214 2.linux命令行操作   快速学习链接:https://www. ...

  4. Python 图片转字符画

    Python 图片转字符画 一.课程介绍 1. 课程来源 原创 2. 内容简介 本课程讲述怎样使用 Python 将图片转为字符画 3. 前置课程 Python编程语言 Linux 基础入门(新版) ...

  5. [笔记] Python 图片转字符画

    一.介绍 用Python 代码完成图片转字符画 二.python 环境 Python 3.6.6 pillow 5.1.0  Python 图像处理库, 需要另外安装 三.原理 gray = 0.21 ...

  6. Python 图片转字符画 学习笔记

    Python 图片转字符画 学习笔记 标签(空格分隔): Python 声明:此文章和所有代码是学习笔记,非原创,原文教程地址:https://www.shiyanlou.com/courses/37 ...

  7. python学习---50行代码实现图片转字符画2

    from PIL import Image codeLib = '''@B%8&WM#*oahkbdpqwmZO0QLCJUYXzcvunxrjft/\|()1{}[]?-_+~<> ...

  8. python学习---50行代码实现图片转字符画1

    转自:https://blog.csdn.net/mm1030533738/article/details/78447714 项目链接: https://www.shiyanlou.com/cours ...

  9. python图片转字符画(转)

    先上代码: from PIL import Image import argparse #命令行输入参数处理 parser = argparse.ArgumentParser() parser.add ...

随机推荐

  1. Annoy解析

    Annoy是高维空间求近似最近邻的一个开源库. Annoy构建一棵二叉树,查询时间为O(logn). Annoy通过随机挑选两个点,并使用垂直于这个点的等距离超平面将集合划分为两部分. 如图所示,图中 ...

  2. FetchType.LAZY 时属性加上@JsonIgnore,避免返回时报错:Could not write JSON: failed to lazily initialize a collection of role

    [示例] @OneToMany(fetch=FetchType.LAZY) @JsonIgnore @Fetch(FetchMode.SELECT) @Cascade(value={CascadeTy ...

  3. websocket协议详解;

    websocket是基于http协议,借用http协议来完成连接阶段的握手: 当连接建立后,浏览器和服务器之间的通信就和http协议没有关系了,b.s之间只用websocket协议来完成基本通信. = ...

  4. 前台如何处理后台返回的json数据

    后台返回的json数据格式: { "state": true, "data": { "id": 0, "name": & ...

  5. android扁平化ProgressBar--progressWheel

    ProgressWheel是git是一个开源项目,为开发者提供一个扁平化的ProgressBar,并可对其进行深度定制   1,将ProgressWheel的源码拷贝到项目中 public class ...

  6. .aspx文件和.aspx.cs文件的区别与联系

    http://zhidao.baidu.com/link?url=_SNw0EHJ8Wg__KanJrKQM3tVEUeFnVilZ6GGIN8ab69RLuyOWD__WyZb7Zb9dJjwDnL ...

  7. [转] const int *a与int *const a,const int *const a的区别

    http://blog.csdn.net/zhangheng837964767/article/details/33783511 关键问题点:const 属于修饰符 ,关键是看const 修饰的位置在 ...

  8. 【WebService】——契约优先

    相关博客: [WebService]--入门实例 [WebService]--SOAP.WSDL和UDDI 前言: 我们先来看一个契约优先的开发实例,通过熟悉他的开发流程,最后再和代码优先的方式进行比 ...

  9. Java中Model1和Model2

    Model1和Model2是java web的两种架构模式.这两种模式各有优缺点,都有各自适合使用的场景. Model1 首先,从分层的角度说,Model1模式可以看作是由两层组成:视图层和模型层. ...

  10. 关于Assert

    如果没有连1394线debug,assert不影响程序的执行,只会在dbgview里面答应"Assertion xxxx"这样的调试信息. 当连着1394线的时候,OS会被hang ...