-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.py
73 lines (60 loc) · 1.97 KB
/
example.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
import sys
import logging
from cloudstorageimageresizer import ImageResizer
from google.cloud import storage
# Demonstrate module usage and asserts its behavior (I know, this is not a
# proper test suite...)
#
# Requirements:
# 1. Put your google storage credentials in json format in the file 'gcloud-credentials.json' # noqa
# 2. Edit the BUCKET_NAME to a bucket you have write access to
#
# Run:
# python example.py
BUCKET_NAME = 'gfdusrpcts'
# Logging setup
log = logging.getLogger(__name__)
root = logging.getLogger()
root.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s: %(levelname)s %(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)
logging.getLogger('boto').setLevel(logging.INFO)
# EOF logging setup. Pfew.
client = storage.Client.from_service_account_json('gcloud-credentials.json')
i = ImageResizer(client)
i.fetch_image_from_url('https://cdn.shopify.com/s/files/1/1414/7912/products/olm_50macs_rainbow.jpg?v=1541103852') # noqa
url = i.store_and_return_url(
in_bucket=BUCKET_NAME,
key_name='raw.png'
)
log.info("Got url %s" % url)
want = 'https://storage.googleapis.com/%s/%s' % (BUCKET_NAME, 'raw.png')
assert url == want, '%s == %s' % (url, want)
# apply exif orientation, if any
i.orientate()
# resize to width 200
ii = i.resize(width=200)
url_w200 = ii.store_and_return_url(
in_bucket=BUCKET_NAME,
key_name='raw_w200.png'
)
log.info("Got url %s" % url_w200)
want = 'https://storage.googleapis.com/%s/%s' % (BUCKET_NAME, 'raw_w200.png')
assert url_w200 == want, '%s == %s' % (url_w200, want)
# resize to height 200
ii = i.resize(height=200)
url_h200 = ii.store_and_return_url(
in_bucket=BUCKET_NAME,
key_name='raw_h200.png'
)
log.info("Got url %s" % url_h200)
# resize to a 100 square
ii = i.resize(width=100, height=100)
url_w100_h100 = ii.store_and_return_url(
in_bucket=BUCKET_NAME,
key_name='raw_w100_h100.png'
)
log.info("Got url %s" % url_w100_h100)