This Python script automates updating Flickr image metadata using OpenAI's GPT-4 vision model. It analyses images in Flickr photosets, generates titles, descriptions, and relevant keywords, and then updates the Flickr metadata accordingly.
- OAuth authentication with Flickr API
- Retrieval of all photosets or processing of a specific photoset
- Image analysis using OpenAI's GPT-4 vision model
- Automatic updating of Flickr image tags, titles, and descriptions
- Cost tracking for OpenAI API usage
- Python 3.x
- Flickr API key and secret
- OpenAI API key
- Clone this repository or download the script.
- Install the required Python packages:
pip install flickrapi openai python-dotenv
- Create a
.env
file (or otherwise set the environment variables) in the same directory as the script with the following content:
FLICKR_API_KEY=your_flickr_api_key
FLICKR_API_SECRET=your_flickr_api_secret
OPENAI_API_KEY=your_openai_api_key
FLICKR_PHOTOSET_ID=optional_specific_photoset_id
Run the script using Python: python flickr_autotagger.py
The script will:
- Authenticate with Flickr using OAuth (requesting oob input)
- Retrieve photosets (all or a specific one)
- Process each image in the photosets
- Analyse images using OpenAI's GPT-4 vision model
- Update Flickr metadata with the generated information
- Track and display the cost of OpenAI API usage
The following optional environment variables can be set to configure how the script works, defaults have been set so an execution should be successful without configuring any of the below:
OPENAI_MODEL
: Specifies the OpenAI model to use (default: "gpt-4o-2024-08-06")OPENAI_COST_PER_PROMPT_TOKEN
: Cost per prompt token (default: 0.00250)OPENAI_COST_PER_COMPLETION_TOKEN
: Cost per completion token (default: 0.01000)OPENAI_VISION_COST_PER_IMAGE
: Fixed cost per image analysis (default: 0.000213)FLICKR_PRIVACY_FILTER
: Tells the Flickr API to return only images with a specific privacy level (0. none, 1. public/default, 2. friends, 3. family, 4. friends & family, 5. private)MAX_KEYWORDS
: Sets the maximum number of keywords to request GPT to generate (default: 10)FLICKR_TOKEN_FILE
: Specifies the file name to store the Flickr OAuth token (default: "flickr_token.json")DESCRIPTIONS_TO_ANALYZE
: A list of strings that, if found at the start of an existing Flickr description, will cause the script to analyze and update the image metadata (default: '["OLYMPUS DIGITAL CAMERA", "Untitled", "DSC_", "IMG_", "DCIM"]')SKIP_PREFIX
: A list of characters that, if found at the start of a photoset name, will cause the script to skip processing that photoset (default: '["#", "@"]')UPDATED_METADATA_FILE
: Specifies the file name to store the updated metadata for all processed images (default: "updated_metadata.json")
As of 2024-08-16, the script is configured to use gpt-4o, however, if you'd like to use the more economical gpt-4o-mini, the following could be used:
OPENAI_MODEL = "gpt-4o-mini"
OPENAI_COST_PER_1K_PROMPT_TOKEN = 0.000150
OPENAI_COST_PER_1K_COMPLETION_TOKEN = 0.000600
OPENAI_VISION_COST_PER_IMAGE = 0.000425
- The script skips photosets with names starting with "#" or "@"
- Images that already have descriptions are skipped unless they start with some that appear autogenerated (e.g. DCIM, IMG_, DSC_)