Home > Software design >  Black sometimes replaces transparent when using PIL on tkinter
Black sometimes replaces transparent when using PIL on tkinter

Time:10-11

I have been trying to display several images (in png) on tkinter. All of them have a transparent background, but for some reason jailer.png (the handcuffs) has been displaying with a black background (see screenshot), while every other image works as it should. Why ?

Here's my code (if create_a_label_with_an_image is redudant, it's because i had a lot of problem wth garbage disposal, if anyone knows the trick, pls tell me !) :

import tkinter as tk
from tkinter import ttk
from os import walk
from PIL import Image, ImageTk

class Gui:
    def __init__(self):
        self.frames={"main":tk.Tk()} #every frame will be added in this dictionnary
        self.l_widgets={} #all widgets

    def create_notebook(self, name, config_args={}, grid_args={}, frame="main"):
        self.l_widgets[name] = (ttk.Notebook(self.frames[frame], **config_args),0)
        self.l_widgets[name][0].grid(**grid_args)
                 
    def create_a_label_with_an_image(self, name, filename, size=(None, None), config_args={}, grid_args={}, frame="main"):
        image_temp=Image.open(filename)
        if size!=(None,None):
            image_temp.thumbnail(size)
        image=ImageTk.PhotoImage(image_temp)
        self.l_widgets[name] = (ttk.Label(self.frames[frame]),image) #Image is stored at several places to hope it will not get erased
        self.l_widgets[name][0].config(**config_args, image=image)
        self.l_widgets[name][0].image=image
        self.l_widgets[name][0].grid(**grid_args)
            
    def add_to_notebook(self, name, name_frame, config_args={}, grid_args={}):
        self.frames[name_frame]=ttk.Frame(self.frames["main"])
        self.l_widgets[name][0].add(self.frames[name_frame], **config_args)
        self.l_widgets[name][0].grid(**grid_args)
#END ####
g=Gui()    

#here, i get all path to icons from this directory
mypath = "./Wolvesville Notebook/icons/characters"
filenames = next(walk(mypath), (None, None, []))[2]  # [] if no file
if filenames==[]:
    raise Warning("no icons found")
    
g.create_notebook("roles") 
g.add_to_notebook("roles","all", config_args={"text":"all"})
nbr_images=0
for icon in filenames:
    g.create_a_label_with_an_image(icon[:-4], mypath "/" icon,
                                   size=(50,50),
                                   grid_args={"row":nbr_images//12, 
                                              "column":nbr_images%12},
                                   frame=str(pool))
    nbr_images =1
                        


tk.mainloop()

Here's what the final result look like : enter image description here

CodePudding user response:

AKX commented under my question to convert the grayscale alpha into rgb alpha, and it works ! I don't know why it was in grayscale, but it doesn't matter. I will close the subject, thanks !

PS : I can't seem to put a comment as the answer, so I wrote another one.

  • Related