defpixels_to_ascii(image): pixels = image.getdata() characters = "".join([ASCII_CHARS[pixel//25] for pixel in pixels]) return(characters)
defmain(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\n") 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 inrange(0, pixel_count, new_width))
print(ascii_image)
withopen("ascii.txt", "w") as f: f.write(ascii_image)