【问题标题】:How can I overlay images with transparencies over a background (which is an image, too) using Python, tkinter, and PIL如何使用 Python、tkinter 和 PIL 在背景(也是图像)上覆盖带有透明度的图像
【发布时间】:2016-05-03 23:03:16
【问题描述】:

如果应该放在上层的图片有透明的,但是它们显示为灰色方块而不是所述的透明,覆盖了背景。

我正在使用 Tkinter 和 PIL 用 Python 编写弹球游戏。我被卡住了,因为我需要在背景上显示游戏的元素(脚蹼、保险杠等),但是,当我尝试覆盖它们时,它们会显示为灰色方块,而不是它们已经具有的透明度。

这是代码:

import sys
from tkinter import *
from PIL import Image, ImageTk


#Secci�³n funciones
def botones():
    juego_Nuevo_Boton = Button( menu_Principal, text = ("Juego nuevo"), command = juegoNuevo, font = ("Arial Black", 10), width = 20).place(x = 15, y = 125 )
    continuar_Juego_Boton = Button( menu_Principal, text = "Continuar juego",  font = ("Arial Black", 10), width = 20) .place(x = 15, y = 160)
    cerrar_El_Juego = Button( menu_Principal, text = "Cerrar el juego", command = menu_Principal.destroy, font = ("Arial Black", 10), width = 20).place(x = 15, y = 195 )

def juegoNuevo():
    ventana_Juego_Nuevo = Toplevel()
    ventana_Juego_Nuevo.geometry("600x700")
    ventana_Juego_Nuevo.title("Nueva partida")
    imagen_Fondo_Juego_Nuevo = PhotoImage( file = "fondo_Juego.gif" )
    fondo_Juego_Nuevo = Label(ventana_Juego_Nuevo, image=imagen_Fondo_Juego_Nuevo)
    fondo_Juego_Nuevo.place( x = 0, y = 0, relwidth = 1, relheight = 1 )
    fondo_Juego_Nuevo.image = imagen_Fondo_Juego_Nuevo
    imagen_lanzador_Canal = PhotoImage( file = "LanzadorCanal.gif" )
    Lanzador_Canal = Label(ventana_Juego_Nuevo, image = imagen_lanzador_Canal)
    Lanzador_Canal.place( x = 0, y = 0 )
    Lanzador_Canal.image = imagen_lanzador_Canal
    ############################################
    #I ALSO TRIED THIS BUT IT SHOWS UP SOME KIND OF IMAGE EDITOR CALLED "IMAGE MAGIC",
    #I don't need to edit the pictures, i just need them overlayed, so i can finish the game.

    #fondo_Del_Juego = Image.open("fondo_Juego.png")
    #lanzador_Canal = Image.open("LanzadorCanal.png")
    #fondo_Del_Juego.paste(lanzador_Canal, (0, 0), lanzador_Canal)
    #fondo_Del_Juego.show()
    #############################################
    ventana_Juego_Nuevo.mainloop()
#Crear pantalla y titulo

menu_Principal =  Tk()
menu_Principal.title("Pinball - Proyecto : creado por Daniel Bonilla")
menu_Principal.geometry("200x400")
imagen_Fondo_Menu = PhotoImage(file = "MenuPrincipal.gif")
fondo_Menu = Label(menu_Principal, image = imagen_Fondo_Menu)
fondo_Menu.place( x = 0, y = 0, relwidth = 1, relheight = 1 )
botones()

#Llamado a la ventana
menu_Principal.mainloop()

特别注意“JuegoNuevo()”函数,问题出在哪里。

所以,差不多,这张图总结了我的问题:

*弹簧已经是透明的了,只是没有那么显眼。

请记住,稍后,我需要放置所有其他元素、碰撞等......所以,我认为“图像魔术”的东西(见上面的代码)是行不通的。

*编辑:我已经尝试过第一个答案中提到的 PNG 文件,但问题仍然存在。

【问题讨论】:

    标签: python image tkinter python-imaging-library


    【解决方案1】:

    使用png 而不是gif 文件将解决您的问题。同时删除未使用的包:

     from PIL import Image, ImageTk
    

    【讨论】:

    • 我已经使用过 PNG、GIF、JPG、ICO,所有这些格式都是如此:')。
    • 我用我的 png 文件尝试了你的代码,它工作正常:D
    • 好的,我试试看,可能是因为我把这些图片都从JPG转换成上述格式,包括PNG;我会尝试使用“自然制作”的 png 图片。
    • @DanielBonillaJaramillo 当您从 JPG 转换时,您很可能会丢失透明度信息。您可以使用像 GIMP 这样的免费程序来添加新的透明度通道。可能是白色,但如果您希望图像中的白色不透明,它可以是另一种颜色。
    猜你喜欢
    • 1970-01-01
    • 2013-02-25
    • 2018-10-02
    • 2012-05-13
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多