3

In my simple game I'm creating I currently have placeholder rectangle objects as graphics. I'm trying to replace them with sprites, but as I understand it Tkinter doesn't have support for PNGs or alpha transparency. I am using Python 3.3, which doesn't work with PIL (and since it is a school project, I am solely trying to use Tkinter as the only external library). Is there a way to use the alpha channel with the supported file formats so that I can have multiple layers of tiles? I just want to filter out the white pixels.

Taylor Hill
  • 1,053
  • 1
  • 14
  • 24
  • I just have the feeling that the color #FF00FF could be transparent or maybe you can set a transparent color. By the way I found a Bitmap image that has Mask data - could be it – User Apr 14 '13 at 21:49
  • Do you know how to implement this? – Taylor Hill Apr 14 '13 at 21:50
  • Ah, I remember.. I think you can try to set only the colorful pixels and the other pixels should be transparent. I f that doesn not work, look at Tkinter.BitmapImage. – User Apr 14 '13 at 21:54

3 Answers3

1

I was able to use an image with transparency. I understand your wish to avoid use of PIL, but the following code works and demonstrates that Tkinter will support formats with transparency.

from Tkinter import Tk, Canvas
import PIL
root = Tk() 
tkimg = PIL.ImageTk.PhotoImage('cat1-a.gif')
canvas = Canvas(root, height=600, width=600)
canvas.grid()
def stamp(event):
    canvas.create_image(event.x, event.y, image=tkimg)
canvas.bind('<ButtonPress-1>', stamp)
root.mainloop()

three create_image() calls demonstrate transparency

Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46
Bennett Brown
  • 5,234
  • 1
  • 27
  • 35
  • When having trouble with ImageTk under Linux (no module _imagingtk), refer to this link: http://askubuntu.com/questions/156484/how-do-i-install-python-imaging-library-pil – lyvic Feb 10 '14 at 09:55
  • 1
    Your answer might not quite fit the question but it has relevance and a nice example potentially helping people finding themselves here with a similar question. Add `from Tkinter import Tk, Canvas` and `import PIL` at the top and `root.mainloop()` at the bottom to run. – valid May 01 '17 at 19:51
  • Thanks @valid, I added that to the code for future readers' convenience and clarity. – Bennett Brown May 03 '17 at 03:26
1

To make the white pixels transparent (I am assuming that white means #ffffff) you could use this function below or something like it. This does not require PIL. It has worked for me for pngs, but also will work for gif.

  • First, make a new blank image the same size as your image.
  • Second, copy pixel by pixel to the new image (unless the pixel is white).
  • Set your original image to the new image.

Here is an example of the function being used:

from tkinter import *


def makeTransparent(img, colorToMakeTransparentInHexFormat):
    newPhotoImage = PhotoImage(width=img.width(), height=img.height())
    for x in range(img.width()):
        for y in range(img.height()):
            rgb = '#%02x%02x%02x' % img.get(x, y)
            if rgb != colorToMakeTransparentInHexFormat:
                newPhotoImage.put(rgb, (x, y))
    return newPhotoImage


root = Tk()
mycanvas = Canvas(root, width=200, height=200,bg="orange")
mycanvas.pack()

myphotoImage = PhotoImage(file="whitecar.gif")
#set your image to the image returned by the function
myphotoImage = makeTransparent(myphotoImage, "#ffffff")
canvasImage = mycanvas.create_image(100, 100, image=myphotoImage, anchor=CENTER)

root.mainloop()

Here is an example of a white car with a white background:

car with white background

Here is an example of that car on the canvas using the example program:

car on canvas with orange background

So I hope I have answered your question.

  • I did not use PIL. nothing but the tkinter module.
  • I only used gif, not png as you asked.
  • Wherever white is, will now be transparent.

Note:

For whatever reason, processing transparency multiple times with the above function can result in viewing errors in tkinter. Below is a way to remove multiple colors by using a color switching function: Here is a car:

red car with grey background

Here is another function to switch colors, which can be implemented before making a color transparent.

def switchColors(img, currentColor,futureColor):
    newPhotoImage = PhotoImage(width=img.width(), height=img.height())
    for x in range(img.width()):
        for y in range(img.height()):
            rgb = '#%02x%02x%02x' % img.get(x, y)
            if rgb == currentColor:
                newPhotoImage.put(futureColor, (x, y))
            else:
                newPhotoImage.put(rgb, (x, y))
    return newPhotoImage

Here it is in use

root = Tk()
mycanvas = Canvas(root, width=200, height=200,bg="orange")
mycanvas.pack()

myphotoImage = PhotoImage(file="car.png")

myphotoImage = switchColors(myphotoImage,"#db0000","#ffffff") #switch red to white
myphotoImage = switchColors(myphotoImage,"#d9d9d9","#ffffff") #switch greybackground to white
myphotoImage = switchColors(myphotoImage,"#6d6d6d","#ffffff") #switch windshield grey to white
myphotoImage = makeTransparent(myphotoImage,"#ffffff") #make white transparent

canvasImage = mycanvas.create_image(100, 100, image=myphotoImage, anchor=CENTER)

root.mainloop()

And here is the result of that process:

car with 2 greys and red removed

Here is a reference to a similar problem: How to rotate an image on a canvas without using PIL?

garydavenport73
  • 379
  • 2
  • 8
  • What if the image has some other color in between that has to be made transparent? – Delrius Euphoria Jul 25 '21 at 19:04
  • Hmm, I guess you could run this function a second time. Run it once to remove white for example, then run it again to remove a second color. I don't see why that wouldn't work. – garydavenport73 Jul 25 '21 at 20:43
  • 1
    @CoolCloud. Actually for reasons I haven't yet figured out, it does not wok properly wihen I run it sequentially. However, I was able to remove 2 colors by writing a second function, `switchColors`. I switched red out for white, then made white transparent, effectively removing 2 colors, both red and white. – garydavenport73 Jul 25 '21 at 21:08
0

There is a way to use PIL with Python 3 using non-official versions of PIL Go to http://www.lfd.uci.edu/~gohlke/pythonlibs/ to download it.