[ create a new paste ] login | about

Link: http://codepad.org/BnqRZXFG    [ raw code | fork ]

Python, pasted on Oct 7:
#!/usr/bin/env python

import time, urllib2, Image, os, random


def get_filename(x, y):
	# this is how are local files are going to look
	return "img_X%s_Y%s.jpg" % ( str(y).zfill(3), str(x).zfill(3) )


def download_images(path, width, height, zoom):
	""""download all of the images into the directory 'path'"""
	
	# make our way throught the tiles, line by line
	for y in range(height):
		for x in range(width):
			
			filename = os.path.join( path, get_filename(x, y) )
			
			# no point in downloading it if we already have that file
			if os.path.isfile(filename):
				print '%s exists, skipping' % filename
				continue
			
			# The javascript app gets all it's images from this url format
			url = "http://realis.mc/brunier/images/cielhd.jpg/getTile?zoom=%f&x=%d&y=%d" % (zoom, x, y)
			print 'downloading %s' % url
			
			# Using urllib2 as it lets up change the useragent
			request = urllib2.Request(url)
			opener = urllib2.build_opener()
			
			# yep of course we're Mozilla running in Windows, now give us the file :)
			useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; ja; rv:1.9.1b2) Gecko/20081201 Firefox/3.1b2"
			request.add_header('User-Agent', useragent)
			
			img_data = opener.open(request).read() 
			
			#except urllib2.URLError:
			
			# slap it into a file
			f = open(filename, "wb")
			f.write(img_data)
			f.close()
			
			# wait for a slightly random time to make the whole gafafwisp wook a little less suspicious
			time.sleep( float( random.randint(1, 20) ) / 10 + 3 )


def stitch_images(path, width, height):
	"""use the great PIL to wack all or downloaded files into one great big one"""
	
	# open up the first tile
	first_tile = Image.open( os.path.join( path, get_filename(0, 0) ) )
	# we assume that all the tiles are the same size and base our calculations on this
	tile_width, tile_height = first_tile.size
	
	# Work out how big the whole image will be
	canvas_width = tile_width * width
	canvas_height = tile_height * height
	
	# Make an image object to slap everything else into
	canvas = Image.new( 'RGBA', (canvas_width, canvas_height) )
	
	# ok so now we are going to use insane amounts of RAM....but it works and it was very, very easy
	
	for y in range(height):
		for x in range(width):
			print 'stitching %d %d' % (x, y)
			filename = os.path.join( path, get_filename(x, y) )
			if not os.path.isfile(filename):
				print "file %s not found!" % filename
				break
			
			tile = Image.open(filename)
			left, top = (x * tile_width, y * tile_width)
			
			# strap this tile onto the canvas
			canvas.paste( tile, (left, top, left + tile_width, top + tile_height) )
			del tile
			time.sleep(0.01)
	
	#canvas.show()
	canvas.save( os.path.join(path, 'merged_result.jpg') )



if __name__ == '__main__':
	#note width & height should be the number of tiles, not the value of the last tile.
	
	download_images('Z0.1_7x15', width=16, height=8, zoom=0.1)
	stitch_images('Z0.1_7x15', width=16, height=8)


Create a new paste based on this one


Comments: