[Python]图片转字符画——这就是我的表情!!!!!!

一、前言

偶尔看到一些视频,他们把图片转字符画,平常也没有去关注,今天来捣鼓一下。
研究了一下发现超级简单。
网上也有很多教学。


二、代码实现

from PIL import Image
# 表示字符颜色,由深到浅(可自定义)
ascii_chars = " .,:-=+o*Oθ%@■▇"
# 像素点 转 字符
# 根据像素点颜色 选取 字符
def pixel_to_char(pixel):
    gray = int(0.2126 * pixel[0] + 0.7152 * pixel[1] + 0.0722 * pixel[2])
    unit = 256 / len(ascii_chars)
    return ascii_chars[int(gray / unit)]
    
# 图片 转 字符画
# 参数:
#     image_path:图片路径,
#	  output_width : 图片大小
def image_to_ascii(image_path, output_width=100):
    img = Image.open(image_path)
    width, height = img.size
    output_height = int(output_width * height / width * 0.55)
    img = img.resize((output_width, output_height))
    ascii_img = ''
    for i in range(output_height):
        for j in range(output_width):
            pixel = img.getpixel((j, i))
            ascii_img += pixel_to_char(pixel)
        ascii_img += '\n'
    return ascii_img
# 调用
print(image_to_ascii(你图片的路径, 180))

三、效果

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

四、扩展——原理(中英双语)

Introduction to ASCII art

ASCII art is a graphic design technique that uses computers for presentation and consists of pictures pieced together from the 95 printable (from a total of 128) characters defined by the ASCII Standard from 1963 and ASCII compliant character sets with proprietary extended characters (beyond the 128 characters of standard 7-bit ASCII).

ASCII art是一种使用计算机进行展示的图形设计技术,由1963年ASCII标准定义的95个可打印字符(共128个)和具有专有扩展字符(超过标准7位ASCII的128个字符)的ASCII兼容字符集拼凑而成的图片组成。

The term is also loosely used to refer to text-based visual art in general.

该术语也被松散地用于泛指基于文本的视觉艺术。

ASCII art can be created with any text editor and is often used with free-form languages.

ASCII art可以用任何文本编辑器创建,并且经常与自由形式的语言一起使用。

Most examples of ASCII art require a fixed-width font (non-proportional fonts, as on a traditional typewriter) such as Courier for presentation.

ASCII艺术的大多数例子都需要固定宽度的字体(非比例字体,如传统打字机上的字体),如Courier。

Among the oldest known examples of ASCII art are the creations by computer-art pioneer Kenneth Knowlton from around 1966, who was working for Bell Labs at the time.

ASCII art最古老的例子是1966年左右计算机艺术先驱肯尼斯·诺尔顿的作品,当时他在贝尔实验室工作。

“Studies in Perception I” by Ken Knowlton and Leon Harmon from 1966 shows some examples of their early ASCII art. ASCII art was invented, in large part, because early printers often lacked graphics ability and thus characters were used in place of graphic marks.

Ken Knowlton和Leon Harmon 1966年的“感知I研究”展示了他们早期ASCII art的一些例子。ASCII art的发明在很大程度上是因为早期的打印机往往缺乏图形能力,因此使用字符来代替图形标记。

Also, to mark divisions between different print jobs from different users, bulk printers often used ASCII art to print large banners, making the division easier to spot so that the results could be more easily separated by a computer operator or clerk.

此外,为了标记来自不同用户的不同打印作业之间的划分,批量打印机通常使用ASCII art来打印大型横幅,使划分更容易被发现,这样计算机操作员或职员就可以更容易地分离结果。

ASCII art was also used in an early e-mail when images could not be embedded. You can find out more about them.

ASCII art也被用于早期的电子邮件中,当时图像无法嵌入。你可以了解更多关于他们的信息。

How it works | 工作原理:

here are the steps the program takes to generate the ASCII
以下是程序生成ASCII的步骤

图像:
image:

  • Convert the input image to grayscale.
    将输入图像转换为灰度。
  • Split the image into M×N tiles.
    将图像拆分为M×N个瓦片。
  • Correct M (the number of rows) to match the image and font aspect ratio.
    更正M(行数)以匹配图像和字体纵横比。
  • Compute the average brightness for each image tile and then look up a suitable ASCII character for each.
    计算每个图像瓦片的平均亮度,然后为每个瓦片查找合适的ASCII字符。
  • Assemble rows of ASCII character strings and print them to a file to form the final image.
    将ASCII字符串的行组合起来,并将它们打印到文件中,以形成最终图像。

Requirements | 要求

You will do this program in python and we will use Pillow which is Python Imaging Library for reading the images, accessing their underlying data, creating and modifying them, and also the scientific module Numpy to compute averages.

您将在python中执行此程序,我们将使用Pillow,它是python成像库,用于读取图像、访问其底层数据、创建和修改图像,还将使用科学模块Numpy来计算平均值。


Converting an image to ASCII image in Python

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码鹿的笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值