python - 更改 PIL 图像的 alpha 时出错
问题描述
我正在尝试使用PIL.Image
包设置图像的 alpha 以及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。
解决方案
这里的问题是.putalpha
直接更改图像并返回None
,而不是新图像。因此,要修复代码,只需替换
background_image = background_image.putalpha(128)
经过
background_image.putalpha(128)
它应该可以工作。
推荐阅读
- c# - 如何从 C# 中的 word 中提取 .emz 文件
- python - Python:xml.Find 总是返回无
- java - selenium中如何使用方法调用内部的各种方法并返回true
- ruby-on-rails - 为什么'serialize'方法会从我在 Rails 应用程序中的表单生成一个空数组?
- excel - Excel行和列选择与合并单元格的方式
- c# - 将带有表情符号的文本保存到文件中成为“?”
- wordpress - 在 Wordpress 中使用带有 ACF 的 scrset 和 Fancybox
- angular - 单击时清除 Angular Material 自动完成功能
- swift - CollectionViewCell 的 IBoutlet 在自定义键盘扩展中为零
- java - 带有继承的 Spring 验证