Using the tutorial found here: http://www.daniweb.com/code/snippet216550.html, I have begun the graphic portion of my game. I have many .gif battler sprites loaded into variables in my project. However, all of these battlers are right-facing. I need some of them to be flipped to be left-facing to be on the hero's side.

Is there a Tkinter command to flip the images when the program is loaded, or will I have to duplicate every battler sprite and flip them myself?

Recommended Answers

All 5 Replies

Here is an example using PIL ...

# use the Python Image Library (PIL) to load and flip an image
# convert the two PIL images to Tkinter images and display
# tested with Python26  by  vegaseat

import Tkinter as tk
from PIL import Image, ImageTk

root = tk.Tk()
cv = tk.Canvas(root, width=500, height=500)
cv.pack(fill='both', expand='yes')

# pick an image file you have .bmp  .jpg  .gif.  .png
# if not in the working directory, give full path
# open as a PIL image object
pil_image = Image.open("ladybug.gif")
# flip the image around a vertical axis
# for horizontal axis use: Image.FLIP_TOP_BOTTOM
pil_image_flip = pil_image.transpose(Image.FLIP_LEFT_RIGHT)   

# convert PIL image object to Tkinter PhotoImage object
tk_image = ImageTk.PhotoImage(pil_image)
tk_image_flip = ImageTk.PhotoImage(pil_image_flip)

# 'nw' uses upper left corner of the image as anchor
# position first image at canvas coordinates x=30, y=20
cv.create_image(30, 20, image=tk_image, anchor='nw')
# position second image at canvas coordinates x=100, y=20
cv.create_image(100, 20, image=tk_image_flip, anchor='nw')

root.mainloop()

I appreciate your help with my problem. However, I don't seem to have a module PIL. Is this something I will need to download once and use to code the game, or will I have to package the module with my game?

from Tkinter import *
from PIL import Image, ImageTk

root = Tk()
root.title("Battle Window")
canvas = Canvas(width = 500, height = 200, bg = 'grey')

canvas.pack(expand = YES, fill = BOTH)

# load battle sprites and flip them:

# first battler:
battler1 = PhotoImage(file = '.\BattleSprites\mon_014.gif')
# flip the battler:
pil_battler1 = Image.open('.\BattleSprites/mon_014.gif')
flip_battler1 = pil_battler1.transpose(Image.FLIP_LEFT_RIGHT)
battler1_flip = ImageTk.PhotoImage(flip_battler1.tk)

# show the battlers:
canvas.create_image(50, 10, image = battler1, anchor = NW)
canvas.create_image(450, 10, image = battler1_flip, anchor = NE)

root.mainloop()

When testing the above code, I get:

Traceback (most recent call last):
  File "C:\Documents and Settings\Alex\My Documents\Python Projects\RPG-DrawGui.py", line 2, in <module>
    from PIL import Image, ImageTk
ImportError: No module named PIL

As a response from the interpreter.

For those of you who use Python27, the Windows installer
PIL-1.1.7.win32-py2.7.exe
(and other versions) can be downloaded free from:
http://effbot.org/downloads/#pil

PIL works very well with the Tkinter GUI, but can be used for console programs too. A release for Python3 has been promised.

PIL uses image Magic on linux system. Images can be printed out even from the cmd very simple. There will only be a popup window.
eg.

From PIL import Image

im = Image.open("path","r")
im.show()

This simple pseudo will show the image.
;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.