Home > Software engineering >  Some transparent images on labels have black backgrounds when using PIL
Some transparent images on labels have black backgrounds when using PIL

Time:12-30

I have a frame where I put multiple labels on. All of these labels contain png images, some that fill the whole background, and others that don't; have transparent backgrounds. I want to get 16x16 px images and resize them to 32x32 for visibility. I noticed that the original images work fine when resized, but when I add a new image to the assets directory and use it, its background turns black. I have an assets folder where I store my png images. I am sure that every one of them should have transparent backgrounds, atleast according to my photo editor.

This is the minimal code:

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()

Output: enter image description here

  • Related