-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 上时。

4

1 回答 1

0

看起来

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

可能会回来None。这使得new_image设置为None.

这可能就是你得到的原因

“None Type” object has no attribute 'save'

当你这样做new_image.save(<...>)

于 2017-03-29T10:58:37.893 回答