我必须创建一个程序,从命令行读取文件并将其转换为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]) …Run Code Online (Sandbox Code Playgroud)