• 元宇宙:本站分享元宇宙相关资讯,资讯仅代表作者观点与平台立场无关,仅供参考.

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

  • AI科技大本营
  • 2021年9月30日03时

作者 |周萝卜

来源 |萝卜大杂烩

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

image 的本质

首先,我们先来阐明下图像在计算机系统中的表示方式。图片通常以 .png 或 .jpg 等格式存储在磁盘上,所有这些文件类型都具有相似的结构:它们大致由标题和数据部分组成,前者存储有关图像的有用信息,例如其格式签名,而后者存储实际的像素数据
我们看到的图像实际由像素组成,像素是我们都熟悉的光栅图像中最小的可寻址元素,它们通常表示为一组通道,也称为颜色。在最常见的颜色值中,有经典的 RGB(红绿蓝)和 RGBA(红绿蓝 Alpha)。两者之间的区别在于后者有一个额外的通道,称为“alpha”,用于指定图像的不透明度。
RGBA 是我们将要使用的,因为它也可以用来表示空背景

将 pixels 转换为 ASCCII

现在我们已经了解了图像的表示方式,接下来讨论如何将像素转换为实际的 ASCII 字符
要理解这一点,我们首先看一下像素颜色强度,该值是指所有像素通道的总和除以通道可以具有的最大值的总和(在本例中为 255)
#Importtypesforclarity
fromtypingimportNewType,Tuple

#Maximumvaluethesumofthepixel'schannelvaluescanreach
MAX_CHANNEL_VALUES=255*4

#DefininganRGBApixeltypeasatupleof4integers
Pixel=NewType("Pixel",Tuple[int,int,int,int])

#Returnsthepixel'sintensityvalueasafloat
defget_pixel_intensity(pixel:Pixel)->float:
#Sumofthepixel'schannelvaluesdividedbythemaximumpossibleintensity
returnsum(pixel)/MAX_CHANNEL_VALUES
为了清晰起见,我们在第一行导入了静态类型
在上述代码中,我们定义了一个新的 Pixel 类型,一个由四个整数组成的元组,每个整数代表一个 RGBA 像素中的一个通道。然后我们又定义了一个函数来提取给定像素的强度,首先将所有通道值相加,然后将结果除以像素通道可以达到的最大值,从而有效地获得强度百分比。
一旦我们计算了像素的强度,就可以将其映射到 ASCII 字符。为此,我们必须定义一个用于表示像素的字符集
#CharactersetforoutASCIIarts
CHARACTERS=('','.','°','*','o','O','#','@')

#Restunsthecharacterthatcorrespondstothegivenpixelintensity
defmap_intensity_to_character(intensity:float)->CHARACTERS:
returnCHARACTERS[round(intensity*len(CHARACTERS))]
字符集的顺序是从空格@,这意味着像素越密集,其对应的 ASCII 字符占用的空间就越多
该函数将给定的像素强度映射到集合中的一个字符,强度 * len(CHARACTERS) 的结果四舍五入,因为索引必须是整数
现在,让我们用一个简单的脚本将这些代码片段组合在一起
#Importanimagelibraryforthesakeofsimplicity
fromPILimportImage
#Importargvforcommandlinearguments
fromsysimportargv

#TransformsanimageintoastringofASCIIcharacters
defconvert_image(image:Image)->str:
ascii_string=''
#Iterateovereverypixeloftheimage
forpixelinimage.getdata():
intensity=get_pixel_intensity(pixel)
character=map_intensity_to_character(intensity)
ascii_string+=character
returnascii_string

defmain():
#Gettheimagenamefromthecommandlineargumentslist
image_name=argv[1]
#OpentheimagefileusingthePILimagelibrary
image=Image.open(image_name)
#ConverttheimagetoastringofASCIIcharacters
ascii_image=convert_image(image)

if__name__=='__main__':
main()

查看 ASCII

一旦我们获得了图像 ASCII 字符串的表示方法,接下来就是通过一种以图形方式查看它的方法,最简单的方法就是将其打印到控制台。由于图像通常按像素行组织,因此在打印它们时,我们也必须相应地使用换行符
在这里,我们编写了一个简单的函数,将 ASCII 打印到控制台以及如何从主函数调用
#PrintsthegivenASCIIart
#sizeisaTuplecontainingthewidthandheightoftheimage
defprint_ascii_art(size:Tuple[int,int],characters:str):
index=0
#Iterateoveralltherowsoftheimage
for_inrange(size[1]):
#Printanumberofcharactersequaltothewidthoftheimage
#fromtheasciistring
print(characters[index:index+size[0]])
index+=size[0]

defmain():
image_name=argv[1]
image=Image.open(image_name)
ascii_image=convert_image(image)
#ActuallyprinttheASCIIimagetotheconsole
print_ascii_art(image.size,ascii_image)
我们先转换一张简单的图片

pythonconverter.pyimage.png
Output:

可以看到,图像还是有些失真,我们再进行下优化
使用 HTML 来展示转换后的图像
#ThestartingpointofthegeneratedHTMLfile
HTML_TEMPLATE="""
<!DOCTYPEhtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<metahttp-equiv="X-UA-Compatible"content="IE=edge">
<metaname="viewport"content="width=device-width,initial-scale=1.0">
<title>ASCIIArt</title>
</head>
<body>
<divstyle="background-color:black;color:white;">
<pre>{}</pre>
</div>
</body>
</html>
"""


defascii_image_to_html(image_name:str,characters:str,size:Tuple[int,int]):
#OpenanHTMLfileforwritingwiththe'.html'extension
withopen(image_name+'.html','w')asimage_file:
ascii_image=''
index=0
#GeneratetheASCIIimageasexplainedbefore
for_inrange(size[1]):
#Manuallyaddanewlinecharacterattheendofeachroworcharacters
ascii_image+=characters[index:index+size[0]]+'\n'
index+=size[0]
#FinallywritetheASCIIstringtotheHTMLfileusingthetemplate
image_file.write(HTML_TEMPLATE.format(ascii_image))

defmain():
image_name=argv[1]
image=Image.open(image_name)
ascii_image=convert_image(image)
#SavetheresultinanHTMLfile
ascii_image_to_html(image_name,ascii_image,image.size)
下面就来看看不同图片转换成 ASCII 之后的效果吧





下面是完整代码
#!/usr/bin/envpython3
fromtypingimportTuple,NewType
fromPILimportImage
fromsysimportargv


Pixel=NewType("Pixel",Tuple[int,int,int,int])

CHARACTERS=('','.','°','*','o','O','#','@')

MAX_CHANNEL_INTENSITY=255
MAX_CHANNEL_VALUES=MAX_CHANNEL_INTENSITY*4#4isthenumberofchannelsofaPixel

HTML_TEMPLATE="""
<!DOCTYPEhtml>
<htmllang="en">
<head>
<metacharset="UTF-8">
<metahttp-equiv="X-UA-Compatible"content="IE=edge">
<metaname="viewport"content="width=device-width,initial-scale=1.0">
<title>ASCIIArt</title>
</head>
<body>
<divstyle="background-color:black;color:white;line-height:10px">
<pre>{}</pre>
</div>
</body>
</html>
"""



defmap_intensity_to_character(intensity:float)->CHARACTERS:
returnCHARACTERS[round(intensity*len(CHARACTERS))]


defget_pixel_intensity(pixel:Pixel)->float:
returnsum(pixel)/1020#1020=255*4


defprint_ascii_art(size:Tuple[int,int],characters:str):
index=0
for_inrange(sie[1]):
print(characters[index:index+size[0]])
index+=size[0]


defascii_image_to_html(image_name:str,characters:str,size:Tuple[int,int]):
withopen(image_name+'.html','w')asimage_file:
ascii_image=''
index=0
for_inrange(size[1]):
ascii_image+=characters[index:index+size[0]]+'\n'
index+=size[0]
image_file.write(HTML_TEMPLATE.format(ascii_image))


defconvert_image(image:Image)->str:
ascii_string=''
forpixelinimage.getdata():
intensity=get_pixel_intensity(pixel)
character=map_intensity_to_character(intensity)
ascii_string+=character
returnascii_string


defmain()->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()
原文地址:https://towardsdatascience.com/convert-pictures-to-ascii-art-ece89582d65b

资讯

AI被当做炒作工具?

资讯

Gartner 发布人工智能技术曲线

资讯

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

资讯

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


分享

点收藏

点点赞

点在看

Copyright © 2021.Company 元宇宙YITB.COM All rights reserved.元宇宙YITB.COM