The script is untested. It will change in the furure. Use at your own risk. Do not redistribute without prior permission of TowerOfCreation.com team (contact: info (at) towerofcreation (dot) com )

Back to the webpage
import Image, sys, os, math

 
def openFiles () :
	'''Load files from user input'''
	print "Running\n"
	filesInput = []

 
	if len(sys.argv) > 1:
		for param in sys.argv[1:]:
			filesInput.append(param)

	else:
		filesInput = ["test.png"]
	try:
		source = []
		i = 0
		for infile in filesInput[0:]:
			source.append ( (Image.open(infile), infile) )

			print "File %s found" % infile + ", ", source[i][0].format, "%dx%d" % source[i][0].size, source[i][0].mode

			i = i +1
	except IOError:
		print "Unable to read file"
	return source

 
def splitImages () :
	'''Send idividual images to split function'''
	imagesToSplit = openFiles ()
	print imagesToSplit
	for tuple in imagesToSplit:
		filename = tuple[1]

		#Clear filename for Fallouty-like names
		filename.lower()
		replace = ("a", "e", "i","o", "u", "y", "_", "-", "?")

		filename = filename.replace(".png", "")
		for char in replace:
			print char
			filename = filename.replace(char, "")

		filename = filename [0:6]
		print filename
		if os.access(filename, os.F_OK) != True:
			try:
				os.mkdir (filename)

			except IOError:
				print IOError
		splitSingle (tuple[0], filename)
	#saveFile (imagesToSplit[0], "blabla", path)

 
def saveFile (image, name="converted.png", path = "./test"):
	#im.save(outfile, format, options...)
	print "Saving file %s at %s" % (name, path)

	try:
		image.save (path + "/" + name + ".png", "png")
	except IOError:
		print "Unable to save converted file"

 
def splitSingle (source, filename):
	'''Does the splitting'''
	sourceSize = (source.size)  #Size of source image

	fBlockSize = (32, 16) #Size of single Fallout hex in px
	hexesInBlock = [0,0]  #Number of blocks - hexes - for one tile
	currentRowCell = [1,1] #Row + cell when splitting

	posHexesInBlock = [ [ 3, 4, 5, 6, 7], [ 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] ] #Possible numbers of hexes in generated tiles, [x values, y values]

	tileSize = [0,0] #tile size in px
	margin = (32,16) #Maximum margin for calculating size of tile in hexes
 

 
	print "Source image x resolution:\t%s" % sourceSize[0]
	print "Source image y resolution:\t%s" % sourceSize[1]

 
	#Let's get the number which need small resizing, bigger ones preferred, some approximation
	for i in range (2):
		print i
		for curBlock in posHexesInBlock[i][0:]:
			if (sourceSize[i] % (curBlock*fBlockSize[i]) <= (sourceSize[i] % fBlockSize[i] + margin[i]) ): #66 is just for 3*32 (3 hexes width)

				hexesInBlock [i]  = curBlock
 
	if (hexesInBlock[0] < 1 or hexesInBlock[1] < 1):
		print "Error: Unable to calculate number of tiles"

		sys.exit()
 
	print hexesInBlock
 
	for i in range (2):
		tileSize[i] = hexesInBlock[i]*fBlockSize[i]

 
	#Now we have the best number of blocks for minimal resizing, so let's count it
	#while ( (sourceSize[i] + resize[i] )%fBlockSize[i]
	resize = [0,0]
	for axis in range (2):
		print axis
		while (    (      (  (sourceSize[axis] + resize[axis])  / fBlockSize[axis]  )     % hexesInBlock[axis]) != 0):
			resize [axis] = resize [axis] + 1

	print resize
 
	#Now we have number of tiles and number of pixels for resizing the image.
	#Create destination image
	tmp = source
	source = Image.new("RGBA",  (sourceSize[0] + resize[0], sourceSize[1] + resize[1]) )

 
	source.paste(tmp, (0, resize[1]) ) #This one is interesting - I want the picture at lowe left corner, not upper left corner, hence the shift
 
	print source.size

 
	#For each row
	while (currentRowCell [1] * tileSize[1] <= sourceSize[1] + tileSize[1] ):
		#All cells in one row

		while (currentRowCell [0] * tileSize[0] <= sourceSize[0] + tileSize[0]  ):  #the extra minus one is to make last tile overleap actual source image

			top = [ (currentRowCell[0] - 1) * tileSize[0],  source.size[1] - currentRowCell[1] * tileSize[0] ]  #I want to calculate from the bottom to the top

			bottom = [currentRowCell[0] * tileSize[0],  source.size[1] - (currentRowCell[1] - 1) * tileSize[0] ]

 
			currentImg = source.crop( (top[0], top[1], bottom[0], bottom[1]) )
			saveFile (currentImg, (filename + "%d%d" %  ((currentRowCell [1] - 1), (currentRowCell [0] - 1)) ) , filename)  #just the naming convention, to keep the first one as image00.png, then image01.png,...

			currentRowCell[0]  = currentRowCell[0] + 1
		currentRowCell[1]  = currentRowCell[1] + 1

		currentRowCell[0] = 1 #Get to the first cell
 
splitImages ()
Back to the webpage