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. eclipse版本命名规则与其他软件命名

    文章:Eclipse各版本代号一览表 eclipse使用星球.神话人物.元素名称作为命名代号. 所以思路要放宽,不要拘泥于已有经验. java是用咖啡命名的: python中文意思是蟒蛇: 不拘泥于已 ...

  2. 最快的Hash表算法

    我们由一个简单的问题逐步入手:有一个庞大的字符串数组,然后给你一个单独的字符串,让你从这个数组中查找是否有这个字符串并找到它,你会怎么做?有一个方法最简单,老老实实从头查到尾,一个一个比较,直到找到为 ...

  3. elementUI中的el-form怎么使用正则进行验证

    http://element.eleme.io/#/zh-CN/component/form里给出了一个form验证的例子,但是都是使用el-form带有的验证规则,怎么使用自己定义的规则进行验证呢? ...

  4. [Java文件操作] 为文本文件添加行号

    [思路]将文件中的内容按行读取存入一个字符串中,在输出时再为每一行加上行号. import java.io.*; public class Text { private String strFinal ...

  5. Problem Collection I 位运算

    XOR ARC 092B CF 959E xor-MST CF 959F

  6. 【题解】洛谷P2418 yyy loves OI IV

    感觉很是妙啊……这题数次误入歧途...最开始想的二维dp,单调队列优化:无果,卒.于是没忍住看了下标签:暴力枚举?搜索?于是开始想记忆化搜索.以为会有什么很强的剪枝之类的:30分,卒.最后终于回到正道 ...

  7. 【bzoj2064】分裂【压状dp】

    Description 背景: 和久必分,分久必和... 题目描述: 中国历史上上分分和和次数非常多..通读中国历史的WJMZBMR表示毫无压力. 同时经常搞OI的他把这个变成了一个数学模型. 假设中 ...

  8. [Leetcode] unique paths 独特路径

    A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...

  9. LowercaseRoutesMVC ASP.NET MVC routes to lowercase URLs

    About this Project Tired of your MVC application generating mixed-case URLs like http://mysite.com/H ...

  10. 安卓和html的互相调用

    1.写html和安卓布局 <Button android:id="@+id/btn" android:layout_width="wrap_content" ...