Improve swift python sample code

Change-Id: I6f43ac14d2a01b17a39c95b17919c5fd42115330
This commit is contained in:
Lingxian Kong 2017-10-09 14:17:57 +13:00
parent 601410e119
commit 3c8746f395

View File

@ -1,14 +1,21 @@
import os
from PIL import Image from PIL import Image
import swiftclient import swiftclient
def resize_image(image_path, resized_path):
with Image.open(image_path) as image:
image.thumbnail((75, 75))
image.save(resized_path)
def main(context, container, object): def main(context, container, object):
conn = swiftclient.Connection( conn = swiftclient.Connection(
session=context['os_session'], session=context['os_session'],
os_options={'region_name': 'RegionOne'}, os_options={'region_name': 'RegionOne'},
) )
# obj_header = conn.head_object(container, object)
new_container = '%s_thumb' % container new_container = '%s_thumb' % container
# Download original photo # Download original photo
@ -17,15 +24,10 @@ def main(context, container, object):
with open(image_path, 'w') as local: with open(image_path, 'w') as local:
local.write(obj_contents) local.write(obj_contents)
print('Downloaded object % from container %s' % (object, container)) print('Downloaded object %s from container %s' % (object, container))
# Resize thumb_path = '/thumb_%s' % object
SIZE = (75, 75) resize_image(image_path, thumb_path)
thumb_path = '/%s_thumb' % object
im = Image.open(image_path)
im.convert('RGB')
im.thumbnail(SIZE, Image.ANTIALIAS)
im.save(thumb_path, 'JPEG', quality=80)
print('Resized.') print('Resized.')
@ -38,6 +40,7 @@ def main(context, container, object):
content_type='text/plain' content_type='text/plain'
) )
print('Uploaded object %s to container %s' % (object, new_container)) os.remove(image_path)
os.remove(thumb_path)
return True print('Uploaded object %s to container %s' % (object, new_container))