-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
143 lines (127 loc) · 3.75 KB
/
base.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# -*- coding: utf-8 -*-
"""
Created on Sat Feb 16 05:03:31 2019
@author: user
"""
#!/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
# 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):
print("Upload response:")
for key in sorted(response.keys()):
print(" %s: %s" % (key, response[key]))
def upload_files():
print("--- Upload a local file")
response = upload("desktop.PNG", tags=DEFAULT_TAG)
dump_response(response)
url, options = cloudinary_url(
response['public_id'],
format=response['format'],
width=200,
height=150,
crop="fill"
)
print("Fill 200x150 url: " + url)
print("")
print("--- Upload a local file with custom public ID")
response = upload(
"desktop.PNG",
tags=DEFAULT_TAG,
public_id="custom_name",
)
dump_response(response)
url, options = cloudinary_url(
response['public_id'],
format=response['format'],
width=200,
height=150,
crop="fit"
)
print("Fit into 200x150 url: " + url)
print("")
print("--- Upload a local file with eager transformation of scaling to 200x150")
response = upload(
"desktop.PNG",
tags=DEFAULT_TAG,
public_id="eager_custom_name",
eager=dict(
width=200,
height=150,
crop="scale"
),
)
dump_response(response)
url, options = cloudinary_url(
response['public_id'],
format=response['format'],
width=200,
height=150,
crop="scale",
)
print("scaling to 200x150 url: " + url)
print("")
print("--- Upload by fetching a remote image")
response = upload(
"http://res.cloudinary.com/demo/image/upload/couple.jpg",
tags=DEFAULT_TAG
)
dump_response(response)
url, options = cloudinary_url(
response['public_id'],
format=response['format'],
width=200,
height=150,
crop="thumb",
gravity="faces",
)
print("Face detection based 200x150 thumbnail url: " + url)
print("")
print("--- Fetch an uploaded remote image, fitting it into 500x500 and reducing saturation")
response = upload(
"http://res.cloudinary.com/demo/image/upload/couple.jpg",
tags=DEFAULT_TAG,
width=500,
height=500,
crop="fit",
effect="saturation:-70",
)
dump_response(response)
url, options = cloudinary_url(
response['public_id'],
format=response['format'],
width=200,
height=150,
crop="fill",
gravity="faces",
radius=10,
effect="sepia",
)
print("Fill 200x150, round corners, apply the sepia effect, url: " + url)
print("")
def cleanup():
response = resources_by_tag(DEFAULT_TAG)
resources = response.get('resources', [])
if not resources:
print("No images found")
return
print("Deleting {0:d} images...".format(len(resources)))
delete_resources_by_tag(DEFAULT_TAG)
print("Done!")
if len(sys.argv) > 1:
if sys.argv[1] == 'upload':
upload_files()
if sys.argv[1] == 'cleanup':
cleanup()
else:
print("--- Uploading files and then cleaning up")
print(" you can only choose one instead by passing 'upload' or 'cleanup' as an argument")
print("")
upload_files()