-2

I am making a tkinter project for school. In the project, I need to use transparent images. In my school, the tkinter only supports GIFs so I cannot use a PNG. Moreover, only the modules that come with python 3 when installing are supported. We cannot download more. Is there a way to solve my problem?

Anonymous Coder
  • 512
  • 1
  • 6
  • 22
  • 1
    GIFs support transparency and tkinter can read GIFs, so what's the problem? – Aran-Fey Mar 17 '18 at 11:12
  • The alternative to PNG would be SVG: [HERE IS LINK ](https://stackoverflow.com/questions/22583035/can-you-display-an-image-inside-of-a-python-program-without-using-pygame) – tareq Mar 17 '18 at 11:13
  • 1
    GIF can only use binary level transparency (i.e. one color in the palette is defined as transparent). Most PNG images/icons found on the web nowadays use full alpha channel (i.e. 256 levels of transparency). When converting such images to GIF, the result is usually not very aesthetic. Maybe you could ask the system administrator from your school to install Python 3.6 which supports PNG in tkinter. It should only take him 5 minutes to install an up-to-date release of Python. – sciroccorics Mar 17 '18 at 12:06

2 Answers2

2

tkinter can show the png transparence.

Example with a basic image.

test

import tkinter as tk

root = tk.Tk()

img = tk.PhotoImage(file='test.png')

can = tk.Canvas(root, width=300, height=300, bg='lightGrey')
can.grid()

can.create_rectangle(0, 0, 200, 200, fill='darkGreen')
can.create_rectangle(100, 100, 300, 300, fill='navy')
can.create_text(150, 150, text='TKINTER', font=('', 32), fill='orange')
can.create_image(150, 150, image=img)

root.mainloop()

result

You just need to save your image with the alpha parametre, with Gimp or a good image editor.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
brodeur
  • 96
  • 5
0

You can do this with GIFsicle, using the following options:

gifsicle -U --disposal=previous --transparent="#ffffff" -O2 pic.gif > pic_trans.gif

where pic.gif and pic_trans.gif are the source and destination file names, and #ffffff is the hex code of the color you want to make transparent (here, pure white).

scenox
  • 698
  • 7
  • 17