如何ASCII艺术图像转换算法的工作? [英] How do ASCII art image conversion algorithms work?

查看:219
本文介绍了如何ASCII艺术图像转换算法的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些不错的自由的形象ASCII艺术像这样的一个转换的网站: ASCII-art.org

如何做这样的图像转换算法的工作?

                         ,
                     。 W,
                     W W @
                     万维网
                  ,  万维网  。
                  #WW @WW WW#
                  W¯¯WW.WWW WW:W
                  W. WW * WWW#WW沃顿
               *:WW.WWWWWWW // WWW @ W#
              + *#WW#WWWWWWWWWWWWW#W¯¯
              W#@WWWWWWWWWWWWWWWWWW¯¯
              WW WWWWWWWWWWWWWWWWWWW¯¯
              WW WWWWWWWWWWWWWWWWWW @ W#
             ,WW.WWWWWWWWWWWWWWWWWWWWW
              WW @ WWWWWWWWWWWWWWWWWWWWW
            :WWWWWWWWWWWWWWWWWWWWWWWW:
            @ WWWWWWWW @ WWWWWWW @@ WWWWWW。
            W * WWWWWW :::: @ WWW :::::#WWWWW
            WWWWWW @ :::+ * :. :: @ WWWW
            WWWWW @:*:。::。:。:WWWW
            @WWWW#::::。 。:: #:@万维网
            :万维网@:#。 :::WWWW:@WWWW
             WWW#*:W @ *沃顿知识。 W:#WWW
            #WWWW:@ :: * WWWW
            瓦@ WW *宽::,::::,:+ @@ WW#,
            WWWW ## ,,:::::。 。万维网:,
            // WWW @:W .. :::::#。 :WWWW
             WWWW :: * ..:。 ::。,。 :WWWW
             WWWW ::::::::,@ WW @
             WWWW::,:,,:WW,
             :#:,:*
              ,W +,:::,:@
              W¯¯:::宽
           @ ,,,W¯¯:。 ,, :: * @ *:,。 :@W ,, @
         + ..... *:::#WWWWW:#:.... +,
        @ ... ::: *:,,:WWWWWWW,* :::: ..,#
      :...::::::万维网。 :W ::::: ...#
     @@@@@@@@@@@W¯¯@@@@@W¯¯@@@@@@W¯¯@@@@@W¯¯@@@@@@@@@@:

解决方案

大画面层次的概念很简单:

  1. 每个可打印的字符可以被分配一个近似的灰度值;在at符号 @ 明明是在视觉上比+号 + ,例如暗。效果会有所不同,这取决于实际使用的字体和间距

  2. 根据所选择字体的比例,组输入图像与恒定宽度和高度(例如,一个矩形4个像素宽和5个像素高)的矩形像素块。每个这种块将成为输出一个字符。 (使用刚才提到的像素块,一个240瓦特-X-320H形象将成为64行60个字符。)

  3. 计算每个像素块的平均灰度值。

  4. 对于每个像素块,选择一个字符,其灰度值(从第1步)的像素块平均值(从第3步)的良好近似。

这是最简单的锻炼方式。打破候选人的像素块之间的关系时,一个更复杂的版本也将采取实际的图形字符的考虑。例如,斜线( / )会比一个反斜杠一个更好的选择(<$ C C $> \ )对于出现有左下到右上对比度特征的像素块。

There are some nice free "image to ASCII art" conversion sites like this one: ASCII-art.org

How does such an image conversion algorithm work?

                         ,                     
                     .   W    ,                
                     W   W    @                
                     W  ,W    W                
                  ,  W, :W*  .W  .             
                  #  WW @WW  WW  #             
                  W  WW.WWW  WW: W             
                  W. WW*WWW# WW@ W             
               * :WW.WWWWWWW@WWW@W  #          
              +* #WW#WWWWWWWWWWWWW# W          
              W# @WWWWWWWWWWWWWWWWW W          
              WW WWWWWWWWWWWWWWWWWW W          
              WW WWWWWWWWWWWWWWWWWW@W#         
             ,WW.WWWWWWWWWWWWWWWWWWWWW         
              WW@WWWWWWWWWWWWWWWWWWWWW         
            : WWWWWWWWWWWWWWWWWWWWWWWW :       
            @ WWWWWWWW@WWWWWWW@@WWWWWW.        
            W*WWWWWW::::@WWW:::::#WWWWW        
            WWWWWW@::   :+*:.   ::@WWWW        
            WWWWW@:*:.::     .,.:.:WWWW        
            @WWWW#:.:::.     .:: #:@WWW        
            :WWW@:#. ::     :WWWW:@WWWW        
             WWW#*:W@*@W     .   W:#WWW        
            #WWWW:@      ::   ::  *WWWW        
            W@WW*W  .::,.::::,:+  @@WW#,       
            WWWW## ,,.: .:::.: .  .WWW:,       
            @WWW@:   W..::::: #.  :WWWW        
             WWWW::  *..:.  ::.,. :WWWW        
             WWWW:: :.:.:   :  :: ,@WW@        
             WWWW:  .:,  :  ,,     :WW,        
             .: #         :  ,     : *         
              W +    .,  :::  .,   : @         
              W ::                .: W         
           @,,,W:.  ,, ::*@*:,  . :@W.,,@      
         +.....*: : : .#WWWWW:  : .#:....+,    
        @...:::*:,, : :WWWWWWW, ,  *::::..,#   
      :...::::::W:,   @W::::*W.   :W:::::...#  
     @@@@@@@@@@@W@@@@@W@@@@@@W@@@@@W@@@@@@@@@@:

解决方案

The big-picture-level concept is simple:

  1. Each printable character can be assigned an approximate gray-scale value; the "at" sign @ obviously is visually darker than the "plus" sign +, for example. The effect will vary, depending on the font and spacing actually used.

  2. Based on the proportions of the chosen font, group the input image into rectangular pixel blocks with constant width and height (e.g. a rectangle 4 pixels wide and 5 pixels high). Each such block will become one character in the output. (Using the pixel blocks just mentioned, a 240w-x-320h image would become 64 lines of 60 characters.)

  3. Compute the average gray-scale value of each pixel block.

  4. For each pixel block, select a character whose gray-scale value (from step 1) is a good approximation of the pixel block average (from step 3).

That's the simplest form of the exercise. A more sophisticated version will also take the actual shapes of the characters into account when breaking ties among candidates for a pixel block. For example, a "slash" (/) would be a better choice than a "backward slash" (\) for a pixel block that appears to have a bottom-left-to-upper-right contrast feature.

这篇关于如何ASCII艺术图像转换算法的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆