Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当编码器运行时,会出现一个错误,说明它是;“无类型”;对象没有属性';保存';_Python_Pasting - Fatal编程技术网

Python 当编码器运行时,会出现一个错误,说明它是;“无类型”;对象没有属性';保存';

Python 当编码器运行时,会出现一个错误,说明它是;“无类型”;对象没有属性';保存';,python,pasting,Python,Pasting,当编码器运行时,我收到以下错误消息: “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 de

当编码器运行时,我收到以下错误消息:

“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作为一个值,也不会保存它,但是如果我取出该部分并要求它保存新图像,则不会出现错误

问题在于当我将图像粘贴到新图像的顶部时。

似乎

make_poster(pict_imgs[x], word_imgs[y])
可能返回
None
。这使得
new_image
设置为
None

这可能就是为什么你会

“None Type” object has no attribute 'save'

当您提交这样的问题时,请将您的代码减少到可能导致错误的最短片段。如果可能的话,使代码自包含,以便其他人可以自己运行代码。最后,始终提供实际的错误回溯,而不仅仅是您对其中最重要部分的解释。#保存更改后的图像,使用PNG保留透明度113 new_image_filename=os.path.join(new_directory,filename+str(number)+'.PNG')-->114 poop_image.save(new_image_filename)115 number+=1 116 AttributeError:“非类型”对象没有属性“保存”AttributeError:“非类型”对象没有属性“保存”