将图片分配给对象列表

时间:2015-11-25 15:08:09

标签: python python-2.7 tkinter python-imaging-library

我正在尝试创建一个生成对象列表的程序,然后分配一个"肖像"对于每个对象。

现在这是我的代码到目前为止:

import random
from Tkinter import *
import ttk
from characters import *
from PIL import Image, ImageTk

characters_creation()

eyeslist = ["faces/eyes1.gif", "faces/eyes2.gif", "faces/eyes3.gif", 
"faces/eyes4.gif", "faces/eyes5.gif", "faces/eyes6.gif", 
"faces/eyes7.gif", "faces/eyes8.gif", "faces/eyes9.gif", 
"faces/eyes10.gif", "faces/eyes11.gif"]

eblist = ["faces/eb1.gif", "faces/eb2.gif", "faces/eb3.gif", 
"faces/eb4.gif", "faces/eb5.gif"]

noselist = ["faces/nose1.gif", "faces/nose2.gif", "faces/nose3.gif", 
"faces/nose4.gif", "faces/nose5.gif", "faces/nose6.gif", 
"faces/nose7.gif"]

mouthlist = ["faces/mouth1.gif", "faces/mouth2.gif", "faces/mouth3.gif", 
"faces/mouth4.gif", "faces/mouth5.gif", "faces/mouth6.gif", 
"faces/mouth7.gif", "faces/mouth8.gif", "faces/mouth9.gif", 
"faces/mouth10.gif", "faces/mouth11.gif"]

fhlist = ["faces/fh1.gif", "faces/fh2.gif", "faces/fh3.gif", 
"faces/fh4.gif", "faces/fh5.gif", "faces/fh6.gif", 
"faces/fh7.gif", "faces/fh8.gif", "faces/fh9.gif", 
"faces/fh10.gif"]

mhlist = ["faces/mh1.gif", "faces/mh2.gif", "faces/mh3.gif", 
"faces/mh4.gif", "faces/mh5.gif", "faces/mh6.gif", 
"faces/mh7.gif", "faces/mh8.gif", "faces/mh9.gif", 
"faces/mh10.gif"]

root = Tk()

def RGBAImage(path):
    return Image.open(path).convert("RGBA")


for character in characters:

    face = RGBAImage("faces/face.gif")
    eb = RGBAImage(random.choice(eblist))
    eyes = RGBAImage(random.choice(eyeslist))
    nose = RGBAImage(random.choice(noselist))
    mouth = RGBAImage(random.choice(mouthlist))

    if character.sexe == "Female":
        hair = RGBAImage(random.choice(fhlist))
    else:
        hair = RGBAImage(random.choice(mhlist))


    face.paste(eyes, (0,0), eyes)
    face.paste(eb, (0,0), eb)
    face.paste(nose, (0,0), nose)
    face.paste(mouth, (0,0), mouth)
    face.paste(hair, (0,0), hair)

    facepic = ImageTk.PhotoImage(face)
    character.portrait.append(facepic)

for character in characters:

    cportrait = Label(image=character.portrait)
    cportrait.pack()


root.mainloop()

所以每张肖像都是由不同的元素(眼睛,鼻子等......)组成的。

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

Traceback (most recent call last):   File
"C:\Users\Patrick\Pictures\Kerrell\Kerrell Python\faces_generator.py",
line 67, in <module>
    cportrait = Label(image=character.portrait)   File "C:\Python27\lib\lib-tk\Tkinter.py", line 2591, in __init__
    Widget.__init__(self, master, 'label', cnf, kw)   File "C:\Python27\lib\lib-tk\Tkinter.py", line 2090, in __init__
    (widgetName, self._w) + extra + self._options(cnf)) TclError: image "[<PIL.ImageTk.PhotoImage instance at 0x027E6080>]" doesn't
exist

我不明白的是,如果我运行打印功能来检查我的角色肖像列表中是否有某些内容......

1 个答案:

答案 0 :(得分:0)

character.portrait.append(facepic)
#...
cportrait = Label(image=character.portrait)

继续append,我猜portrait是一个列表。您无法列出标签的image属性。

更改portrait,使其不是列表:

character.portrait = facepic

或者从portrait列表中选择标签应包含的图片:

cportrait = Label(image=character.portrait[0]) #or whatever index

或显示所有这些:

for character in characters:
    for img in character.portrait:
        Label(image=img).pack()