472,345 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,345 software developers and data experts.

Tkinter PhotoImage or PIL transparent subimages

In the new Python game I'm developing, I need to crop out individual
tiles from larger tilesets, and maintain transparency. Unfortunately,
I've run into major deficiencies in both Tkinter and PIL (PyGame,
wxPython, PyQt, etc. are not really suitable for this program, for a
number of reasons, and I have zero interest in discussing why right
now).

Since the Tkinter.PhotoImage.copy() method doesn't allow a region
parameter, I have to save the tile to a disk file (write does have a
region parameter) and read it back in; this does work correctly, but
it's not very fast. Tk's copy() method *does* support a region
parameter, but Tkinter inexplicably does not pass it through. It'd
probably take someone who can program in C and knows the Tkinter library
30 minutes to fix this, check it in for Python 2.3, and add it to the
documentation. HINT HINT. If you want a PEP first, I'll write one, but
this just looks like a bugfix to me.

PIL has a fast crop() method, but it then throws away the transparency
information, making it useless to me. I'm not that familiar with PIL,
but the obvious way to use it does not work, the documentation is
uninformative on the problem, and according to google, people who've
previously asked similar questions have received no answers.

I need answers.

I'd rather have a fixed Tkinter than PIL, since getting users to
install more libraries sucks and I have no need for PIL's other
features, but I'll take what I can get.

Right now, I have the following code, and my images are all
transparent GIFs.

Thanks in advance.

---cut here---
def getTileImagePIL(tile):
key = getTile(tile)
filename, tx, ty = key
tx *= TILESIZE
ty *= TILESIZE
img = IMAGES.get(key)
if not img:
# get master image
bitmap = IMAGES.get(filename)
if not bitmap:
if DEBUG: Log.log('Loading %s' % filename)
bitmap = Image.open(filename)
pagename, width, height = getPagedata(filename)
bitmapsize = bitmap.size
if (width*TILESIZE != bitmapsize[0]
or height*TILESIZE != bitmapsize[1]):
raise AssertionError, 'Image size mismatch: %s' % filename
IMAGES[filename] = bitmap
# get one tile from image
subimage = bitmap.crop( (tx, ty, tx+TILESIZE, ty+TILESIZE) )
img = ImageTk.PhotoImage(subimage)
IMAGES[key] = img
return img

def getTileImageTkinter(tile):
key = getTile(tile)
filename, tx, ty = key
tx *= TILESIZE
ty *= TILESIZE
img = IMAGES.get(key)
if not img:
# get master image
bitmap = IMAGES.get(filename)
if not bitmap:
if DEBUG: Log.log('Loading %s' % filename)
bitmap = Tkinter.PhotoImage(file=filename)
pagename, width, height = getPagedata(filename)
if (width*TILESIZE != bitmap.width()
or height*TILESIZE != bitmap.height()):
raise AssertionError, 'Image size mismatch: %s' % filename
IMAGES[filename] = bitmap
# get one tile from image
try:
bitmap.write('tmp.gif', format='GIF',
from_coords=(tx, ty, tx+TILESIZE, ty+TILESIZE) )
except Tkinter.TclError:
Log.log('TclError, coords %d,%d-%d,%d in %d,%d' % (tx, ty,
tx+TILESIZE, ty+TILESIZE, bitmap.width(), bitmap.height()) )
raise
img = Tkinter.PhotoImage(file='tmp.gif')
IMAGES[key] = img
return img

try:
import Image, ImageTk
PIL = True
getTileImage = getTileImagePIL
except ImportError:
import Tkinter, tkMessageBox
PIL = False
getTileImage = getTileImageTkinter
---cut here---

--
<a href="http://kuoi.asui.uidaho.edu/~kamikaze/"> Mark Hughes </a>
"The computer is incredibly fast, accurate, and stupid.
Man is unbelievably slow, inaccurate, and brilliant.
The marriage of the two is a force beyond calculation." -Leo Cherne
Jul 18 '05 #1
0 4651

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Gregor Lingl | last post by:
Hi all, The Tkinter PhotoImage Widget can be set to "transparent" with the method blank(). Moreover the color of single pixels or definite...
2
by: klappnase | last post by:
Hello everyone, I have seen several threads here on this problem, but still cannot figure out the solution. I want to create a window icon for a...
1
by: midtoad | last post by:
I'm trying to display a GIF image in a label as the central area to a Tkinter GUI. The image does not appear, though a space is made for it. Why...
0
by: erkidevries | last post by:
Problem compiling Tkinter program with bmp images (using py2exe) I have a Tkinter gui program that uses bmp as backgrounds. The program itself...
2
by: vm | last post by:
please help! I can't find anything wrong (except the result ofc ;-) This: picfile = 'logo.gif'
2
by: sj | last post by:
I am just learning to use Tkinter and am having problems displaying image files. I am able to display an image using tutorials (such as...
1
by: nish88 | last post by:
hi to all... i'm working on an animation where i have to make the picture background.gif transparent. l'm not able to do it.can you please help me...
2
by: =?iso-8859-1?q?C=E9dric_Lucantis?= | last post by:
Hi, I can only load gif images with Tkinter.PhotoImage and none with BitmapImage. I tried png, jpg, bmp and xpm and always got this errors : ...
2
by: skanemupp | last post by:
why is the first program not working? when i click the screen the map is not appearing. the second program works. from Tkinter import * ...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.