Create a gist now

Instantly share code, notes, and snippets.

a short script to grab a random object and then get its Albers rings and build and image.
#!/usr/bin/env python
import glob
import pycurl
import urllib
import simplejson as json
import cStringIO
import Image, ImageDraw
import sys
import os
from random import randint
import urlparse
buf = cStringIO.StringIO()
c = pycurl.Curl()
c.setopt(c.URL, '')
d = {'method':'cooperhewitt.objects.getRandom','access_token':api_token}
c.setopt(c.WRITEFUNCTION, buf.write)
c.setopt(c.POSTFIELDS, urllib.urlencode(d) )
random = json.loads(buf.getvalue())
object_id = random.get('object', [])
object_id = object_id.get('id', [])
print object_id
d = {'method':'cooperhewitt.objects.getAlbers','id':object_id ,'access_token':api_token}
c.setopt(c.POSTFIELDS, urllib.urlencode(d) )
albers = json.loads(buf.getvalue())
rings = albers.get('rings',[])
ring1color = rings[0]['hex_color']
ring2color = rings[1]['hex_color']
ring3color = rings[2]['hex_color']
ring1id = rings[0]['value']
ring2id = rings[1]['value']
ring3id = rings[2]['value']
print ring1color, ring2color, ring3color
# build the image / write it to disk
size = (1000,1000)
im ='RGB', size, ring1color)
draw = ImageDraw.Draw(im)
ring2coordinates = ( randint(50,100), randint(50,100) , randint(900, 950), randint(900,950))
print ring2coordinates
ring3coordinates = ( randint(ring2coordinates[0]+50, ring2coordinates[0]+100) , randint(ring2coordinates[1]+50, ring2coordinates[1]+100) , randint(ring2coordinates[2]-200, ring2coordinates[2]-50) , randint(ring2coordinates[3]-200, ring2coordinates[3]-50) )
print ring3coordinates
draw.rectangle(ring2coordinates, fill=ring2color)
draw.rectangle(ring3coordinates, fill=ring3color)
del draw'file.png', 'PNG')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment