Python中将PPM图像转换为ASCII艺术
我需要写一个程序,从命令行读取一个文件,然后把它转换成ASCII艺术。我使用的是PPM格式,这里有一个链接到项目的详细信息。
这是我目前的进展:
import sys
def main(filename):
image = open(filename)
#reads through the first three lines
color = image.readline().splitlines()
size_width, size_height = image.readline().split()
max_color = image.readline().splitlines()
#reads the body of the file
pixels = image.read().split()
red = 0
green = 0
blue = 0
r_g_b_value = []
#pulls out the values of each tuple and coverts it to its grayscale value
for i in pixels:
if i != "\n" or " ":
if len(i) == 3:
red = int(i[0]) * .3
green = int(i[1]) * .59
blue = int(i[2]) * .11
elif len(i) == 2:
red == int(i[0]) * .3
green == int(i[1]) *.59
blue == 0
elif len(i) == 1:
red == int(i[0]) * .3
green == 0
blue == 0
r_g_b_value = [red + green + blue]
character = []
for j in len(r_g_b_value):
if int(j) <= 16:
character = " "
elif int(j) > 16 and int(j) <= 32:
character = "."
elif int(j) > 32 and int(j) <= 48:
character = ","
elif int(j) > 48 and int(j) <= 64:
charcter = ":"
elif int(j) > 64 and int(j) <= 80:
character = ";"
elif int(j) > 80 and int(j) <= 96:
character = "+"
elif int(j) > 96 and int(j) <= 112:
character = "="
elif int(j) > 112 and int(j) <= 128:
character = "o"
elif int(j) > 128 and int(j) <= 144:
character = "a"
elif int(j) > 144 and int(j) <= 160:
character = "e"
elif int(j) > 160 and int(j) <= 176:
character = "0"
elif int(j) > 176 and int(j) <= 192:
character = "$"
elif int(j) > 192 and int(j) <= 208:
character = "@"
elif int(j) > 208 and int(j) <= 224:
character = "A"
elif int(j) > 224 and int(j) <= 240:
character = "#"
else:
character = "M"
grayscale = character
print(grayscale)
main(sys.argv[1])
我遇到了一个错误,提示说'int'对象不可迭代。有没有简单的方法来解决这个问题?还有,如何在打印的时候保持图像的完整性?
最后,我不太确定的是,如何保持图片的宽度和高度。
任何帮助都会非常感谢。
3 个回答
0
我之前用C#写过一个这样的东西,我用这个公式来计算要使用的字符:
index_into_array = (int)(r_g_b_value * (chars_array_length / (255.0)));
至于宽度和高度,你可以把每两行的垂直像素平均一下,这样就能把高度减半。
编辑1:针对评论的回复:基本的想法是把你的RGB值从0到255的范围缩放到0到数组的长度,然后用这个值作为索引。
编辑2:更新一下,之前我忽略了你的灰度归一化。
4
这里是你修改过并且可以运行的代码。
虽然它不是最优的,没考虑到头部可能有多或少的注释,也没有处理异常,但这算是一个开始。
import sys
import numpy as np
RGBS = range(16, 255, 16)
CHARS = [' ', '.', ',', ':', ';', '+', '=', 'o',
'a', 'e', '0', '$', '@', 'A', '#']
FACTORS = [.3, .59, .11]
def main(filename):
image = open(filename)
#reads header lines
color = image.readline()
_ = image.readline()
size_width, size_height = image.readline().split()
max_color = image.readline()
size_width = int(size_width)
max_color = int(max_color)
#reads the body of the file
data = [int(p) for p in image.read().split()]
#converts to array and reshape
data = np.array(data)
pixels = data.reshape((len(data)/3, 3))
#calculate rgb value per pixel
rgbs = pixels * FACTORS
sum_rgbs = rgbs.sum(axis=1)
rgb_values = [item * 255 / max_color for item in sum_rgbs]
grayscales = []
#pulls out the value of each pixel and coverts it to its grayscale value
for indx, rgb_val in enumerate(rgb_values):
#if max width, new line
if (indx % size_width) == 0 : grayscales.append('\n')
for achar, rgb in zip(CHARS, RGBS):
if rgb_val <= rgb:
character = achar
break
else:
character = 'M'
grayscales.append(character)
print ''.join(grayscales)
main('test.ppm')
这些是ppm图像和ASCII艺术的结果。
还有我用来做示例的微型ppm文件:
P3
# test.ppm
4 4
15
0 0 0 0 0 0 0 0 0 15 0 15
0 0 0 0 15 7 0 0 0 0 0 0
0 0 0 0 0 0 0 15 7 0 0 0
15 0 15 0 0 0 0 0 0 0 0 0
9
你可以使用image-to-ansi.py
这个脚本来进行转换。
首先,下载image-to-ansi.py
:
wget https://gist.githubusercontent.com/klange/1687427/raw/image-to-ansi.py
然后,把它改成Python 3的语法:
2to3 -w image-to-ansi.py
接下来,安装一个叫Pillow的图像库:
pip3 install pillow
把这个脚本保存为ppmimage.py
:
# Parses a PPM file and loads it into image-to-ansi.py
import re, itertools, sys
from PIL import Image
image_to_ansi = __import__("image-to-ansi") # __import__ because of minuses in filename. From https://gist.github.com/1687427
if __name__ == '__main__':
im = Image.open('/dev/stdin')
for y in range(im.size[1]):
for x in range(im.size[0]):
p = im.getpixel((x,y))
h = "%2x%2x%2x" % (p[0],p[1],p[2])
short, rgb = image_to_ansi.rgb2short(h)
sys.stdout.write("\033[48;5;%sm " % short)
sys.stdout.write("\033[0m\n")
sys.stdout.write("\n")
你可以这样测试这个脚本(假设你已经安装了netpbm
和imagemagick
):
convert -resize $(($COLUMNS*2))x$(($LINES*2)) logo: pnm:- | pnmtoplainpnm | python3 ppmimage.py
在我的电脑上,效果是这样的: