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

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

image 的本质


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

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

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


将 pixels 转换为 ASCCII


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

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

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

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

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


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

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

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


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

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

# Import an image library for the sake of simplicity
from PIL import Image
# Import argv for command line arguments
from sys import argv
# Transforms an image into a string of ASCII characters
def convert_image(image: Image) -> str:
  ascii_string = ''
  # Iterate over every pixel of the image
  for pixel in image.getdata():
    intensity = get_pixel_intensity(pixel)
    character = map_intensity_to_character(intensity)
    ascii_string += character
  return ascii_string
def main():
  # Get the image name from the command line arguments list
  image_name = argv[1]
  # Open the image file using the PIL image library
  image = Image.open(image_name)
  # Convert the image to a string of ASCII characters
  ascii_image = convert_image(image)
if __name__ == '__main__':
  main()


查看 ASCII


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

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

# Prints the given ASCII art
# size is a Tuple containing the width and height of the image
def print_ascii_art(size: Tuple[int, int], characters: str):
  index = 0
  # Iterate over all the rows of the image
  for _ in range(size[1]):
    # Print a number of characters equal to the width of the image
    # from the ascii string
    print(characters[index:index+size[0]])
    index += size[0]
def main():
  image_name = argv[1]
  image = Image.open(image_name)
  ascii_image = convert_image(image)
  # Actually print the ASCII image to the console
  print_ascii_art(image.size, ascii_image)

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

网络异常,图片无法展示
|

python converter.py image.png

Output:

微信图片_20220521154924.png

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

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

# The starting point of the generated HTML file
HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ASCII Art</title>
</head>
<body>
    <div style="background-color: black; color: white;">
        <pre>{}</pre>
    </div>
</body>
</html>
"""
def ascii_image_to_html(image_name: str, characters: str, size: Tuple[int, int]):
  # Open an HTML file for writing with the '.html' extension
  with open(image_name + '.html', 'w') as image_file:
    ascii_image = ''    
    index = 0
    # Generate the ASCII image as explained before
    for _ in range(size[1]):
      # Manually add a newline character at the end of each row or characters
      ascii_image += characters[index:index+size[0]] + '\n'
      index += size[0]
    # Finally write the ASCII string to the HTML file using the template  
    image_file.write(HTML_TEMPLATE.format(ascii_image))
def main():
  image_name = argv[1]
  image = Image.open(image_name)
  ascii_image = convert_image(image)
  # Save the result in an HTML file
  ascii_image_to_html(image_name, ascii_image, image.size)

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

微信图片_20220521154928.png

微信图片_20220521154931.png

微信图片_20220521154934.png

微信图片_20220521154937.png

微信图片_20220521154940.png

微信图片_20220521154943.png

下面是完整代码

#!/usr/bin/env python3
from typing import Tuple, NewType
from PIL import Image
from sys import argv
Pixel = NewType("Pixel", Tuple[int, int, int, int])
CHARACTERS = (' ', '.', '°', '*', 'o', 'O', '#', '@')
MAX_CHANNEL_INTENSITY = 255
MAX_CHANNEL_VALUES = MAX_CHANNEL_INTENSITY * 4 # 4 is the number of channels of a Pixel
HTML_TEMPLATE = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ASCII Art</title>
</head>
<body>
    <div style="background-color: black; color: white; line-height: 10px">
        <pre>{}</pre>
    </div>
</body>
</html>
"""
def map_intensity_to_character(intensity: float) -> CHARACTERS:
    return CHARACTERS[round(intensity * len(CHARACTERS))]
def get_pixel_intensity(pixel: Pixel) -> float:
    return sum(pixel) / 1020 # 1020 = 255 * 4
def print_ascii_art(size: Tuple[int, int], characters: str):
    index = 0
    for _ in range(size[1]):
        print(characters[index:index+size[0]])
        index += size[0]
def ascii_image_to_html(image_name: str, characters: str, size: Tuple[int, int]):
    with open(image_name + '.html', 'w') as image_file:
        ascii_image = ''    
        index = 0
        for _ in range(size[1]):
            ascii_image += characters[index:index+size[0]] + '\n'
            index += size[0]
        image_file.write(HTML_TEMPLATE.format(ascii_image))
def convert_image(image: Image) -> str:
    ascii_string = ''
    for pixel in image.getdata():
        intensity = get_pixel_intensity(pixel)
        character = map_intensity_to_character(intensity)
        ascii_string += character
    return ascii_string
def main() -> None:
    image_name = argv[1]
    image = Image.open(image_name)
    print(image.size, image.mode, image.size, image.getcolors())
    ascii_image = convert_image(image)
    #print_ascii_art(image.size, ascii_image)
    ascii_image_to_html(image_name, ascii_image, image.size)
if __name__ == '__main__':
    main()
相关文章
|
5月前
|
存储 人工智能 开发工具
AI助理化繁为简,速取代码参数——使用python SDK 处理OSS存储的图片
只需要通过向AI助理提问的方式输入您的需求,即可瞬间获得核心流程代码及参数,缩短学习路径、提升开发效率。
1495 4
AI助理化繁为简,速取代码参数——使用python SDK 处理OSS存储的图片
|
5月前
|
Python
Python实用记录(六):如何打开txt文档并删除指定绝对路径下图片
这篇文章介绍了如何使用Python打开txt文档,删除文档中指定路径的图片,并提供了一段示例代码来展示这一过程。
53 1
|
5月前
|
计算机视觉 Python
Python实用记录(一):如何将不同类型视频按关键帧提取并保存图片,实现图片裁剪功能
这篇文章介绍了如何使用Python和OpenCV库从不同格式的视频文件中按关键帧提取图片,并展示了图片裁剪的方法。
150 0
|
7月前
|
计算机视觉 Windows Python
windows下使用python + opencv读取含有中文路径的图片 和 把图片数据保存到含有中文的路径下
在Windows系统中,直接使用`cv2.imread()`和`cv2.imwrite()`处理含中文路径的图像文件时会遇到问题。读取时会返回空数据,保存时则无法正确保存至目标目录。为解决这些问题,可以使用`cv2.imdecode()`结合`np.fromfile()`来读取图像,并使用`cv2.imencode()`结合`tofile()`方法来保存图像至含中文的路径。这种方法有效避免了路径编码问题,确保图像处理流程顺畅进行。
586 1
|
4月前
|
开发工具 Python
[oeasy]python043_自己制作的ascii码表_循环语句_条件语句_缩进_indent
本文介绍了如何使用Python制作ASCII码表,回顾了上一次课程中`print`函数的`end`参数,并通过循环和条件语句实现每8个字符换行的功能。通过调整代码中的缩进,实现了正确的输出格式。最后展示了制作完成的ASCII码表,并预告了下一次课程的内容。
37 2
|
4月前
|
人工智能 Shell 开发工具
[oeasy]python0041_输出ASCII码表_英文字符编码_键盘字符_ISO_646
本文介绍了ASCII码表的生成与使用,包括英文字符、数字和符号的编码。通过Python代码遍历0到127的ASCII值,解决了找不到竖线符号的问题,并解释了ASCII码的固定映射关系及其重要性。文章还介绍了ASCII码的历史背景,以及它如何成为国际标准ISO 646。最后,通过安装`ascii`程序展示了完整的ASCII码表。
50 1
|
5月前
|
Python
Socket学习笔记(二):python通过socket实现客户端到服务器端的图片传输
使用Python的socket库实现客户端到服务器端的图片传输,包括客户端和服务器端的代码实现,以及传输结果的展示。
229 3
Socket学习笔记(二):python通过socket实现客户端到服务器端的图片传输
|
5月前
|
Python
Python实用记录(四):os模块-去后缀或者改后缀/指定目录下图片或者子目录图片写入txt/csv
本文介绍了如何使用Python的os模块来操作文件,包括更改文件后缀、分割文件路径和后缀、将指定目录下的所有图片写入txt文档,以及将指定目录下所有子目录中的图片写入csv文档,并为每个子目录分配一个标签。
58 1
|
5月前
|
编解码 UED Python
Python批量修改指定目录下图片的大小名文章
Python批量修改指定目录下图片的大小名文章
34 1
|
5月前
|
iOS开发 MacOS Python
Python编程小案例—利用flask查询本机IP归属并输出网页图片
Python编程小案例—利用flask查询本机IP归属并输出网页图片
53 1

热门文章

最新文章