所以,呃,我是一個新的 Python 學習者,試著制作一個 ASCII 影像轉換器。所以我寫了一個從 youtube 上下來的代碼,它給了我錯誤。我查看了視頻,一切都很好
這是代碼
#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()
這是錯誤
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
所以我想知道出了什么問題以及這些錯誤意味著什么
uj5u.com熱心網友回復:
如果image = PIL.Image.open(path)生成例外,則image永遠不會為該變數賦值。然后,當您嘗試在此處參考它時會出現錯誤:new_image_data = pixels_to_ascii(grayify(resize_image(image))).
您需要更新您的except子句以退出程式或為image.
uj5u.com熱心網友回復:
這意味著 python 正在尋找名為 image 的變數,但沒有找到……你已經在 try 塊中洗掉了 image var ……但是,有些代碼沒有進入 Try 塊……嘗試在下面except移動代碼并將代碼移動到try塊中
#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()
uj5u.com熱心網友回復:
首先,你的input函式和它的引數之間有一個錯誤的空格。其次,您應該考慮重寫您的try/except子句,except因為由于缺少image變數的定義,您將在運行時遇到錯誤。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/517211.html
