Skip to content

Instantly share code, notes, and snippets.

@NordomWhistleklik
Created January 7, 2013 16:56
Show Gist options
  • Save NordomWhistleklik/4476526 to your computer and use it in GitHub Desktop.
Save NordomWhistleklik/4476526 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys
import os
import ssl
try:
import cloudfiles
except ImportError:
sys.exit("cloudfiles module missing. Please install before running.")
try:
from PIL import Image
except ImportError:
sys.exit("PIL module missing. Please install before running.")
# Cloud Files username & API key
username = ''
key = ''
# Source and destination container names
originContainerName = ''
targetContainerName = ''
# Max width and height for new images
newSize = (2000,2000)
def getImg(name):
obj = originContainer.get_object(name)
obj.save_to_filename("down/"+name)
return
def resizeImg(name):
img = Image.open("down/"+name)
img.thumbnail(newSize, Image.ANTIALIAS)
f,e = os.path.splitext(name)
new_name = f + ".jpg"
img.save("up/"+new_name)
return new_name
def uploadImg(new_name):
new_obj = targetContainer.create_object(new_name)
new_obj.content_type = 'image/jpeg'
new_obj.load_from_filename("up/"+new_name)
return
def processImg(name):
print "Converting",name
# Get Image
done = False
while not done:
try:
print "-- Getting image... ",
getImg(name)
print "[DONE]"
done = True
except ssl.SSLError:
print "[FAILED - RETRYING]"
continue
except:
print "[FAILED - SKIPPING]"
return
# Resize Image
try:
print "-- Resizing image... ",
new_name = resizeImg(name)
print "[DONE]"
except:
print "[FAILED - SKIPPING]"
return
# Upload Image
try:
print "-- Uploading image... ",
uploadImg(new_name)
print "[DONE]"
except:
print "[FAILED - SKIPPING]"
return
return
def cleanup():
print "Removing local files"
try:
dirListDown=os.listdir("down")
for fname in dirListDown:
os.remove("down/"+fname)
dirListUp=os.listdir("up")
for fname in dirListUp:
os.remove("up/"+fname)
print "-- [DONE]"
except:
print "-- [FAILED]"
def containerDif(originContainer,targetContainer):
print "Determining files left to convert"
already_done_list = []
already_done_10000 = targetContainer.list_objects()
while already_done_10000:
already_done_list.extend(already_done_10000)
mark = already_done_list[-1]
already_done_10000 = targetContainer.list_objects(marker=mark)
origin_list = []
origin_10000 = originContainer.list_objects()
while origin_10000:
origin_list.extend(origin_10000)
mark = origin_list[-1]
origin_10000 = originContainer.list_objects(marker=mark)
mod_already_done_list = []
for i in already_done_list:
f,e = os.path.splitext(i)
mod_already_done_list.append(f)
to_do_list = []
for i in origin_list:
origin_list_f,origin_list_e = os.path.splitext(i)
if origin_list_f not in mod_already_done_list:
to_do_list.append(i)
print "-- [DONE]"
return to_do_list
def cloudConnect():
global originContainer
global targetContainer
global connection
print "Creating connection"
# (servicenet=True), if running script on a cloud server
connection = cloudfiles.get_connection(username,key,servicenet=True)
print "-- [DONE]"
print "Accessing containers"
originContainer = connection.create_container(originContainerName)
targetContainer = connection.create_container(targetContainerName)
print "-- [DONE]"
return
def main():
cloudConnect()
to_do_list = containerDif(originContainer,targetContainer)
for name in to_do_list:
print ""
processImg(name)
cleanup()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment