Code Monkey home page Code Monkey logo

string_image's Introduction

字符画项目及其实现原理

原图效果:

字符画效果:

实现原理

首先准备一个字符集

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

然后调用PIL库,将图片转化为灰度图

im = Image.open('messi.jpg')   
# Image.open对于彩色图像返回后图像模式都为RGB
# Image.open对于灰度图像,打开后模式都为L
im = im.resize((268, 180), Image.ANTIALIAS)
im = im.convert('L')
# L模式为灰色图像
# 后续可以了解各种convert()以及图像模式
im.save('messi-L.jpg')

灰度图效果:

原图属性显示图片一共有268x180个像素,所以我们需要将268x180个像素的灰度值转化为相对应的字符,将灰度值大于240的都转化为空字符,其他的,按比例映射到字符集上

def get_char(gray):
    if gray >= 240:
        return ' '
    else:
        return char_set[int(gray/((256.0+1)/len(char_set)))]

最终效果图:

建议使用浏览器打开字符画文本,记事本打开显示效果不理想

string_image's People

Contributors

mesk0 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.