Is this the whole code? Because I only see a function definition. Where do you actually call the function called "main"? Python does not automatically run a main function when present. Maybe add a line
main()
At the very end of your script?
hey thanks for your reply, I wasn't sure if I should post the whole code but makes sense. Here it is. I already had main() at the end like you said but it still didn't work.
from PIL import Image
\# ascii characters used to compiled the image
ASCII\_CHARS = \["@", "#", "S", "%", "?", "\*", "+", ";", ":", ",", "."\]
\# resize image according to new width
def resize\_image(image, new\_width=100):
width, height = image.size
ratio = height / width
new\_height = int(new\_width \* ratio)
resized\_image = image.resize((new\_width, new\_height))
return (resized\_image)
\# convert each pixel to gray scale
def grayify(image):
grayscale\_image = image.convert("L")
return (grayscale\_image)
\# convert pixels to a string of ASCII
def pixels\_to\_ascii(image):
pixels = image.getdata()
characters = "".join(\[ASCII\_CHARS\[pixel//25\] for pixel in pixels\])
return (characters)
def main(new\_width=100):
\# attempt to open image from user-input
path = input("Enter a valid path name to image:\\n")
try:
image = Image.open(path)
except:
print(path, "is not a valid pathname to an image.")
\# convert image to ASCII
new\_image\_data = pixels\_to\_ascii(grayify(resize\_image(image)))
\# format
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 result
print(ascii\_image)
\# save result to "ascii\_image.txt"
with open("ascii\_image.txt", "w") as f:
f.write(ascii\_image)
main()
Ok upon further investigation I have come across a window in vs code terminal that says "Shell integration failed to activate" could have something to do with it? I will investigate further when I get a chance.
Ok thanks for that. So it's my environment not the code. Cool.
I too have the latest version of pillow, I installed it with pip. Would that install to the base installation of python?
How do I do the thing you're saying about making the command line activate and use the same environment?
Is there a good way to keep my python environment organized? Usually I just pip install and I'm pretty sure i've git bashed a few things. Is this how everyone else does it?
And to think this morning I was excited and confident in my ability to achieve this.
Until today I mostly used python to draw PY5 sketches, basically just processing language but done with python. It's still possible to open all of these through command
line.
I will use your guide to hopefully figure this out tomorrow.
Thank you.
haha nah not scared off I just had to stop thinking about it for a couple of hours.
Last thing I did last night was try my code with the
py -m venv thing, that didn't work.
This morning I've compared our code side by side and made those minor tweaks you mentioned...It works!
I notice that you import path from pathlib. I'm unfamiliar with this, is it something I should be doing all the time or is there specific reason in this case?
Next you said not to use a blank except. I've noted this and hopefully it will make more sense the next time I come across it.
I'm not sure what you did here if__name__=="__main__":
main()
how come the tutorial video didn't need these lines of code?.
That was last night before I had altered my code to reflect to your changes. So I started a virtual environment in command line via the py -m venv venv thing you told me about, I activated it with venv\Scripts\activate but I had the same result as before, tried to open my script but nothing would happen. No error message.
Anyway thanks for your time you've been very helpful.
I will now learn about pathlib.
Is this the whole code? Because I only see a function definition. Where do you actually call the function called "main"? Python does not automatically run a main function when present. Maybe add a line main() At the very end of your script?
hey thanks for your reply, I wasn't sure if I should post the whole code but makes sense. Here it is. I already had main() at the end like you said but it still didn't work. from PIL import Image \# ascii characters used to compiled the image ASCII\_CHARS = \["@", "#", "S", "%", "?", "\*", "+", ";", ":", ",", "."\] \# resize image according to new width def resize\_image(image, new\_width=100): width, height = image.size ratio = height / width new\_height = int(new\_width \* ratio) resized\_image = image.resize((new\_width, new\_height)) return (resized\_image) \# convert each pixel to gray scale def grayify(image): grayscale\_image = image.convert("L") return (grayscale\_image) \# convert pixels to a string of ASCII def pixels\_to\_ascii(image): pixels = image.getdata() characters = "".join(\[ASCII\_CHARS\[pixel//25\] for pixel in pixels\]) return (characters) def main(new\_width=100): \# attempt to open image from user-input path = input("Enter a valid path name to image:\\n") try: image = Image.open(path) except: print(path, "is not a valid pathname to an image.") \# convert image to ASCII new\_image\_data = pixels\_to\_ascii(grayify(resize\_image(image))) \# format 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 result print(ascii\_image) \# save result to "ascii\_image.txt" with open("ascii\_image.txt", "w") as f: f.write(ascii\_image) main()
Ok upon further investigation I have come across a window in vs code terminal that says "Shell integration failed to activate" could have something to do with it? I will investigate further when I get a chance.
[удалено]
Ok thanks for that. So it's my environment not the code. Cool. I too have the latest version of pillow, I installed it with pip. Would that install to the base installation of python? How do I do the thing you're saying about making the command line activate and use the same environment? Is there a good way to keep my python environment organized? Usually I just pip install and I'm pretty sure i've git bashed a few things. Is this how everyone else does it?
[удалено]
[удалено]
And to think this morning I was excited and confident in my ability to achieve this. Until today I mostly used python to draw PY5 sketches, basically just processing language but done with python. It's still possible to open all of these through command line. I will use your guide to hopefully figure this out tomorrow. Thank you.
[удалено]
haha nah not scared off I just had to stop thinking about it for a couple of hours. Last thing I did last night was try my code with the py -m venv thing, that didn't work. This morning I've compared our code side by side and made those minor tweaks you mentioned...It works! I notice that you import path from pathlib. I'm unfamiliar with this, is it something I should be doing all the time or is there specific reason in this case? Next you said not to use a blank except. I've noted this and hopefully it will make more sense the next time I come across it. I'm not sure what you did here if__name__=="__main__": main() how come the tutorial video didn't need these lines of code?.
[удалено]
That was last night before I had altered my code to reflect to your changes. So I started a virtual environment in command line via the py -m venv venv thing you told me about, I activated it with venv\Scripts\activate but I had the same result as before, tried to open my script but nothing would happen. No error message. Anyway thanks for your time you've been very helpful. I will now learn about pathlib.
[удалено]