简体   繁体   中英

image not transparent despite being RGBA PNG format and transparent when opened on preview on mac

Im trying to make a game engine with tkinter but I've encountered a problem. I have an image when opened, clearly shows transparency and, when this code is run:

print(image.mode)

it prints RGBA as the mode. I have googled the necessary elements for transparent images on tkinter and I have all of it. Mode=RGBA format=PNG and yet, it's still not completely transparent:

在此处输入图像描述

and here's the image when opened on preview. It clearly shows transparency:

在此处输入图像描述

So why does it show transparency in preview and other apps(like google docs, slides, etc.) but doesn't show transparency in tkinter?

heres the full code:

from tkinter import *
from PIL import ImageTk, Image

class Create(object):
  def __init__(self, root, img="default.png", width=None, height=None, x=0, y=0):
    self._debug_img = Image.open(img)
    self.img = ImageTk.PhotoImage(Image.open(img))
    
    self.img_wdth, self.img_hgt = self._debug_img.size

    if width == None and height == None:
        self.width = self.img_wdth
        self.height = self.img_hgt

    elif type(width) != int and type(height) != int:
        self.width = self.img_wdth
        self.height = self.img_hgt
    
    elif width != self.img_wdth and height != self.img_hgt:
        self.copy = self._debug_img.resize((self.width, self.height))
        self.img = ImageTk.PhotoImage(Image.open(self.copy))

    self.hitbox = (width, height)
    self.x = x
    self.y = y

    self.root = root

    self.sprite = Label(self.root, image=self.img)
    self.sprite.place(x=self.x, y=self.y, anchor=CENTER)

  def update_pos(self):
    self.sprite.place(x=self.x, y=self.y)

  def update_sprite(self):
    self.copy = ImageTk.PhotoImage(Image.open(Image.open(self.img).resize(self.width, self.height)))
    self.sprite = Label(self.root, image=self.copy)

  def update_hitbox(self):
    self.hitbox = (self.width, self.height)

Steps to solve: Make sure your Image is indeed a PNG and RGBA , Then you must put your image inside a tkinter canvas canvas.create_image(150, 150, image=photoimage) Here is a modified code and its result from How do I make Tkinter support PNG transparency? for Python 3

from tkinter import Tk, Frame, Canvas
from PIL import ImageTk, Image



from PIL import Image
im = Image.open("default.png")

if im.mode in ["RGBA","P"]:


    #Displaying:
    t = Tk()
    t.title("Transparency")

    frame = Frame(t)
    frame.pack()

    canvas = Canvas(frame, bg="black", width=500, height=500, bd=0, highlightthickness=0)
    canvas.pack()

    photoimage = ImageTk.PhotoImage(file="default.png")
    canvas.create_image(150, 150, image=photoimage)#,alpha=0.5)#,bg="black")

    t.mainloop()
else:
    print("Can't display transparency since image mode is ",im.mode)
    input()

这个例子

Wish you best!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM