Flickrapi and photos of a specific user with Python

Asked

Viewed 31 times

0

Hey, guys, all right?

I need to get some pictures of a specific FLICKR user and with a specific description for a college job. I’m still learning how to program, so I don’t really know how to do it, I tried to read the API guidelines, but I didn’t get it right. The most I could download were some photos with a specific "keyword", but the content doesn’t exactly match what I need. I tried to put several Keywords or try to add the photographer’s name, but I could not download any photo. I believe it would solve the problem if I pulled only the photos of a specific user with the description I need, it is possible to do this with the flickrapi or other api?

#id of the marcozeroconteudo: 152991702@N05

Here send an example photo I need to download. And the descriptions of the text are these:

Marco Zero Content Suape,16 August 2018. Atlantico Shipyard South. Credit: Keila Castro/MZ Content

Her tags are: Suape, Estaleiro, Atlantico Sul

import flickrapi
import urllib
import urllib.request
from PIL import Image
from tqdm import tqdm
from keras.preprocessing.image import ImageDataGenerator

# image.load_img()
#id da marcozeroconteudo: 152991702@N05

# Flickr api access key 
flickr=flickrapi.FlickrAPI('MINHA_API_KEY', 'MINHA_API_SECRET', cache=True)


N_MAX = 500
keyword = 'Estaleiro Altantico Sul'


PHOTOs = flickr.walk(text=keyword,
                     tag_mode='all',
                     tags=keyword,
                     extras='url_c',
                     per_page=108,           # may be you can try different numbers..
                     sort='relevance')

urls = []
for N, PHOTO in tqdm(enumerate(PHOTOs)):
    
    url = PHOTO.get('url_c')
    urls.append(url)
    
    # get 50 urls
    if N > N_MAX:
      break
print (urls)


# Download image from the url and save it to '00001.jpg'
#urllib.request.urlretrieve(urls[0], '00001.jpg')
RESIZE_OPTION = Image.ANTIALIAS #Image.NEAREST, Image.BICUBIC

for i in range(N_MAX):
  fname = f'img_{keyword}{i}.jpg'
  urllib.request.urlretrieve(urls[i], f'img{keyword}_{i}.jpg')
# Resize the image and overwrite it

image = image.open(fname) 
image = image.resize((256, 256), RESIZE_OPTION)
image.save(fname)

Thanks in advance for the help,

1 answer

0


Repurposing your code

Importing the necessary libraries:

import flickrapi
import urllib.request
from PIL import Image

Authentication:

api_key = u'API_KEY' # passando em formato unicode
api_secret = u'API_SECRET' # passando em formato unicode
flickr = flickrapi.FlickrAPI(api_key, api_secret, cache = True)

Tags that will be searched:

tags = ['Suape', 'Estaleiro', 'Atlântico Sul'] 
photos = flickr.walk(tag_mode='all',
                     tags = tags,
                     extras='url_c',
                     per_page=100)

Taking the urls, in this case I only insert the 65535, which is a default of the url of the user you want to grab the images:

urls = []
for i, photo in enumerate(photos):
    user = '65535'
    try:
        url = photo.get('url_c')
        if user in url:
            urls.append(url)
    except:
        pass

Downloading the images and saving in the directory where the script is running:

for i, url in enumerate(urls):
    try:
        name = str(i) + '.jpg'
        urllib.request.urlretrieve(url, name)
        image = Image.open(name) 
        image = image.resize((256, 256), Image.ANTIALIAS)
        image.save(name)
    except:
        pass

I have removed api_key and secret since the question has been updated so as not to harm the questioner.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.