Download Images From Flickr With Python

Flickr has an amazing library of images and a stellar API for accessing and easily downloading them. I wanted to make use of their API to start downloading a collection of images to use on a future website project and so I started looking for a nice Python script to help me do it.

I found the awesome flickrpy python API wrapper that makes all the hard work very easy to use and wrote a short script that will search for images with a given tag(s) and download them to the current directory.

Expanding on this you could easily use PIL to modify the images and re-purpose them for use on another website such as a wordpress photoblog.

To use the script you’ll have to download flickrpy, and get a Flickr API key.

Here’s the Python script that will download 20 images from Flickr:

#!/usr/bin/env python
"""Usage: python flickrDownload.py TAGS
TAGS is a space delimited list of tags
 
Created by Matt Warren on 2009-09-08.
Copyright (c) 2009 HalOtis.com. All rights reserved.
"""
import sys
import shutil
import urllib
 
import flickr
 
NUMBER_OF_IMAGES = 20
 
#this is slow
def get_urls_for_tags(tags, number):
    photos = flickr.photos_search(tags=tags, tag_mode='all', per_page=number)
    urls = []
    for photo in photos:
        try:
            urls.append(photo.getURL(size='Large', urlType='source'))
        except:
            continue
    return urls
 
def download_images(urls):
    for url in urls:
        file, mime = urllib.urlretrieve(url)
        name = url.split('/')[-1]
        print name
        shutil.copy(file, './'+name)
 
def main(*argv):
    args = argv[1:]
    if len(args) == 0:
        print "You must specify at least one tag"
        return 1
 
    tags = [item for item in args]
 
    urls = get_urls_for_tags(tags, NUMBER_OF_IMAGES)
    download_images(urls)
 
if __name__ == '__main__':
    sys.exit(main(*sys.argv))

Technorati Tags: , , , , , , , ,



RSS feed | Trackback URI

3 Comments »

Comment by seozero
2009-09-09 01:17:13

Matt, what about Creative Commons-license? Can we specify:

1) Only search within Creative Commons-licensed content
2) Find content to use commercially
3) Find content to modify, adapt, or build upon

Thanx

Comment by Matt Warren
2009-09-09 08:23:17

sure.

just modify the photos_search function call like:
photos = flickr.photos_search(tags=tags, tag_mode=’all’, per_page=number, license=0)

where the license has the values:

0 – All Rights Reserved
4 – Attribution License
6 – Attribution-NoDerivs License
3 – Attribution-NonCommercial-NoDerivs License
2 – Attribution-NonCommercial License
1 – Attribution-NonCommercial-ShareAlike License
5 – Attribution-ShareAlike License
7 – No known copyright restrictions
8 – United States Government Work

 
 
Comment by Jason
2010-02-05 20:31:43

Dear halotis:

Hi I’m a graduate student in Taiwan.

I am trying to construct a dataset for my lab, which the dataset should have 1M photos

I want 10,000 photos for a tag.

but i realize even you change the photo.search’s arguments : page = ‘ ‘ for 1 , 2 , 3 , 4, 5 ..etc

flickr always give you many same result ( that mean many photos’ url are the same )

so even i request 500 photo per page for 30 page

i always get 3 or 4 thousand photos

That’s no enough cuz i want 10,000 photos for a tag.

Do you have some suggestion for me , thanks a lot .

 
Name (required)
E-mail (required - never shown publicly)
URI
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight=""> in your comment.