Friday, July 29, 2011

Robohash

A few days ago, I read about Robohash, a website for creating unique images (of robots) from any text. The author was thinking of using it for icons on a forum, but there are probably other use-cases for this. For example, as a variation of the color password hash to help users avoid typos in their passwords or, perhaps, as a visual representation of a user-submitted "secret code".
USING: images.http kernel sequences urls urls.encoding ;
First, we need to create URLs of the form http://robohash.org/YOUR_TEXT, as instructed:
: robohash-url ( str -- url )
    url-encode "http://robohash.org/" prepend >url ;
Next, we would like to support the different "image sets" that Robohash supports.
: (robohash) ( str type -- image )
    [ robohash-url ] [ "set" set-query-param ] bi*
    load-http-image ;
Using this, we can create image loaders for each set (currently three sets: "set1", "set2", and "set3"):
: robohash1 ( str -- image ) "set1" (robohash) ;

: robohash2 ( str -- image ) "set2" (robohash) ;

: robohash3 ( str -- image ) "set3" (robohash) ;
You can try it out and see that it works:
Robohash also supports custom backgrounds, changing image sizes, and varying image formats (e.g., JPG or BMP). Adding support for that is an exercise to the reader.

The code for this is on my Github.

4 comments:

Anonymous said...

Eh, nice, I didn't know robohash

iamhuzhe said...

error message from "load-http-image":

Generic word length does not define a method for the url class.

mrjbq7 said...

@iamhuzhe: You might be using an older version of Factor. I made a commit six months ago that allows URLs to be used with load-http-image.

You can get this to work on your version by calling "present" before load-http-image to turn it into a string. (Remember to USE: present).

iamhuzhe said...

Hi mrjbq7
I was using the Development release on Windows X86 (2011-02-27). Judging from the date, it does lag behind the Mac Os X version.