public
Last active

a short script to grab a random object and then get its Albers rings and build and image.

  • Download Gist
albersify.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
#!/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
 
api_token = 'YOUR-COOPER-HEWITT-TOKEN'
buf = cStringIO.StringIO()
c = pycurl.Curl()
c.setopt(c.URL, 'https://api.collection.cooperhewitt.org/rest')
d = {'method':'cooperhewitt.objects.getRandom','access_token':api_token}
 
c.setopt(c.WRITEFUNCTION, buf.write)
 
c.setopt(c.POSTFIELDS, urllib.urlencode(d) )
c.perform()
 
random = json.loads(buf.getvalue())
 
buf.reset()
buf.truncate()
 
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) )
c.perform()
 
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
 
buf.close()
 
# build the image / write it to disk
 
size = (1000,1000)
im = Image.new('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
 
im.save('file.png', 'PNG')

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.