developer tip

ASCII 아트 이미지 변환 알고리즘은 어떻게 작동합니까?

optionbox 2021. 1. 6. 08:05
반응형

ASCII 아트 이미지 변환 알고리즘은 어떻게 작동합니까?


다음과 같은 멋진 무료 "이미지를 ASCII 아트로"변환 사이트가 있습니다. ASCII-art.org

이러한 이미지 변환 알고리즘은 어떻게 작동합니까?

                         ,                     
                     . W,                
                     W W @                
                     W, WW                
                  , 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여          
              WW WWWWWWWWWWWWWWWWWW          
              WW WWWWWWWWWWWWWWWWWW @ W #         
             , WW.WWWWWWWWWWWWWWWWWWWW         
              WW @ WWWWWWWWWWWWWWWWWWWWW         
            : WWWWWWWWWWWWWWWWWWWWWWW :       
            @ 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 @@@@@@@@@@ :


큰 그림 수준의 개념은 간단합니다.

  1. 인쇄 가능한 각 문자에는 대략적인 회색조 값을 할당 할 수 있습니다. 예를 들어 "at"기호 @는 "더하기"기호보다 시각적으로 더 어둡습니다 +. 효과는 실제로 사용되는 글꼴과 간격에 따라 달라집니다.

  2. 선택한 글꼴의 비율에 따라 입력 이미지를 너비와 높이가 일정한 직사각형 픽셀 블록으로 그룹화합니다 (예 : 너비 4 픽셀, 높이 5 픽셀 직사각형). 이러한 각 블록은 출력에서 ​​하나의 문자가됩니다. (방금 언급 한 픽셀 블록을 사용하면 240w-x-320h 이미지는 60 자 64 줄이됩니다.)

  3. 각 픽셀 블록의 평균 회색조 값을 계산합니다.

  4. 각 픽셀 블록에 대해 회색조 값 (1 단계)이 픽셀 블록 평균 (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.


aalib (last release in 2001) is an open source ASCII art library that's used in applications like mplayer. You may want to check out its source code to see how it does it. Other than that, this page describes in more detail about how such algorithms work.


Also you can take a look at libcaca (latest release 2014), which acording to their website has the following improvements over aalib:

  • Unicode support
  • 2048 available colours (some devices can onlyhandle 16)
  • dithering of colour images
  • advanced text canvas operations (blitting, rotations)

I found this CodeProject article written by Daniel Fisher containing a simple C# implementation of a image to ASCII art conversion algorithm.

These are the steps the program/library performs:

  1. Load the Image stream to a bitmap object
  2. Grayscale the bitmap using a Graphics object
  3. Loop through the image's pixels (because we don't want one ASCII character per pixel, we take one per 10 x 5)
  4. To let every pixel influence the resulting ASCII char, we loop them and calculate the brightness of the amount of the current 10 x 5 block.
  5. Finally, append different ASCII characters based for the current block on the calculated amount.

Quite easy, isn't it?

BTW: In the comments to the article I found this cool AJAX implementation: Gaia Ajax ASCII Art Generator:

[...] I felt compelled to demonstrate it could easily be done in a standardized set of web technologies. I set out to see if I could find some libraries to use, and I found Sau Fan Lee's codeproject article about his ASCII fying .NET library.

P.S.: Lucas (see comments) found another CodeProject article.

ReferenceURL : https://stackoverflow.com/questions/394882/how-do-ascii-art-image-conversion-algorithms-work

반응형