【发布时间】:2018-04-11 06:50:43
【问题描述】:
我正在运行以下 python 函数:
def imageprepare(argv):
im = Image.open(argv).convert('L')
width = float(im.size[0])
height = float(im.size[1])
newImage = Image.new('L', (28, 28), (255))
if width > height:
nheight = int(round((20.0/width*height),0))
if (nheigth == 0):
nheigth = 1
img = im.resize((20,nheight), Image.ANTIALIAS).filter(ImageFilter.SHARPEN)
wtop = int(round(((28 - nheight)/2),0))
newImage.paste(img, (4, wtop))
else:
nwidth = int(round((20.0/height*width),0)) #
if (nwidth == 0):
nwidth = 1
img = im.resize((nwidth,20), Image.ANTIALIAS).filter(ImageFilter.SHARPEN)
wleft = int(round(((28 - nwidth)/2),0))
newImage.paste(img, (wleft, 4))
tv = list(newImage.getdata())
tva = [ (255-x)*1.0/255.0 for x in tv]
return tva
它给出了错误:
UnboundLocalError:在赋值之前引用了局部变量“nheigth”。
我在 conda 环境中运行它并使用 python 3.6。
请帮助我。我是 python 新手。
【问题讨论】:
标签: python-3.x conda