So uh I'm a new python learner trynna make an ASCII image converter.. So I wrote a code that I got off from youtube and it's giving me errors.. I reviewed the video and everything's fine
Heres the code
#note: READ THIS OR WONT WORK
#bassically to make this work you need to place this piece o code inside the same folder as your image thats being converted
#then run the code and write the file name and extension name (.jpg, .png, etc.)
#should run..
#the ascii import
import PIL.Image
#things to build the output image
#can be changed but this variety is decent
ASCII_CHARS = ["@", "#", "S", "%", "?", "*", " ", ";", ":", ",", "."]
#resizing image
#pretty confusing but just keep this piece
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)
#making stuff grayscale
#this is done cus ASCII dont got any integerated colour thing
#defining the graying variable and then applying it to the image
def grayify(image):
grayscale_image = image.convert("L")
return(grayscale_image)
#make the pixels the ASCII combination mentioned above (mixture of #'s and $'s and whatever)
def pixels_to_ascii(image):
pixels = image.getdara()
characters = "".join([ASCII_CHARS[pixel//25] for pixel in pixels])
return(characters)
def main(new_width=100):
#opening source image
path = input ("Enter pathname for image:\n")
try:
image = PIL.Image.open(path)
#if no pathname found
except:
print(path, "isnt a valid pathname, try another one bro")
#define the image to make it gray then resize the gray output
new_image_data = pixels_to_ascii(grayify(resize_image(image)))
#formattin'
pixel_count = len(new_image_data)
#'ascii_image' is the head of everything where all grayscales and widths and ratios and whatnot is connected
ascii_image = "\n".join(new_image_data[i:(i new_width)] for i in range(0, pixel_count, new_width))
#printing!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
print(ascii_image)
#save result
with open("ascii_image.txt", "w") as f:
f.write(ascii_image)
main()
And here's the errors
Traceback (most recent call last):
File "C:\Users\user\PycharmProjects\pythonProject\random\ASCII.py", line 59, in <module>
main()
File "C:\Users\user\PycharmProjects\pythonProject\random\ASCII.py", line 46, in main
new_image_data = pixels_to_ascii(grayify(resize_image(image)))
UnboundLocalError: local variable 'image' referenced before assignment
Process finished with exit code 1
So i wanna know whats wrong and what these errors means
CodePudding user response:
If image = PIL.Image.open(path)
generates an exception, then the variable image
is never assigned a value. Then, you get the error when you try to reference it here: new_image_data = pixels_to_ascii(grayify(resize_image(image)))
.
You need to update your except
clause to either exit the program or assign another value to image
.
CodePudding user response:
Which means python looking for variable called image & It's not found...You alredy decleraed image var in try block...But, Some how code not enetering into Try block...Try moving code below except
and move code to try
block
#note: READ THIS OR WONT WORK
#bassically to make this work you need to place this piece o code inside the same folder as your image thats being converted
#then run the code and write the file name and extension name (.jpg, .png, etc.)
#should run..
#the ascii import
import PIL.Image
#things to build the output image
#can be changed but this variety is decent
ASCII_CHARS = ["@", "#", "S", "%", "?", "*", " ", ";", ":", ",", "."]
#resizing image
#pretty confusing but just keep this piece
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)
#making stuff grayscale
#this is done cus ASCII dont got any integerated colour thing
#defining the graying variable and then applying it to the image
def grayify(image):
grayscale_image = image.convert("L")
return(grayscale_image)
#make the pixels the ASCII combination mentioned above (mixture of #'s and $'s and whatever)
def pixels_to_ascii(image):
pixels = image.getdara()
characters = "".join([ASCII_CHARS[pixel//25] for pixel in pixels])
return(characters)
def main(new_width=100):
#opening source image
path = input ("Enter pathname for image:\n")
try:
image = PIL.Image.open(path)
#define the image to make it gray then resize the gray output
new_image_data = pixels_to_ascii(grayify(resize_image(image)))
#formattin'
pixel_count = len(new_image_data)
#'ascii_image' is the head of everything where all grayscales and widths and ratios and whatnot is connected
ascii_image = "\n".join(new_image_data[i:(i new_width)] for i in range(0, pixel_count, new_width))
#printing!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
print(ascii_image)
#save result
with open("ascii_image.txt", "w") as f:
f.write(ascii_image)
#if no pathname found
except:
print(path, "isnt a valid pathname, try another one bro")
main()
CodePudding user response:
First of all there is a wrong space between your input
function and its argument. Secondly you should consider rewriting your try/except
clause as you will run into errors when except
is run due to the missing definition of the image
variable.