?

Log in

No account? Create an account

Previous Entry | Next Entry

fun little codescriptthing

Some python to get quick and dirty assets for a project I'm working on (to be replaced by paid assets before anyone sees it publicly, of course) ~


objects = [ "ant", "axe", "baby", "bag", "ball", ] # etc...

crop_and_resize_to = (640,480)

import urllib2
import json
import ImageOps
from PIL import Image
from cStringIO import StringIO
for object in objects:
  url = "https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q={}".format(object)
  data = json.loads(str(urllib2.urlopen(url).read()),"utf-8")
  first_image_data = data['responseData']['results'][0]
  url = first_image_data['unescapedUrl']
  image_file = urllib2.urlopen(url)
  im = StringIO(image_file.read())
  image = Image.open(im)
  image = ImageOps.fit(image, (640,480), Image.BICUBIC, 0, (0.5,0.5))
  if image.mode != "RGB":
    #required to save as jpg, if image is single-channel (or otherwise weird)
    image = image.convert("RGB")
  image.save("objects/{}.jpg".format(object));


Grabs the first google-image result for a given word, crop/fits it, saves it as that word. :)

Some of the images are hilarious (well, at least not what I expected). Axe. Turkey. Mouse. Xylophone.

Latest Month

February 2016
S M T W T F S
 123456
78910111213
14151617181920
21222324252627
2829     

Tags

Powered by LiveJournal.com
Designed by chasethestars