2016-12-10 10 views
0

とのpythonに画像をreseizた後、私は28 * 28ピクセルに画像2.jpegのサイズを変更したいと私はこの絵のサイズを変更するためのモジュールPILを使用していますよ: 私は、このクラスを作成しています:レイズはAttributeError(名)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__ 

は何の問題であり、どのようにこれを解決するために?

答えて

2

メソッドがないオブジェクトでwithステートメントを使用しているようです。 with文を使用しないでください:

image = Image.open(f) 
cover = ... 
+0

エラー: 'IMG = img.resize((Image.LANCZOS、[1])new_size、[0] new_size) はAttributeError: 'モジュール' オブジェクト「はない属性を持っていないLANCZOS'' – DrTestax

+1

私はそれを考えますPILのバージョンによって異なる場合があります。あなたのオプションを見るには 'pydoc PIL.Image.Image.resize'を試してください。 – Gribouillis

+0

問題を解決したい – DrTestax

1

はい! with Image.open(f) as image:image = Image.open(f)に変更してください。 修正済みです。

関連する問題