Using RMagick with Flickr
Posted by Corban Brook Mon, 23 Oct 2006 21:14:00 GMT
In my previous article, Render Great-looking Collages with Ruby and RMagick, I showed how you can use RMagick to generate a lovely collage from a selection of random images. This tutorial will take it one step further and show you how to use RMagick with RFlickr to generate a collage from your own Flickr account or even a results from a flickr search.

4 random images found with the search term “Mazda MX-3” (my car)
Preface
This tutorial will show some of the basic features of the rflickr library, including:
- authorizing your application with Flickr
- getting a list of image urls from one of your own photo sets.
- getting a list of image urls returned from a search of other peoples photo sets.
After authorizing the application with Flickr and obtaining a list of image URLs we will then load the images into RMagick with the open-uri lib and generate a collage.
Note: This article is a continuation of my previous article, Render Great-looking Collages with Ruby and RMagick, it is highly recommended that you read it first.
What you will need
- Ruby
- RMagick
- RFlicker
- ImageMagick or GraphicMagick
- open-uri, a standard ruby core library
If you have Ruby and ImageMagick already installed you can easily install the RMagick and RFlickr libs with rubygems
gem install rmagick
gem install rflickr
To require the rmagick and rflickr gems in our code, require ‘rubygems’ first
require 'rubygems'
require 'RMagick'
require 'flickr'
require 'open-uri'
include 'Magick'
In this tutorial I am using Douglas Bowman’s templates from his gallery theme. Here are the two images you will need, in PNG-24 format.
Here is the finished flickr_collage.rb file you may use to follow along.
- Download: flickr_collage.rb
Rock and Roll
Step 1: Obtaining an API Key from Flickr
- Sign up for your very own API Key
If you do not already have a flickr account or yahoo screen name, you will need to create one now.
You will need to authorize your email address before using the key.
The API Key is represented in 2 parts, the Key and Secret. After you login you may view your registered keys.
Step: Authorizing the application
Within the flickr_collage.rb there is a routine for authorizing your application and saving the returned token in a cache file. The location of the cache file, stored in the TOKEN_CACHE constant must be user writable. In this example TOKEN_CACHE is set to ’/var/tmp/token_cache’ on my Mac OS X system, you may want to change it to the present working directory. Since this is the first time authorizing your application Flickr will return an authorization link, which will grant your application a permission level. Follow the link and follow the instructions.
You will need to set up these constants within the source document, API_KEY, SHARED_SECRET, and TOKEN_CACHE
API_KEY = ''
SHARED_SECRET = ''
TOKEN_CACHE = '/var/tmp/tocken_cache'
flickr = Flickr.new(TOKEN_CACHE, API_KEY, SHARED_SECRET)
# if your application token_cache has not yet been created you will need to
# authorize your application with flickr.
unless flickr.auth.token
flickr.auth.getFrob
url = flickr.auth.login_link
puts "You must visit #{url} to authorize this application. Press enter"+
" when you have done so. This is the only time you will have to do this."
gets
flickr.auth.getToken
flickr.auth.cache_token
endStep 3. Fetching Images from your Photo Set
The following block of code connects to your flickr account and returns urls for all images within your chosen photo set.
Note: the SET_TITLE constant must be set to the name of a photo set in your album. In this example I am grabbing photos from my “Friends” photo set, which contain photos of my favourite friends.
SET_TITLE = "Friends" # change this to the name of one of your photosets
# get a list of images from your photoset
sets = flickr.photosets.getList
set = sets.find { |s| s.title == SET_TITLE }
photos = set.photos
# The following loop picks 4 random images from our Flickr set
# 1 for the main image and 3 for the slides
images = Array.new
(1..4).each do
image_number = rand(photos.size)
redo if images.include? photos[image_number].url
images << photos[image_number].url
end4 random images from the “Friends” photoset are now stored in the images array. Now all that is needed to do is run the collage routine from the previous article and voila
Step 4: Searching for photos
Flickr API also lets you search their entire database of public photos. Next we will modify our code to search for photos by replacing the photoset code with the following block of code:
search_term = ARGV.shift
photos = flickr.photos.search(nil, nil, nil, search_term)This will allow us to specify our search term from the command line and get a unique collage back everytime.
ruby flickr_collage.rb "Flowers"

4 random flower pictures








Beautiful example of Ruby’s power. Thanks for the work you put into this tutorial! Please, keep up the good work!
Yes this is an awesome tutorial! I should put my little warhol it! application up, but it is not even remotely as advanced as this. The composites is where the power and fun lie IMHO.
:)
Thanks for the article. I enjoyed both your posts on using RMagick and look forward to trying some of this stuff out myself. Great design on your blog too.