FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-sdk-quickstart/server.py at main · cloudinary-devs/python-sdk-quickstart · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
cloudinary-devs
/
python-sdk-quickstart
Public
Notifications
You must be signed in to change notification settings
Fork
1
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-sdk-quickstart
/
server.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
124 lines (88 loc) · 4.4 KB
Breadcrumbs
python-sdk-quickstart
/
server.py
Copy path
File metadata and controls
124 lines (88 loc) · 4.4 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Set your Cloudinary credentials
# ==============================
from
dotenv
import
load_dotenv
load_dotenv
()
# Import the Cloudinary libraries
# ==============================
import
cloudinary
from
cloudinary
import
CloudinaryImage
import
cloudinary
.
uploader
import
cloudinary
.
api
# Import to format the JSON responses
# ==============================
import
json
# Set configuration parameter: return "https" URLs by setting secure=True
# ==============================
config
=
cloudinary
.
config
(
secure
=
True
)
# Log the configuration
# ==============================
print
(
"****1. Set up and configure the SDK:****
\n
Credentials: "
,
config
.
cloud_name
,
config
.
api_key
,
"
\n
"
)
def
uploadImage
():
# Upload the image and get its URL
# ==============================
# Upload the image.
# Set the asset's public ID and allow overwriting the asset with new versions
cloudinary
.
uploader
.
upload
(
"https://cloudinary-devs.github.io/cld-docs-assets/assets/images/butterfly.jpeg"
,
public_id
=
"quickstart_butterfly"
,
unique_filename
=
False
,
overwrite
=
True
)
# Define the transformation separately
transformation
=
[
{
"width"
:
400
,
"height"
:
400
,
"crop"
:
"crop"
}
]
# Upload the image with the specified transformation
result
=
cloudinary
.
uploader
.
upload
(
"https://res.cloudinary.com/demo/image/upload/v1702378721/docs/wines.jpg"
,
public_id
=
"improve"
,
transformation
=
[{
"width"
:
400
,
"height"
:
400
,
"crop"
:
"auto"
,
"gravity"
:
"auto"
,
"effect"
:
"improve:50"
}]
)
# Build the URL for the image and save it in the variable 'srcURL'
srcURL
=
CloudinaryImage
(
"quickstart_butterfly"
).
build_url
()
# Log the image URL to the console.
# Copy this URL in a browser tab to generate the image on the fly.
print
(
"****2. Upload an image****
\n
Delivery URL: "
,
srcURL
,
"
\n
"
)
def
getAssetInfo
():
# Get and use details of the image
# ==============================
# Get image details and save it in the variable 'image_info'.
image_info
=
cloudinary
.
api
.
resource
(
"quickstart_butterfly"
)
print
(
"****3. Get and use details of the image****
\n
Upload response:
\n
"
,
json
.
dumps
(
image_info
,
indent
=
2
),
"
\n
"
)
# Assign tags to the uploaded image based on its width. Save the response to the update in the variable 'update_resp'.
if
image_info
[
"width"
]
>
900
:
update_resp
=
cloudinary
.
api
.
update
(
"quickstart_butterfly"
,
tags
=
"large"
)
elif
image_info
[
"width"
]
>
500
:
update_resp
=
cloudinary
.
api
.
update
(
"quickstart_butterfly"
,
tags
=
"medium"
)
else
:
update_resp
=
cloudinary
.
api
.
update
(
"quickstart_butterfly"
,
tags
=
"small"
)
# Log the new tag to the console.
print
(
"New tag: "
,
update_resp
[
"tags"
],
"
\n
"
)
def
createTransformation
():
# Transform the image
# ==============================
transformedURL
=
CloudinaryImage
(
"quickstart_butterfly"
).
build_url
(
width
=
100
,
height
=
150
,
crop
=
"fill"
)
# Log the URL to the console
print
(
"****4. Transform the image****
\n
Transfrmation URL: "
,
transformedURL
,
"
\n
"
)
# Use this code instead if you want to create a complete HTML image element:
# imageTag = cloudinary.CloudinaryImage("quickstart_butterfly").image(radius="max", effect="sepia")
# print("****4. Transform the image****\nTransfrmation URL: ", imageTag, "\n")
# Retrieve user's profile image ID from database
user_profile_image_id
=
"quickstart_butterfly"
# Instantiate a CloudinaryImage object with user's profile image ID
profile_image
=
CloudinaryImage
(
user_profile_image_id
)
# Generate URL with custom transformations based on user preferences
profile_image_url
=
profile_image
.
build_url
(
transformation
=
{
'width'
:
300
,
'height'
:
300
,
'crop'
:
'thumb'
})
print
(
"User Profile Image URL:"
,
profile_image_url
)
# User's preferred asset dimensions retrieved from database
user_preferred_width
=
300
user_preferred_height
=
200
# Check if user prefers square thumbnails
if
user_preferred_width
==
user_preferred_height
:
# Generate square thumbnail with crop mode
dynamic_asset_url
,
_
=
cloudinary
.
utils
.
cloudinary_url
(
"sample"
,
transformation
=
{
'width'
:
user_preferred_width
,
'height'
:
user_preferred_height
,
'crop'
:
'fill'
})
else
:
# Generate rectangular thumbnail without crop
dynamic_asset_url
,
_
=
cloudinary
.
utils
.
cloudinary_url
(
"sample"
,
transformation
=
{
'width'
:
user_preferred_width
,
'height'
:
user_preferred_height
})
print
(
"Dynamic Asset URL:"
,
dynamic_asset_url
)
def
main
():
uploadImage
()
getAssetInfo
()
createTransformation
()
main
();
Back
|
FazBrowse Home
|
New Git URL