当前位置:   article > 正文

通过 Python 把图片转换为 ASCII art,好玩!

ascii art python

126d1a3d61190b5af50321268e5970c0.gif

作者 |周萝卜

来源 |萝卜大杂烩

相信很多人都知道 ASCII art,这是一种使用可打印 ASCII 字符集来构图的图形设计技术。这种艺术最简单的形式就是表情符号,例如:-) 或 :-3,今天我们就来制作更为复杂的图像

8a272990c283905283dc2b2cce8cee29.png

image 的本质

首先,我们先来阐明下图像在计算机系统中的表示方式。图片通常以 .png 或 .jpg 等格式存储在磁盘上,所有这些文件类型都具有相似的结构:它们大致由标题和数据部分组成,前者存储有关图像的有用信息,例如其格式签名,而后者存储实际的像素数据

我们看到的图像实际由像素组成,像素是我们都熟悉的光栅图像中最小的可寻址元素,它们通常表示为一组通道,也称为颜色。在最常见的颜色值中,有经典的 RGB(红绿蓝)和 RGBA(红绿蓝 Alpha)。两者之间的区别在于后者有一个额外的通道,称为“alpha”,用于指定图像的不透明度。

RGBA 是我们将要使用的,因为它也可以用来表示空背景

将 pixels 转换为 ASCCII

现在我们已经了解了图像的表示方式,接下来讨论如何将像素转换为实际的 ASCII 字符

要理解这一点,我们首先看一下像素颜色强度,该值是指所有像素通道的总和除以通道可以具有的最大值的总和(在本例中为 255)

  1. # Import types for clarity
  2. from typing import NewType, Tuple
  3. # Maximum value the sum of the pixel's channel values can reach
  4. MAX_CHANNEL_VALUES = 255 * 4
  5. # Defining an RGBA pixel type as a tuple of 4 integers
  6. Pixel = NewType("Pixel", Tuple[int, int, int, int])
  7. # Returns the pixel's intensity value as a float
  8. def get_pixel_intensity(pixel: Pixel) -> float:
  9.     # Sum of the pixel's channel values divided by the maximum possible intensity
  10.     return sum(pixel) / MAX_CHANNEL_VALUES

为了清晰起见,我们在第一行导入了静态类型

在上述代码中,我们定义了一个新的 Pixel 类型,一个由四个整数组成的元组,每个整数代表一个 RGBA 像素中的一个通道。然后我们又定义了一个函数来提取给定像素的强度,首先将所有通道值相加,然后将结果除以像素通道可以达到的最大值,从而有效地获得强度百分比。

一旦我们计算了像素的强度,就可以将其映射到 ASCII 字符。为此,我们必须定义一个用于表示像素的字符集

  1. # Character set for out ASCII arts
  2. CHARACTERS = (' ''.''°''*''o''O''#''@')
  3. # Restuns the character that corresponds to the given pixel intensity
  4. def map_intensity_to_character(intensity: float) -> CHARACTERS:
  5.     return CHARACTERS[round(intensity * len(CHARACTERS))]

字符集的顺序是从空格@,这意味着像素越密集,其对应的 ASCII 字符占用的空间就越多

该函数将给定的像素强度映射到集合中的一个字符,强度 * len(CHARACTERS) 的结果四舍五入,因为索引必须是整数

现在,让我们用一个简单的脚本将这些代码片段组合在一起

  1. # Import an image library for the sake of simplicity
  2. from PIL import Image
  3. # Import argv for command line arguments
  4. from sys import argv
  5. # Transforms an image into a string of ASCII characters
  6. def convert_image(image: Image) -> str:
  7.   ascii_string = ''
  8.   # Iterate over every pixel of the image
  9.   for pixel in image.getdata():
  10.     intensity = get_pixel_intensity(pixel)
  11.     character = map_intensity_to_character(intensity)
  12.     ascii_string += character
  13.   return ascii_string
  14. def main():
  15.   # Get the image name from the command line arguments list
  16.   image_name = argv[1]
  17.   # Open the image file using the PIL image library
  18.   image = Image.open(image_name)
  19.   # Convert the image to a string of ASCII characters
  20.   ascii_image = convert_image(image)
  21. if __name__ == '__main__':
  22.   main()
查看 ASCII

一旦我们获得了图像 ASCII 字符串的表示方法,接下来就是通过一种以图形方式查看它的方法,最简单的方法就是将其打印到控制台。由于图像通常按像素行组织,因此在打印它们时,我们也必须相应地使用换行符

在这里,我们编写了一个简单的函数,将 ASCII 打印到控制台以及如何从主函数调用

  1. # Prints the given ASCII art
  2. # size is a Tuple containing the width and height of the image
  3. def print_ascii_art(size: Tuple[intint], characters: str):
  4.   index = 0
  5.   # Iterate over all the rows of the image
  6.   for _ in range(size[1]):
  7.     # Print a number of characters equal to the width of the image
  8.     # from the ascii string
  9.     print(characters[index:index+size[0]])
  10.     index += size[0]
  11. def main():
  12.   image_name = argv[1]
  13.   image = Image.open(image_name)
  14.   ascii_image = convert_image(image)
  15.   # Actually print the ASCII image to the console
  16.   print_ascii_art(image.size, ascii_image)

我们先转换一张简单的图片

964f66f36ed8b0847af66df4f7d034ac.png

python converter.py image.png

Output:

3052a9d0609f9dd7ccb2bc25486d8ebe.png

可以看到,图像还是有些失真,我们再进行下优化

使用 HTML 来展示转换后的图像

  1. # The starting point of the generated HTML file
  2. HTML_TEMPLATE = """
  3. <!DOCTYPE html>
  4. <html lang="en">
  5. <head>
  6.     <meta charset="UTF-8">
  7.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  8.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  9.     <title>ASCII Art</title>
  10. </head>
  11. <body>
  12.     <div style="background-color: black; color: white;">
  13.         <pre>{}</pre>
  14.     </div>
  15. </body>
  16. </html>
  17. """
  18. def ascii_image_to_html(image_name: str, characters: str, size: Tuple[intint]):
  19.   # Open an HTML file for writing with the '.html' extension
  20.   with open(image_name + '.html''w') as image_file:
  21.     ascii_image = ''    
  22.     index = 0
  23.     # Generate the ASCII image as explained before
  24.     for _ in range(size[1]):
  25.       # Manually add a newline character at the end of each row or characters
  26.       ascii_image += characters[index:index+size[0]] + '\n'
  27.       index += size[0]
  28.     # Finally write the ASCII string to the HTML file using the template  
  29.     image_file.write(HTML_TEMPLATE.format(ascii_image))
  30. def main():
  31.   image_name = argv[1]
  32.   image = Image.open(image_name)
  33.   ascii_image = convert_image(image)
  34.   # Save the result in an HTML file
  35.   ascii_image_to_html(image_name, ascii_image, image.size)

下面就来看看不同图片转换成 ASCII 之后的效果吧

0053d19b631b778a7dbd45214cabf0ac.png

e9ae7082e60ea1b517593d5d371ecf0f.png

259a6190fd99344f1a4bf808f0525de8.png

64b8004b3c8c9378efa53ddd7516683d.png

2f210e2523dd2f725c6349195bdf5406.png

04061d43581d1276c3938822b2fd492c.png

下面是完整代码

  1. #!/usr/bin/env python3
  2. from typing import Tuple, NewType
  3. from PIL import Image
  4. from sys import argv
  5. Pixel = NewType("Pixel", Tuple[intintintint])
  6. CHARACTERS = (' ''.''°''*''o''O''#''@')
  7. MAX_CHANNEL_INTENSITY = 255
  8. MAX_CHANNEL_VALUES = MAX_CHANNEL_INTENSITY * 4 # 4 is the number of channels of a Pixel
  9. HTML_TEMPLATE = """
  10. <!DOCTYPE html>
  11. <html lang="en">
  12. <head>
  13.     <meta charset="UTF-8">
  14.     <meta http-equiv="X-UA-Compatible" content="IE=edge">
  15.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  16.     <title>ASCII Art</title>
  17. </head>
  18. <body>
  19.     <div style="background-color: black; color: white; line-height: 10px">
  20.         <pre>{}</pre>
  21.     </div>
  22. </body>
  23. </html>
  24. """
  25. def map_intensity_to_character(intensity: float) -> CHARACTERS:
  26.     return CHARACTERS[round(intensity * len(CHARACTERS))]
  27. def get_pixel_intensity(pixel: Pixel) -> float:
  28.     return sum(pixel) / 1020 # 1020 = 255 * 4
  29. def print_ascii_art(size: Tuple[intint], characters: str):
  30.     index = 0
  31.     for _ in range(size[1]):
  32.         print(characters[index:index+size[0]])
  33.         index += size[0]
  34. def ascii_image_to_html(image_name: str, characters: str, size: Tuple[intint]):
  35.     with open(image_name + '.html''w') as image_file:
  36.         ascii_image = ''    
  37.         index = 0
  38.         for _ in range(size[1]):
  39.             ascii_image += characters[index:index+size[0]] + '\n'
  40.             index += size[0]
  41.         image_file.write(HTML_TEMPLATE.format(ascii_image))
  42. def convert_image(image: Image) -> str:
  43.     ascii_string = ''
  44.     for pixel in image.getdata():
  45.         intensity = get_pixel_intensity(pixel)
  46.         character = map_intensity_to_character(intensity)
  47.         ascii_string += character
  48.     return ascii_string
  49. def main() -> None:
  50.     image_name = argv[1]
  51.     image = Image.open(image_name)
  52.     print(image.size, image.mode, image.size, image.getcolors())
  53.     ascii_image = convert_image(image)
  54.     #print_ascii_art(image.size, ascii_image)
  55.     ascii_image_to_html(image_name, ascii_image, image.size)
  56. if __name__ == '__main__':
  57.     main()

原文地址:https://towardsdatascience.com/convert-pictures-to-ascii-art-ece89582d65b

fc79642104d83b385ba7a4df9661d103.gif

16730df0ebc84cb9eb5abac430da412b.png

资讯

AI被当做炒作工具?

资讯

Gartner 发布人工智能技术曲线

资讯

机器学习可以忘记吗?是个好问题

资讯

AI不可以作为专利认证发明人

f576b4359d2c43d27a90f7d09a034a10.png

分享

c385cf16c39a5c70dbeb89a1e96091a1.png

点收藏

6d3da82f6424ba0e8e26d83f50b07377.png

点点赞

f1dc091a770390359799653b847b043c.png

点在看

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/822229
推荐阅读
相关标签
  

闽ICP备14008679号