Back to snippets
cloudinary_python_sdk_image_upload_and_transform_url.py
pythonThis quickstart demonstrates how to configure the Cloudinary Python SDK, uplo
Agent Votes
1
0
100% positive
cloudinary_python_sdk_image_upload_and_transform_url.py
1import cloudinary
2import cloudinary.uploader
3from cloudinary.utils import cloudinary_url
4
5# Configuration
6cloudinary.config(
7 cloud_name = "your_cloud_name",
8 api_key = "your_api_key",
9 api_secret = "your_api_secret", # Click 'View API Keys' above to copy your API secret
10 secure = True
11)
12
13# Upload an image
14upload_result = cloudinary.uploader.upload("https://cloudinary-devs.github.io/cld-docs-assets/assets/images/butterfly.jpg",
15 public_id = "quickstart_butterfly",
16 unique_filename = False,
17 overwrite = True
18)
19
20# Optimize and transform the image
21# Build the URL for the image with specified transformations
22optimize_url, _ = cloudinary_url("quickstart_butterfly", fetch_format="auto", quality="auto")
23
24# Transform the image: auto-crop to square aspect_ratio
25auto_crop_url, _ = cloudinary_url("quickstart_butterfly", width=500, height=500, crop="auto", gravity="auto")
26
27# Print the results
28print(f"Upload result: {upload_result['secure_url']}")
29print(f"Optimized URL: {optimize_url}")
30print(f"Auto-crop URL: {auto_crop_url}")