This project demonstrates a simple Flask API for social media content creation. The API allows users to upload images, apply transformations suitable for social media (like resizing, cropping, and adjusting image formats), and optionally set custom public IDs. Additionally, the API provides functionality to clean up old uploads by tag.
- Upload images for social media content creation.
- Apply transformations such as resizing, cropping, and format adjustment for optimal display on platforms like Instagram, Facebook, and Twitter.
- Option to assign custom public IDs for better image management.
- Cleanup of previously uploaded images by tag.
- Image Uploading: Easily upload images to Cloudinary for use in your content.
- Image Transformation: Utilize powerful transformation capabilities to manipulate images directly in your content generation process.
- Content Delivery: Benefit from fast and efficient image delivery through Cloudinary's global CDN.
- AI Integration: Enhance your content generation logic by integrating AI models for dynamic content creation.
- Dynamic Content Creation: Create personalized content based on user preferences or trends.
Before running this project, ensure you have:
- Python 3.x
- A Cloudinary account
- Cloudinary Python SDK installed via pip.
After cloning or downloading this repository, install the required packages using pip
:
pip install flask cloudinary
You need to configure the CLOUDINARY_URL environment variable with your Cloudinary credentials. You can find your credentials in the Cloudinary Management Console.
For Linux/MacOS (bash/zsh):
export CLOUDINARY_URL=cloudinary://<API-Key>:<API-Secret>@<Cloud-name>
For Windows (Command Prompt/PowerShell):
set CLOUDINARY_URL=cloudinary://<API-Key>:<API-Secret>@<Cloud-name>
Start the Flask server by running:
python app.py
The server will be available at http://127.0.0.1:5000/.
To upload an image with transformations applied (suitable for social media), send a POST request to the /generate_post endpoint with the image file. You can optionally provide a public_id for the image.
- Endpoint: /generate_post
- Method: POST
- Parameters:
- image (required): The image file to upload.
- public_id (optional): Custom public ID for the image.
Image Transformations:
The API will automatically resize the image to a 1:1 aspect ratio (200x200px), perfect for profile pictures, thumbnails, or other social media purposes.
Example with cURL:
curl -X POST http://localhost:5000/generate_post \
-F "image=@/path/to/your/social_media_image.jpg" \
-F "public_id=my_custom_id"
Example Response:
{
"status": "success",
"image_url": "http://res.cloudinary.com/<cloud-name>/image/upload/v12345678/my_custom_id.jpg"
}
The image is transformed (resized to 200x200, cropped to fill), optimized for social media platforms.
To delete all images uploaded with the default tag (set as python_sample_basic), you can run:
python app.py cleanup
This will delete all images tagged under DEFAULT_TAG.
- Profile Pictures/Thumbnails: Resize to 200x200px with a 1:1 aspect ratio.
- Banners: Crop to 1200x400px for optimal display on platforms like Twitter.
- Story Images: Resize to 1080x1920px (vertical aspect ratio) for Instagram or Snapchat stories.
Example Transformations in the Code:
Resize and Crop: Automatically applied transformation in the API:
url, options = cloudinary_url(
response['public_id'],
format=response['format'],
width=200,
height=200,
crop="fill"
)
This resizes the uploaded image to 200x200 pixels and crops it to fit.
- Setting Custom Public IDs: You can assign custom public IDs for uploaded images, which is useful for managing content more effectively.
- Dynamic Transformations: Feel free to modify the transformations in upload_file() to match specific platform requirements (e.g., square thumbnails, vertical or horizontal banners, etc.).
If you prefer, you can store the CLOUDINARY_URL in a .env file to make environment configuration easier:
CLOUDINARY_URL=cloudinary://<API-Key>:<API-Secret>@<Cloud-name>
After creating the .env file, load it by running:
source .env
This project is designed to help you quickly set up an image uploading API tailored to social media content creation needs. It handles image transformations, easy uploads, and content management using Cloudinary. By leveraging the features of pycloudinary
, you can create a robust content generation system that enhances your posts with relevant images.
Good luck and happy posting!