quasi random (kaolinfire) wrote,
quasi random

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")

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.
Tags: code, programming, python

  • all I want for my birthday

    Thank you for the slew of birthday wishes, already. I'm up earlier than I wanted due to, well, things, but that just means more birthday to enjoy,…

  • On my birthday (meme)

    Go to wikipedia; Enter your birth day/month. List three events/births/deaths that "speak to you". EVENTS 1895 - At the Swedish-Norwegian Club in…

  • feedback loops

    Ah, feedback loops. I was kind of out of sorts, yesterday, and for some reason had a lot of diet coke (to try to feel better, though I "knew" it…

  • Post a new comment


    default userpic

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.