如何利用Python製作ASCII圖

Share This Post

甚麼是ASCII art

  • 文字圖、字元畫、文字畫
  • 1982年出現

例子

  • 顏文字
    : )
    ==
  • 複雜一點的
                       ______
                      /\     \
                     /  \     \
                    /    \_____\
                   _\    / ____/_
                  /\ \  / /\     \
                 /  \ \/_/  \     \
                /    \__/    \_____\
               _\    /  \    / ____/_
              /\ \  /    \  / /\     \
             /  \ \/_____/\/_/  \     \
            /    \_____\    /    \_____\
           _\    /     /    \    / ____/_
          /\ \  /     /      \  / /\     \
         /  \ \/_____/        \/_/  \     \
        /    \_____\            /    \_____\
       _\    /     /            \    / ____/_
      /\ \  /     /              \  / /\     \
     /  \ \/_____/                \/_/  \     \ 
    /    \_____\                    /    \_____\
   _\    /     /_  ______  ______  _\____/ ____/_
  /\ \  /     /  \/\     \/\     \/\     \/\     \
 /  \ \/_____/    \ \     \ \     \ \     \ \     \
/    \_____\ \_____\ \_____\ \_____\ \_____\ \_____\
\    /     / /     / /     / /     / /     / /     /
 \  /     / /     / /     / /     / /     / /     /
  \/_____/\/_____/\/_____/\/_____/\/_____/\/_____/
  • 動畫

開始

pip install Pillow
  1. 加入圖像
import PIL.Image
 
img_flag = True
path = input("輸入圖片路徑 : \n")
 
try:
  img = PIL.Image.open(path)
  img_flag = True
except:
  print(path, "找不到圖片")
  1. 調整圖像大小
width, height = img.size
aspect_ratio = height/width
new_width = 100
new_height = aspect_ratio * new_width * 0.5625
img = img.resize((new_width, int(new_height)))
  1. 把圖像轉為灰階
img = img.convert('L')
  1. 創建ASCII字符列表
  • ASCII是由最暗到最亮的排列,也可以自訂下面的字符。
chars = ["%", "*", "^", "^", "-", "+", "<", "!", ":", ".", " "]
  1. 轉換成ASCII ART
pixels = img.getdata()
new_pixels = [chars[pixel//25] for pixel in pixels]
new_pixels = ''.join(new_pixels)
new_pixels_count = len(new_pixels)
ascii_image = [new_pixels[index:index + new_width]
            for index in range(0, new_pixels_count, new_width)]
ascii_image = "\n".join(ascii_image)

with open("ascii_image.txt", "w") as f:
    f.write(ascii_image)

結果

::::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::@===========:::^==========%:::::::::::
:::::::::::==============================%::::::::
::::::::::================================::::::::
:::::::::%======%=======%=====%==============:::::
:::::::::======================================:::
::::::%========%.............%=======%==========::
:::::==========%......%%%%%%...%%====%..........%:
::::%=============...%%%%%%%%...........%%%%%%...:
::::==================%%%%%%%==........%%%%%.%.=::
:::@=======================%==========^========:::
:::=======================%==================&%:::
::+================&============%====%========::::
::+========================%=============%%:::::::
:::=======================%============%::::::::::
:::%=================================%::::::::::::
::::&%=============================@&:::::::::::::
::&&&&%======%============@%=====&&&&&&&::::::::::
:&&&&&&&&&&%=================%&&&&&&&&&&&:::::::::
:&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&::::::::
:&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&<:::::::
:&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&:::::::

訂閱研究文章

Get updates and learn from the best

More To Explore

Scroll to Top

hurry up !

軟體工程師培訓

限時免費報名中

藉由與「真實世界軟體專案」相同的技術、工具與開發流程,化簡成與商業機密無關、門檻較低更容易上手的「模擬專案」,讓你有機會在職場前輩的陪伴下,完成真槍實彈的練習,動手解決真實的問題,快速累積個人的經驗與作品,而不只是「學習技術」而已。