Skip to content

Instantly share code, notes, and snippets.

@Stovoy
Created April 27, 2016 11:24
Show Gist options
  • Select an option

  • Save Stovoy/b3b32a26ceffcbdc8733624029cb5c2d to your computer and use it in GitHub Desktop.

Select an option

Save Stovoy/b3b32a26ceffcbdc8733624029cb5c2d to your computer and use it in GitHub Desktop.
from PIL import Image
import requests
import os
from threading import Thread
from Queue import Queue
root = 'http://microsculpture.net/assets/img/tiles'
tiles = 'highres_orchid_%20cuckoo_bee_side_view'
# Another is highres_tortoise_beetle
tiles_dir = 'tiles/{0}'.format(tiles)
if not os.path.exists(tiles_dir):
os.makedirs(tiles_dir)
def download(url, x, y):
path = '{0}/{1}-{2}.jpg'.format(tiles_dir, x, y)
if os.path.exists(path):
# Already downloaded.
return
print 'Downloading {0}, {1}'.format(x, y)
r = requests.get(url, stream=True)
if r.ok:
with open(path, 'wb') as f:
for block in r.iter_content(1024):
f.write(block)
# The following values determined via painful trial and error...
# Values for highres_orchid_%20cuckoo_bee_side_view
min_x = 0
max_x = 118
min_y = 20
max_y = 95
# Values for highres_tortoise_beetle:
# min_x = 30
# max_x = 88
# min_y = 10
# max_y = 100
def download_all():
# Download images concurrently. Wayyyyyyy too slow otherwise.
# Apologies, microsculptures.net, for the traffic rush :)
concurrent = 100
q = Queue(concurrent * 2)
def do_work():
while True:
download(*q.get())
q.task_done()
for i in xrange(concurrent):
t = Thread(target=do_work)
t.daemon = True
t.start()
for x in xrange(min_x, max_x):
for y in xrange(min_y, max_y):
path = '{0}/{1}/x1/7/{2}/{3}.jpg'.format(root, tiles, x, y)
q.put((path, x, y))
print 'Waiting...'
q.join()
def stitch_all():
# Expected width and height. If wrong, this will not work right.
image_width = 256
image_height = 256
stitched_image = Image.new('RGB',
(image_width * (max_x - min_x),
image_height * (max_y - min_y)))
for x in xrange(min_x, max_x):
for y in xrange(min_y, max_y):
tile_image = Image.open('tiles/{0}/{1}-{2}.jpg'.format(tiles, x, y))
print 'Stitching {0}, {1}'.format(x, y)
stitched_image.paste(tile_image,
((x - min_x) * image_width,
(y - min_y) * image_height))
# Uncomment this when doing trial and error cropping, for speed.
# stitched_image.thumbnail((800, 800))
print 'Saving'
stitched_image.save('{0}.jpg'.format(tiles))
download_all()
stitch_all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment