简体   繁体   中英

doesn't do anythinng when I run the ascii art program in pycharm

I have been working on this project for a while. What Ascii does is when we run the program, it asks for a picture, and after we give it the picture it changes it into symbols/letters if u still didn't understand, read this: ASCII art is a graphic design technique that uses computers for presentation and consists of pictures pieced together from the 95 printable characters defined by the ASCII Standard from 1963 and ASCII compliant character sets with proprietary extended characters.

so here is the code I wrote in python (Pycharm, a 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()

and every time I run this I have 0 errors, 0 warnings, 0 weak warnings, and 0 typos. But it doesn't work and shows this:

Process finished with exit code 0

and nothing else, how can I fix this and make it work...

You are calling main() function inside the main() function itself, hence there is no output.
You should place main() without any indentation:

def main():
    # code

main()

Best Practices for calling main() function:

if __name__ == "__main__":
    main()

So that you can reuse this program further without execution of main function in any import.

One more thing, you are not doing anything in main function after opening image with PIL hence no output, and you should not write import PIL.Image if you are using PIL.Image , you should use import PIL , else it is recommended to use from PIL import Image when you want to use Image class from PIL . You are also using image inside the body of except .

Suggestions:
You are taking path to image file as input which is somewhat annoying to the user, you can use tkinter.filedialog to get file path like this:

from tkinter.filedialog import askopenfilename
.
.
def main():
    path = askopenfilename(filetypes=(("Images", ["*.jpg", "*.png", "*.jpeg"]), ))
# ..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM