当编码器运行时,我收到以下错误消息:
“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的顶部。
答案 0 :(得分:0)
似乎
make_poster(pict_imgs[x], word_imgs[y])
可以返回None
。这使new_image
设置为None
。
这可能是你获得
的原因“None Type” object has no attribute 'save'
执行new_image.save(<...>)