代码之家  ›  专栏  ›  技术社区  ›  Torin M.

更改pil图像的alpha时出错

  •  1
  • Torin M.  · 技术社区  · 8 年前

    我正在尝试使用 PIL.Image 包装与 tkinter . 这是我的密码。

    background_image = Image.open(file_path + "\\static\\backgroundimage.jpg")
    background_image = background_image.putalpha(128)
    background_photo = ImageTk.PhotoImage(background_image)
    background_label = tkinter.Label(dashboard_page, image=background_photo)
    background_label.background_image = background_photo
    background_label.place(x=0, y=0, relwidth=1, relheight=1)
    

    错误发生在第二行。 background_image = background_image.putalpha(128) . 如果我注释掉这一行,程序就不会出错并显示图像。但是,如果取消对此行的注释,则会出现此错误。 AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo' . 发生了什么事,我该怎么处理?谢谢您。我使用的是python 3.4.4。

    1 回复  |  直到 8 年前
        1
  •  1
  •   j_4321    8 年前

    这里的问题是 .putalpha 直接更改图像并返回 None ,不是新图像。因此,要修复代码,只需替换

    background_image = background_image.putalpha(128)
    

    通过

    background_image.putalpha(128)
    

    它应该是有效的。