#!/usr/bin/env python # display a tiled image from tileset with PyQt import sys from PIL import Image from PIL.ImageQt import ImageQt from PyQt4 import QtGui, QtCore from PyQt4.QtGui import QImage from numpy import ndarray # Simple background, will use open from a file in future background = [[12, 2,12, 2, 1, 2, 3, 3, 1, 1], [ 1, 1, 7, 8, 5, 6, 7, 8,12, 3], [ 1, 3, 1, 3,12,10,10, 1,12,12], [ 2,12, 0, 4,10, 3,12, 2,12,12], [12,12, 1, 1,10, 3,12, 2,12, 1], [12,12,12, 0,10, 2, 1,12, 1,12], [ 3,12, 3,12, 0, 2, 2,12,12, 3], [ 1,12, 1,12, 1, 1,12,12, 3,12], [ 3,12, 0,12,12,12,12,12, 3, 3], [12, 3, 1, 2, 3,12,12,12, 1,12]] # This will have the tileset tileset = [] # last time I was writing this save function! def save(): f = open( "map.txt" , "wb" ) f.write( "background : [" ) for i in range(len(background) ): f.write( "[" ) for j in range(len(background[0])): f.write( str(background[j][i]) ) f.write( "," ) if j != len(background[0])-1 else (f.write( "]," ) if i != len(background)-1 else f.write( "]" )) f.write( "\n" ) if i != len(background)-1 else f.write( "]" ) f.close() class MyImage(QtGui.QWidget): def __init__(self, parent, width, height): QtGui.QWidget.__init__(self, parent) BOX_SIZE = 32 image_file = Image.open("simpletile.png") self.setWindowTitle("View tiled background") # get tileset file and split it in images that can be pointed through array if image_file.size[0] % BOX_SIZE == 0 and image_file.size[1] % BOX_SIZE ==0 : currentx = 0 currenty = 0 tilei = 0 while currenty < image_file.size[1]: while currentx < image_file.size[0]: print currentx,",",currenty tileset.append( image_file.crop((currentx,currenty,currentx + BOX_SIZE, currenty + BOX_SIZE)) ) tilei += 1 currentx += BOX_SIZE currenty += BOX_SIZE currentx = 0 # get the background numbers and use to get the tiles for i in range(len(background) ): for j in range(len(background[0])): image = ImageQt( tileset[ background[j][i] ] ) pixmap = QtGui.QPixmap.fromImage(image) image = QtGui.QPixmap(pixmap) label = QtGui.QLabel(self) label.setGeometry(i*BOX_SIZE+10, j*BOX_SIZE+10, BOX_SIZE, BOX_SIZE) label.setPixmap(image) save() app = QtGui.QApplication(sys.argv) width = 320 height = 320 w = MyImage(None, width, height) w.setGeometry(100, 100, width+20, height+20) w.show() app.exec_()