Python 图像显示问题
当我加载带有以下内容的透明图像时:Python 图像显示问题,python,image,tkinter,transparency,python-imaging-library,Python,Image,Tkinter,Transparency,Python Imaging Library,当我加载带有以下内容的透明图像时: def load_image(path): img = Image.open(path) return ImageTk.PhotoImage(img) class Home_Screen(threading.Thread): def __init__(self): threading.Thread.__init__(self) self.home = Tk() self.home.res
def load_image(path):
img = Image.open(path)
return ImageTk.PhotoImage(img)
class Home_Screen(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.home = Tk()
self.home.resizable(width = False,height = False)
self.home.geometry("700x500+300+100")
self.start()
def run(self):
self.images()
Label(self.home, image = self.background).pack() # Put it in the display window
button_instructions = Label(self.home,image = self.b_instructions).place(x = 300,y = 200)
self.home.mainloop()
def images(self):
self.background = load_image("Images/night-sky.jpg")
self.b_instructions = load_image("button_instructions.png")
def start_game(self):
self.home.destroy()
Home = Home_Screen()
我得到一个周围有白色边框的图像。有人知道为什么没有保留原来的透明度吗?如果是这样的话,请提供一个解决方案
使用画布而不是标签小部件。透明度不会丢失,因为您看到的是小部件的背景
def run(self):
img_sky = ImageTk.PhotoImage(file="Images/night-sky.jpg")
img_button = ImageTk.PhotoImage(file="button_instructions.png")
self.canvas = Canvas(self.home, width=700, height=500)
self.canvas.create_image(0, 0, image=img_sky)
self.canvas.create_image(300, 200, image=img_button)
self.canvas.pack()
self.home.mainloop()
只是想确定一下,你确定图像有alpha通道(比如PNG,而不是JPEG)吗?我确定我有一个透明的.PNG图像,我检查了好几次。首先,你必须找出透明度丢失的地方。获取
img
模式以确保其具有alpha通道,然后执行pixels=img.load()
并确保某些像素实际上是透明的。尝试将img
保存到一个新的PNG文件中,并确保它仍然是透明的。如果所有这些都能正常工作,那么问题就出在PhotoImage
上,或者是在您显示它的方式上。但我打赌不是这样。