2017-03-28 1 views
-1

コーダを実行すると、私は次のエラーメッセージを取得する場合:は、それが「なしタイプ」オブジェクトは「保存」何の属性を持っていないことを言って表示されます

“None Type” object has no attribute 'save' 

次のようにスクリプトは次のとおりです。

#Image.blend(image1, image2, alpha) 

from PIL import Image 
import PIL 
import matplotlib.pyplot as plt # single use of plt is commented out 
import os.path 
import PIL.ImageDraw 

def get_images(directory=None): 
    """ Returns PIL.Image objects for all the images in directory. 

    If directory is not specified, uses current directory. 
     Returns a 2-tuple containing 
    a list with a PIL.Image object for each image file in root_directory,  and 
    a list with a string filename for each image file in root_directory 
    """ 

    if directory == None: 
     directory = os.getcwd() # Use working directory if unspecified 

    word_images = [] # Initialize aggregaotrs 
    pic_list = [] 
    picture= [] 
    words= [] 

    directory_list = os.listdir('C:\\Users\\claudiahurtado\\Documents\\Python Programs\\Project') #  Get list of files 
    for entry in directory_list: 
     absolute_filename = os.path.join('C:\\Users\\claudiahurtado\\Documents\\Python Programs\\Project', entry) 
     #print(absolute_filename) 
     try: 
      image = PIL.Image.open(absolute_filename) 
      picture += [entry] # i am creating diffrent files one that contains the names of the files and the other that is the pictures 
      pic_list += [image] 
     except IOError: 
      pass 

    directory_list2 = os.listdir('C:\\Users\\claudiahurtado\\Documents\\Python Programs\\1.4.5 Images') # Get list of files 
    for entry in directory_list2: 
     absolute_filename2 = os.path.join('C:\\Users\\claudiahurtado\\Documents\\Python Programs\\1.4.5 Images', entry) 
     try: 
      image = PIL.Image.open(absolute_filename2) 
      words += [entry] # i am creating diffrent files one that contains the names of the files and the other that is the pictures 
      word_images += [image]   
     except IOError: 
      pass # do nothing with errors tying to open non-images 

    return word_images, pic_list, words, picture #returns 3 lists, word_list and pic_list are lists containing the pictures 
     #words contains the file names of the list of pictures that are transparent, pictures is a list of the names of the other pictures 

def make_poster(img1,img2): 
    width, hight= img1.size 
    img2.resize((width,hight)) 
    img1.convert('RGBA') 
    img2.convert('RGBA') 
    new_image= Image.blend(img1, img2.resize((width,hight)), alpha=.5) 
    new_image.show() 
    return new_image 

def make_all_poster(directory=None): 
    if directory == None: 
     directory = os.getcwd() # Use working directory if unspecified 

    # Create a new directory 'modified' 
    new_directory = os.path.join(directory, 'modified') 
    try: 
     os.mkdir(new_directory) 
    except OSError: 
     pass 
    word_imgs, pict_imgs, word_filenames, picture_filenames = get_images() 
      number=0 
    logo_img = PIL.Image.open('aclu.jpg') 
    logo_w, logo_h = logo_img.size 
    for x in range(len(pict_imgs)): 
     for y in range(len(word_imgs)): 
     # Parse the filenamefor n in range(len(image_list)): 
     # Parse the filename 
      filename, filetype = picture_filenames[x].split('.') 

      new_image= make_poster(pict_imgs[x], word_imgs[y]) 
      w,h = new_image.size 

      new_image_filename = os.path.join(new_directory, filename + str(number)+ '.png') 
      new_image.save(new_image_filename) 
      number+=1 

エラーは、新しいimgが作成された場所に由来します。これは、コードが機能しなくなり、エラーを返すようになったときです。何らかの理由で、新しいimgを値として取っておらず、保存しませんが、私がその部分を取り出して新しいイメージを保存するように頼むとエラーはありません。

問題は、new_imageの画像のontopを貼り付けるときにあります。

+1

new_image.save(<...>)を行うと思われる

は、なぜあなたは

“None Type” object has no attribute 'save' 

を取得し、エラーになりますことができ、最短の断片にコードを削減してください。可能であれば、コードを自己完結型にして、他の人がコードを実行できるようにしてください。最後に、最も重要な部分の解釈だけでなく、実際のエラートレースバックを常に提供してください。 –

+0

#変更されたイメージを保存して、透過性を維持するためにPNGを使用します。 113 new_image_filename = os.path.join(new_directory、filename + str(number)+ '.png') - > 114 poop_image.save(new_image_filename) 115 number + = 1 AttributeError: 'NoneType'オブジェクトに 'save'属性がありません –

+0

AttributeError: 'NoneType'オブジェクトには 'save'属性がありません。 –

答えて

0

make_poster(pict_imgs[x], word_imgs[y]) 

Noneを返すことが表示されます。 new_imageNoneに設定します。このような質問を提出するときは、

関連する問題