Monday, April 30, 2012

Create tile image for your game using python script

What is tile image?
Tile image is a method of storing a sequence of images placed in a single image file.
These images are then processed according to user needs.
Here's an example below:
How we can create these images?
We can use graphics editing software to create them separately.
I used Blender 3D to create separate images.
A tutorial how to do this can be found here on section Blender 3D.
After I rendered images separately and named: 0000.png , 0001.png , 0002.png , 0003.png
I created a python script to put in an tile image, see below:
import os
import PIL
from PIL import Image
from PIL import ImageDraw
o=Image.new("RGBA",(192,48))
d= ImageDraw.Draw(o)
for pic in range(0,4):
        strpic=str(pic)
        filnam="000"+strpic+".png"
        x=pic*48
        img=Image.open(filnam)
        o.paste(img,(0+x,0))
        o.save("out.png")
The script reads the image files of size 48 pixels and puts them into one image called out.png

1 comment:

Rommel Anatoli Quintanilla Cruz said...

Hey, thank you was helpfully, here is my modification :D ...

Just in doubt (tabbing), the paste bin:
http://pastebin.com/3LF592mp

# usage: createTiledImg.py [-h] -n NAMEFILES [NAMEFILES ...] -o OUTPUTFILE
#
# optional arguments:
# -h, --help show this help message and exit
# -n NAMEFILES [NAMEFILES ...], --namefiles NAMEFILES [NAMEFILES ...]
# List of namefiles of images
# -o OUTPUTFILE, --outputfile OUTPUTFILE
# Name of output file

import os
import PIL
from PIL import Image
from PIL import ImageDraw
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('-n','--namefiles',help='List of namefiles of images',nargs='+',required=True)
parser.add_argument('-o','--outputfile',help='Name of output file',nargs=1,required=True)
args = parser.parse_args()

imgs = args.namefiles #list of namefiles

im = Image.open(imgs[0])
w = im.size[0] #width
h = im.size[1] #height

o = Image.new("RGBA",(w*len(imgs),h))
d = ImageDraw.Draw(o)

for c in range(0,len(imgs)):
x = c*w
img = Image.open(imgs[c])
o.paste(img,(0+x,0))
o.save(args.outputfile[0])

Post a Comment