First, go to the Flickr page and click ** Request an API Key **. If you haven't created a Flickr account here, create one and log in.

The left side is for free (non-commercial law) and the right side is for paid (commercial law), so select ** APPLY FOR A NON-COMMERICIAL KEY ** surrounded by a red line.

Then, the application registration screen will be displayed. Enter the name and intended use for the application, and click ** SUBMIT **.

After registration, you can get the key and Secret, so make a note of it in a memo pad.

Install the Flickr module from the following command.
pip install flickrapi
Before executing the program, create a folder to save the acquired images.
download.py
from flickrapi import FlickrAPI
from urllib.request import urlretrieve
from pprint import pprint
import os,time,sys
key = "Obtained key"
secret = "Acquired Secret"
wait_time = 1
#Get the first value of the command line argument. In the following cases[cat]Get
# python download.py cat 
animalname = sys.argv[1]
savedir = "./"+animalname
# format:Data to receive(Receive with json)
flickr = FlickrAPI(key, secret, format='parsed-json')
"""
text :Search keyword
per_page :Number of data you want to acquire
media :Type of data to search
sort :Sequence of data
safe_seach :Whether to display UI content
extras :The value of the option you want to get(url_q Image address information)
"""
result  = flickr.photos.search(
    text = animalname,
    per_page = 400,
    media = 'photos',
    sort = 'relevance',
    safe_seach = 1,
    extras = 'url_q, licence'
)
#result
photos = result['photos']
pprint(photos)
If the above program is executed and it succeeds, the following display will be displayed.
python
           {'farm': 5,
            'height_q': 150,
            'id': '4375810205',
            'isfamily': 0,
            'isfriend': 0,
            'ispublic': 1,
            'owner': '47750313@N07',
            'secret': '8d0a7d24a1',
            'server': '4008',
            'title': 'cat',
            'url_q': 'https://live.staticflickr.com/4008/4375810205_8d0a7d24a1_q.jpg',
            'width_q': 150},
           {'farm': 1,
            'height_q': 150,
            'id': '27083352608',
            'isfamily': 0,
            'isfriend': 0,
            'ispublic': 1,
            'owner': '144380504@N04',
            'secret': 'd1cd159107',
            'server': '811',
            'title': 'cat',
            'url_q': 'https://live.staticflickr.com/811/27083352608_d1cd159107_q.jpg',
            'width_q': 150}],
Get the URL registered in'url_q'and get the image data using urllib.
Add a program to acquire image data to the previous program.
python
for i,photo in enumerate(photos['photo']):
    url_q = photo['url_q']
    filepath = savedir + '/' + photo['id'] + '.jpg'
   #If there are duplicate files, skip them.
    if os.path.exists(filepath):continue
   #Download image data
    urlretrieve(url_q, filepath)
   #Wait 1 second to avoid overloading the server
    time.sleep(wait_time)
After executing the program with "cat" as an argument, the image of the cat could be obtained from Flickr.

Recommended Posts