【发布时间】:2022-01-25 02:12:18
【问题描述】:
我从事这个项目已经有一段时间了。 Ascii 所做的是,当我们运行程序时,它会要求提供一张图片,在我们给它图片后,它会将其更改为符号/字母 如果您仍然不明白,请阅读以下内容: ASCII 艺术是一种图形设计技术,它使用计算机进行演示,由 1963 年 ASCII 标准定义的 95 个可打印字符和具有专有扩展字符的 ASCII 兼容字符集拼凑而成的图片组成。
这是我用python(Pycharm,一个Python IDE)编写的代码:
import PIL.Image
ASCII_CHARS = ["@", "#", "S", "%", "?", "*", "+", ";", ":", ",", "."]
def resize_image(image, new_width=100):
width, height = image.size
ratio = height / width / 1.65
new_height = int(new_width * ratio)
resized_image = image.resize((new_width, new_height))
return(resized_image)
def grayify(image):
grayscale_image = image.convert("L")
return(grayscale_image)
def pixels_to_ascii(image):
pixels = image.getdata()
charecters = "".join([ASCII_CHARS[pixel//25] for pixel in pixels])
return(charecters)
def main(new_width=100):
path = input("Enter a valid pathname to an image :\n")
try:
image = PIL.Image.open(path)
except:
print(path, "is not a valid pathname to an image")
new_image_data = pixels_to_ascii(grayify(resize_image(image)))
pixel_count = len(new_image_data)
ascii_image = '\n'.join(new_image_data[i:(i+new_width)] for i in range(0, pixel_count, new_width))
print(ascii_image)
with open("ascii_image.txt", "w") as f:
f.write(ascii_image)
main()
每次运行时,我都会遇到 0 个错误、0 个警告、0 个弱警告和 0 个拼写错误。但它不起作用并显示:
Process finished with exit code 0
仅此而已,我该如何解决这个问题并使其正常工作...
【问题讨论】:
标签: python pycharm ide python-imaging-library ascii-art