使用 PIL 时,标签上的一些透明图像有黑色背景
Some transparent images on labels have black backgrounds when using PIL
我有一个可以放置多个标签的框架。所有这些标签都包含 png 图像,有些会填满整个背景,有些则不会;有透明背景。我想获得 16x16 像素的图像并将它们的大小调整为 32x32 以提高可见性。我注意到原始图像在调整大小时工作正常,但是当我将新图像添加到资产目录并使用它时,它的背景变成黑色。
我有一个资产文件夹,用于存储我的 png 图像。我确信他们每个人都应该有透明背景,至少根据我的照片编辑器。
这是最少的代码:
from tkinter import *
from PIL import Image, ImageTk
import os
project_path = os.path.dirname(os.path.abspath(__file__))
root = Tk()
button_frame1 = Frame(root)
button_frame1.pack(side=TOP)
Label(button_frame1, text="Hotbar",padx=8).grid(row=0,column=2)
hotbarFrame = Frame(button_frame1, bg="white", width=288, height=32, highlightbackground="black", highlightthickness=1)
hotbarFrame.grid(row=0,column=3,padx=10)
hotbarItems = ["dirt", "grass_block_side", "grass", "stone", "cobblestone", "oak_planks", "oak_log", "oak_leaves2", "glass"]
hotbarImages, hotbarSlots = {}, []
for slot in range(len(hotbarItems)):
item = hotbarItems[slot]
item_file = open(project_path + "\assets\blocks\" + item + ".png", "rb")
hotbarImages[item] = ImageTk.PhotoImage(Image.open(item_file).resize((32,32), Image.NONE))
hotbarSlots.append(Label(hotbarFrame, image=hotbarImages[item], width=32, height=32, highlightthickness=1))
hotbarSlots[slot].grid(row=0,column=slot)
root.mainloop()
输出:
Notice that the eighth icon has a black background, even though it's transparent.
Click here to see the original eighth icon.
你应该使用这个代码
hotbarSlots.append(Label(hotbarFrame, image=hotbarImages[item], width=32, height=32, highlightthickness=1 , bd=None))
或者可以将与 window 背景
的标签颜色相同
root.config(bg="white")
hotbarSlots.append(Label(hotbarFrame, image=hotbarImages[item], width=32,height=32, highlightthickness=1 , background="white"))
问题是你的PNG图片不是RGBA模式,而是P模式。
P mode
8-bit pixels, mapped to any other mode using a color palette
from PIL import Image
im = Image.open("D:/oak_leaves2.png")
print(im.mode) # P
im.show()
下面的简单脚本显示结果
from PIL import Image, ImageTk
import tkinter as tk
root = tk.Tk()
frame = tk.Frame(root, bg='blue')
frame.pack(fill='both', expand=1)
im = Image.open("D:/oak_leaves2.png").resize((80, 80), Image.BICUBIC)
image = ImageTk.PhotoImage(im)
label = tk.Label(frame, text='Hello', image=image)
label.pack()
root.mainloop()
所以,实际上,它看起来像这样
我有一个可以放置多个标签的框架。所有这些标签都包含 png 图像,有些会填满整个背景,有些则不会;有透明背景。我想获得 16x16 像素的图像并将它们的大小调整为 32x32 以提高可见性。我注意到原始图像在调整大小时工作正常,但是当我将新图像添加到资产目录并使用它时,它的背景变成黑色。 我有一个资产文件夹,用于存储我的 png 图像。我确信他们每个人都应该有透明背景,至少根据我的照片编辑器。
这是最少的代码:
from tkinter import *
from PIL import Image, ImageTk
import os
project_path = os.path.dirname(os.path.abspath(__file__))
root = Tk()
button_frame1 = Frame(root)
button_frame1.pack(side=TOP)
Label(button_frame1, text="Hotbar",padx=8).grid(row=0,column=2)
hotbarFrame = Frame(button_frame1, bg="white", width=288, height=32, highlightbackground="black", highlightthickness=1)
hotbarFrame.grid(row=0,column=3,padx=10)
hotbarItems = ["dirt", "grass_block_side", "grass", "stone", "cobblestone", "oak_planks", "oak_log", "oak_leaves2", "glass"]
hotbarImages, hotbarSlots = {}, []
for slot in range(len(hotbarItems)):
item = hotbarItems[slot]
item_file = open(project_path + "\assets\blocks\" + item + ".png", "rb")
hotbarImages[item] = ImageTk.PhotoImage(Image.open(item_file).resize((32,32), Image.NONE))
hotbarSlots.append(Label(hotbarFrame, image=hotbarImages[item], width=32, height=32, highlightthickness=1))
hotbarSlots[slot].grid(row=0,column=slot)
root.mainloop()
输出: Notice that the eighth icon has a black background, even though it's transparent.
Click here to see the original eighth icon.
你应该使用这个代码
hotbarSlots.append(Label(hotbarFrame, image=hotbarImages[item], width=32, height=32, highlightthickness=1 , bd=None))
或者可以将与 window 背景
的标签颜色相同root.config(bg="white")
hotbarSlots.append(Label(hotbarFrame, image=hotbarImages[item], width=32,height=32, highlightthickness=1 , background="white"))
问题是你的PNG图片不是RGBA模式,而是P模式。
P mode 8-bit pixels, mapped to any other mode using a color palette
from PIL import Image
im = Image.open("D:/oak_leaves2.png")
print(im.mode) # P
im.show()
下面的简单脚本显示结果
from PIL import Image, ImageTk
import tkinter as tk
root = tk.Tk()
frame = tk.Frame(root, bg='blue')
frame.pack(fill='both', expand=1)
im = Image.open("D:/oak_leaves2.png").resize((80, 80), Image.BICUBIC)
image = ImageTk.PhotoImage(im)
label = tk.Label(frame, text='Hello', image=image)
label.pack()
root.mainloop()
所以,实际上,它看起来像这样