From cb21ba082817b39c91794fea155f398cfbffa586 Mon Sep 17 00:00:00 2001 From: priya Date: Fri, 4 Oct 2024 11:06:12 +0530 Subject: [PATCH 1/5] feature done --- samples/content-creation/README.md | 138 ++++++++++++++++++ .../cloudinary-ai-post-generator.py | 101 +++++++++++++ 2 files changed, 239 insertions(+) create mode 100644 samples/content-creation/README.md create mode 100644 samples/content-creation/cloudinary-ai-post-generator.py diff --git a/samples/content-creation/README.md b/samples/content-creation/README.md new file mode 100644 index 00000000..11aea37f --- /dev/null +++ b/samples/content-creation/README.md @@ -0,0 +1,138 @@ +# Social Media Content Creation API with Cloudinary + +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. + +## Features + +- 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. + +## Prerequisites + +Before running this project, ensure you have: + +1. [Python 3.x](https://www.python.org/downloads/) +2. A [Cloudinary account](https://cloudinary.com/users/register/free) +3. Cloudinary Python SDK installed via pip. + +## Setup Instructions + +### 1. Install Dependencies + +After cloning or downloading this repository, install the required packages using `pip`: + +```bash +pip install flask cloudinary +``` + +2. Configure 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): +```bash +export CLOUDINARY_URL=cloudinary://:@ +``` + +For Windows (Command Prompt/PowerShell): +```bash +set CLOUDINARY_URL=cloudinary://:@ +``` + +3. Running the Flask App + +Start the Flask server by running: + +```bash +python app.py +``` + +The server will be available at http://127.0.0.1:5000/. + + +Usage +1. Uploading an Image for Social Media + +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: + +```bash +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: +```bash +{ + "status": "success", + "image_url": "http://res.cloudinary.com//image/upload/v12345678/my_custom_id.jpg" +} +``` +The image is transformed (resized to 200x200, cropped to fill), optimized for social media platforms. + + +2. Cleaning Up Uploaded Images + +To delete all images uploaded with the default tag (set as python_sample_basic), you can run: + +```bash +python app.py cleanup +``` + +This will delete all images tagged under DEFAULT_TAG. +Recommended Image Transformations for Social Media + + 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: + +```bash +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. +Additional Functionality +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.). +Environment Variables (Optional) + +If you prefer, you can store the CLOUDINARY_URL in a .env file to make environment configuration easier: + +```bash +CLOUDINARY_URL=cloudinary://:@ +``` +After creating the .env file, load it by running: +```bash +source .env +``` +Conclusion + +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. + +Good luck and happy posting! \ No newline at end of file diff --git a/samples/content-creation/cloudinary-ai-post-generator.py b/samples/content-creation/cloudinary-ai-post-generator.py new file mode 100644 index 00000000..d5b6b348 --- /dev/null +++ b/samples/content-creation/cloudinary-ai-post-generator.py @@ -0,0 +1,101 @@ +#!/usr/bin/env python +import os +import sys + +from cloudinary.api import delete_resources_by_tag, resources_by_tag +from cloudinary.uploader import upload +from cloudinary.utils import cloudinary_url +from flask import Flask, request, jsonify + +# Initialize Flask app +app = Flask(__name__) + +# Config +os.chdir(os.path.join(os.path.dirname(sys.argv[0]), '.')) +if os.path.exists('settings.py'): + exec(open('settings.py').read()) + +DEFAULT_TAG = "python_sample_basic" + + +def dump_response(response): + """Function to print and handle upload response""" + print("Upload response:") + for key in sorted(response.keys()): + print(" %s: %s" % (key, response[key])) + + +def upload_file(file_path, public_id=None): + """Upload a file to Cloudinary with options for custom public ID""" + print(f"--- Uploading {file_path}") + + # Upload with optional custom public ID + response = upload(file_path, tags=DEFAULT_TAG, public_id=public_id) + + dump_response(response) + + url, options = cloudinary_url( + response['public_id'], + format=response['format'], + width=200, + height=150, + crop="fill" + ) + print("Image URL: " + url) + return url + + +@app.route('/generate_post', methods=['POST']) +def generate_post(): + """API endpoint to handle post generation and image upload""" + try: + # Get image file from request + image = request.files.get('image') + + if not image: + return jsonify({"error": "No image file provided"}), 400 + + # Create uploads directory if it doesn't exist + uploads_dir = os.path.join(os.path.dirname(__file__), 'uploads') + os.makedirs(uploads_dir, exist_ok=True) + + # Save image locally + file_path = os.path.join(uploads_dir, image.filename) + image.save(file_path) + + # Upload file to Cloudinary + public_id = request.form.get('public_id', None) + image_url = upload_file(file_path, public_id=public_id) + + # Clean up the local file after upload + os.remove(file_path) + + # Return response + return jsonify({"status": "success", "image_url": image_url}) + + except Exception as e: + print(f"Error: {str(e)}") # Log the error + return jsonify({"error": str(e)}), 500 + + +def cleanup(): + """Cleanup resources by tag""" + response = resources_by_tag(DEFAULT_TAG) + resources = response.get('resources', []) + if not resources: + print("No images found") + return + print(f"Deleting {len(resources)} images...") + delete_resources_by_tag(DEFAULT_TAG) + print("Done!") + + +if __name__ == '__main__': + if len(sys.argv) > 1: + if sys.argv[1] == 'upload': + upload_file("sample.jpg") + elif sys.argv[1] == 'cleanup': + cleanup() + else: + print("--- Starting Flask server ---") + app.run(debug=True) \ No newline at end of file From ddb2496b8bfd9c3e6826d505212e1258b1882b9c Mon Sep 17 00:00:00 2001 From: priya Date: Fri, 4 Oct 2024 11:28:20 +0530 Subject: [PATCH 2/5] editing build files --- .travis.yml | 4 ++-- tox.ini | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index abc300c0..4931d343 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,7 +27,7 @@ matrix: - python: 3.11 env: TOXENV=py311-django41 install: -- pip install tox +- pip install tox pytest # Install pytest alongside tox before_script: > export CLOUDINARY_URL=$(bash tools/get_test_cloud.sh); @@ -39,4 +39,4 @@ script: notifications: email: recipients: - - sdk_developers@cloudinary.com + - sdk_developers@cloudinary.com \ No newline at end of file diff --git a/tox.ini b/tox.ini index 14404d1a..184856a4 100644 --- a/tox.ini +++ b/tox.ini @@ -3,19 +3,21 @@ envlist = py{27,37,38,39,310,311}-core py{27}-django{111} py{37,38,39,310,311}-django{22,32,40,41} + [testenv] usedevelop = True commands = - core: python setup.py test {env:P_ARGS:} + core: pytest {env:P_ARGS:} django{111,22,32}: django-admin.py test -v2 django_tests {env:D_ARGS:} django{40,41}: django-admin test -v2 django_tests {env:D_ARGS:} passenv = * deps = - django{111,22,32,40,41}: mock + mock + django{111,22,32,40,41} django111: Django>=1.11,<1.12 django22: Django>=2.2,<2.3 django32: Django>=3.2,<3.3 django40: Django>=4.0,<4.1 django41: Django>=4.1,<4.2 setenv = - DJANGO_SETTINGS_MODULE=django_tests.settings + DJANGO_SETTINGS_MODULE=django_tests.settings \ No newline at end of file From 7fe4ac071eb7093a4fe2193c73ed1c69afe1e628 Mon Sep 17 00:00:00 2001 From: priya Date: Fri, 4 Oct 2024 11:33:23 +0530 Subject: [PATCH 3/5] undo: editing build files --- .travis.yml | 2 +- tox.ini | 6 ++---- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4931d343..a363b6f4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -27,7 +27,7 @@ matrix: - python: 3.11 env: TOXENV=py311-django41 install: -- pip install tox pytest # Install pytest alongside tox +- pip install tox before_script: > export CLOUDINARY_URL=$(bash tools/get_test_cloud.sh); diff --git a/tox.ini b/tox.ini index 184856a4..62a2d38a 100644 --- a/tox.ini +++ b/tox.ini @@ -3,17 +3,15 @@ envlist = py{27,37,38,39,310,311}-core py{27}-django{111} py{37,38,39,310,311}-django{22,32,40,41} - [testenv] usedevelop = True commands = - core: pytest {env:P_ARGS:} + core: python setup.py test {env:P_ARGS:} django{111,22,32}: django-admin.py test -v2 django_tests {env:D_ARGS:} django{40,41}: django-admin test -v2 django_tests {env:D_ARGS:} passenv = * deps = - mock - django{111,22,32,40,41} + django{111,22,32,40,41}: mock django111: Django>=1.11,<1.12 django22: Django>=2.2,<2.3 django32: Django>=3.2,<3.3 From 9a6b23ad26cee0ac29c5117476dcac261ba3f959 Mon Sep 17 00:00:00 2001 From: priya Date: Thu, 10 Oct 2024 11:20:26 +0530 Subject: [PATCH 4/5] ui changes and additional --- .../cloudinary-ai-post-generator.py | 45 ++++++++-- samples/{basic => content-creation}/lake.jpg | Bin samples/content-creation/settings.py | 7 ++ samples/content-creation/static/index.html | 77 ++++++++++++++++++ 4 files changed, 123 insertions(+), 6 deletions(-) rename samples/{basic => content-creation}/lake.jpg (100%) create mode 100644 samples/content-creation/settings.py create mode 100644 samples/content-creation/static/index.html diff --git a/samples/content-creation/cloudinary-ai-post-generator.py b/samples/content-creation/cloudinary-ai-post-generator.py index d5b6b348..e4d80417 100644 --- a/samples/content-creation/cloudinary-ai-post-generator.py +++ b/samples/content-creation/cloudinary-ai-post-generator.py @@ -25,15 +25,41 @@ def dump_response(response): print(" %s: %s" % (key, response[key])) -def upload_file(file_path, public_id=None): - """Upload a file to Cloudinary with options for custom public ID""" +def upload_file(file_path, public_id=None, mood=None, theme=None): + """Upload a file to Cloudinary with options for custom public ID and transformations""" print(f"--- Uploading {file_path}") - # Upload with optional custom public ID - response = upload(file_path, tags=DEFAULT_TAG, public_id=public_id) + # Define transformations based on mood + transformations = [] + if mood == "happy": + transformations.append({"effect": "brightness:30"}) # Increase brightness + elif mood == "sad": + transformations.append({"effect": "grayscale"}) # Convert to grayscale + + # Add text overlay based on theme + if theme: + transformations.append({ + "overlay": { + "font_family": "Arial", + "font_size": 20, + "text": f"{theme.capitalize()} - {mood.capitalize()}", + "text_color": "white" + }, + "gravity": "north", + "y": 10 + }) + + # Upload with transformations + response = upload( + file_path, + public_id=public_id, + transformation=transformations, + tags=DEFAULT_TAG + ) + dump_response(response) - + url, options = cloudinary_url( response['public_id'], format=response['format'], @@ -65,7 +91,9 @@ def generate_post(): # Upload file to Cloudinary public_id = request.form.get('public_id', None) - image_url = upload_file(file_path, public_id=public_id) + mood = request.form.get('mood', None) + theme = request.form.get('theme', None) + image_url = upload_file(file_path, public_id=public_id, mood=mood, theme=theme) # Clean up the local file after upload os.remove(file_path) @@ -90,6 +118,11 @@ def cleanup(): print("Done!") +@app.route('/') +def index(): + return app.send_static_file('index.html') + + if __name__ == '__main__': if len(sys.argv) > 1: if sys.argv[1] == 'upload': diff --git a/samples/basic/lake.jpg b/samples/content-creation/lake.jpg similarity index 100% rename from samples/basic/lake.jpg rename to samples/content-creation/lake.jpg diff --git a/samples/content-creation/settings.py b/samples/content-creation/settings.py new file mode 100644 index 00000000..039125b4 --- /dev/null +++ b/samples/content-creation/settings.py @@ -0,0 +1,7 @@ +import cloudinary + +cloudinary.config( + cloud_name = "dlpsfujke", + api_key = "293164293581939", + api_secret = "j6d15YfK6-uQgm_nYO5f7EHBd9o" +) diff --git a/samples/content-creation/static/index.html b/samples/content-creation/static/index.html new file mode 100644 index 00000000..331180aa --- /dev/null +++ b/samples/content-creation/static/index.html @@ -0,0 +1,77 @@ + + + + + + Image Upload + + + +

Upload Image to Cloudinary

+
+ + + + + + +
+
+ + + + \ No newline at end of file From 77d42ae4fcb9b78d8510c30c5df7ad6552224907 Mon Sep 17 00:00:00 2001 From: priya Date: Thu, 10 Oct 2024 11:24:34 +0530 Subject: [PATCH 5/5] readme update --- samples/content-creation/README.md | 63 +++++++++++++++------------- samples/content-creation/settings.py | 6 +-- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/samples/content-creation/README.md b/samples/content-creation/README.md index 11aea37f..3c9732db 100644 --- a/samples/content-creation/README.md +++ b/samples/content-creation/README.md @@ -8,6 +8,11 @@ This project demonstrates a simple Flask API 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. ## Prerequisites @@ -27,9 +32,10 @@ After cloning or downloading this repository, install the required packages usin pip install flask cloudinary ``` -2. Configure Cloudinary +### 2. Configure 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): ```bash export CLOUDINARY_URL=cloudinary://:@ @@ -40,7 +46,7 @@ For Windows (Command Prompt/PowerShell): set CLOUDINARY_URL=cloudinary://:@ ``` -3. Running the Flask App +### 3. Running the Flask App Start the Flask server by running: @@ -50,22 +56,23 @@ python app.py The server will be available at http://127.0.0.1:5000/. +## Usage -Usage -1. Uploading an Image for Social Media +### 1. Uploading an Image for Social Media 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. +- **Endpoint**: /generate_post +- **Method**: POST +- **Parameters**: + - image (required): The image file to upload. + - public_id (optional): Custom public ID for the image. -Image Transformations: +**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: + +**Example with cURL**: ```bash curl -X POST http://localhost:5000/generate_post \ @@ -73,7 +80,7 @@ curl -X POST http://localhost:5000/generate_post \ -F "public_id=my_custom_id" ``` -Example Response: +**Example Response**: ```bash { "status": "success", @@ -82,8 +89,7 @@ Example Response: ``` The image is transformed (resized to 200x200, cropped to fill), optimized for social media platforms. - -2. Cleaning Up Uploaded Images +### 2. Cleaning Up Uploaded Images To delete all images uploaded with the default tag (set as python_sample_basic), you can run: @@ -92,15 +98,16 @@ python app.py cleanup ``` This will delete all images tagged under DEFAULT_TAG. -Recommended Image Transformations for Social Media - 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. +## Recommended Image Transformations for Social Media -Example Transformations in the Code: +- **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. - Resize and Crop: Automatically applied transformation in the API: +**Example Transformations in the Code**: + +Resize and Crop: Automatically applied transformation in the API: ```bash url, options = cloudinary_url( @@ -113,14 +120,13 @@ url, options = cloudinary_url( ``` This resizes the uploaded image to 200x200 pixels and crops it to fit. -Additional Functionality -Setting Custom Public IDs -You can assign custom public IDs for uploaded images, which is useful for managing content more effectively. -Dynamic Transformations +## Additional Functionality + +- **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.). -Feel free to modify the transformations in upload_file() to match specific platform requirements (e.g., square thumbnails, vertical or horizontal banners, etc.). -Environment Variables (Optional) +### Environment Variables (Optional) If you prefer, you can store the CLOUDINARY_URL in a .env file to make environment configuration easier: @@ -131,8 +137,9 @@ After creating the .env file, load it by running: ```bash source .env ``` -Conclusion -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. +## Conclusion + +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! \ No newline at end of file diff --git a/samples/content-creation/settings.py b/samples/content-creation/settings.py index 039125b4..e0add057 100644 --- a/samples/content-creation/settings.py +++ b/samples/content-creation/settings.py @@ -1,7 +1,7 @@ import cloudinary cloudinary.config( - cloud_name = "dlpsfujke", - api_key = "293164293581939", - api_secret = "j6d15YfK6-uQgm_nYO5f7EHBd9o" + cloud_name = "xxx", + api_key = "xxx", + api_secret = "xxx" )