使用PIL在python上重新设置图像后引发AttributeError(name)
我想将图像的大小调整为28*28像素,我正在使用模块使用PIL在python上重新设置图像后引发AttributeError(name),python,Python,我想将图像的大小调整为28*28像素,我正在使用模块PIL来调整此图片的大小: 我正在创建这个类: from PIL import Image import PIL import numpy from resizeimage import resizeimage import scipy.misc ''' This class is to resize input image to MNIST size (28x28 px) ''' class Resize_img: def
PIL
来调整此图片的大小:
我正在创建这个类:
from PIL import Image
import PIL
import numpy
from resizeimage import resizeimage
import scipy.misc
''' This class is to resize input image to MNIST size (28x28 px) '''
class Resize_img:
def __init__(self, imageName):
print 'Image -- ', imageName
self.resized_image = ''
# resize img to mnist size [28x28]
with open(imageName,'r+b') as f:
with Image.open(f) as image:
cover = resizeimage.resize_cover(image, [28, 28])
self.resized_image = 'new ' + imageName
cover.save(self.resized_image, image.format)
# transform img to MNIST form
# image to ndarray
PILimg = PIL.Image.open(self.resized_image)
self.mnist_image_input = scipy.misc.fromimage(PILimg,
True) # True => space gray! ----------------------------------------------------
self.mnist_image_input = (numpy.multiply(self.mnist_image_input,
1.0 / 255.0) - 1.0) * -1.0 # inverse the image :D ( white -> dark )
def main(): # To test this Class.
imageTest = '/home/brm17/Desktop/Myapp/2.jpeg' # The name of the image to resize
imageTest = Resize_img(imageTest)
scipy.misc.imshow(imageTest.mnist_image_input)
if __name__ == "__main__":
main()
# sudo apt-get install libtiff4-dev libjpeg8-dev zlib1g-dev libfreetype6-dev liblcms2-dev libwebp-dev tcl8.5-dev tk8.5-dev python-tk
如果运行此脚本,终端将打印:
Image -- /home/brm17/Desktop/Myapp/2.jpeg
Traceback (most recent call last):
File "Resize_img.py", line 40, in <module>
main()
File "Resize_img.py", line 35, in main
imageTest = Resize_img(imageTest)
File "Resize_img.py", line 18, in __init__
with Image.open(f) as image:
File "/usr/lib/python2.7/dist-packages/PIL/Image.py", line 528, in __getattr__
raise AttributeError(name)
AttributeError: __exit__
Image--/home/brm17/Desktop/Myapp/2.jpeg
回溯(最近一次呼叫最后一次):
文件“Resize_img.py”,第40行,在
main()
文件“Resize_img.py”,第35行,主
imageTest=Resize_img(imageTest)
文件“Resize_img.py”,第18行,在uu init中__
带图像。打开(f)作为图像:
文件“/usr/lib/python2.7/dist packages/PIL/Image.py”,第528行,在__
提升属性错误(名称)
AttributeError:\uuu退出__
问题是什么,如何解决 您似乎在使用
with
语句,而对象没有\uuuu退出\uuuu
方法。只是不要使用with语句:
image = Image.open(f)
cover = ...
似乎您正在使用带有对象的
with
语句,而该对象没有\uuuuuuuu退出方法。只是不要使用with语句:
image = Image.open(f)
cover = ...
对!!
使用图像更改。打开(f)作为图像:
到图像=图像。打开(f)
。
它修好了 是的!
使用图像更改。打开(f)作为图像:
到图像=图像。打开(f)
。
它修好了 错误:`img=img.resize((新大小[0],新大小[1]),Image.LANCZOS)AttributeError:'module'对象没有属性'LANCZOS',`我认为这可能取决于您的PIL版本。请尝试pydoc PIL.Image.Image.resize
查看您的选项。错误:`img=img.resize((新大小[0],新大小[1]),Image.LANCZOS)AttributeError:'module'对象没有属性'LANCZOS',`我想这可能取决于您的PIL版本。尝试pydoc PIL.Image.Image.resize
查看您的选项。