Khanh Tran
Khanh Tran

Reputation: 35

How to Generate Thumbnail for File Stored on Rackspace CloudFiles

I stored images using Rackspace CloudFiles. Now I want to display it as a gallery in the browser. Is there any way to generate thumbnails from my files from the Rackspace side?

Upvotes: 1

Views: 1180

Answers (1)

Kyle Kelley
Kyle Kelley

Reputation: 14144

While there's not a way to do this with the Rackspace or OpenStack Swift libraries, you can create thumbnails for the images programmatically and upload those.

Python - pyrax + PIL (pillow)

For instance, if you're using Python you can use Pillow (PIL) to create thumbnails and pyrax to upload. You'll need to pip install both of these. Prior to installing Pillow, make sure to install system packages for libjpeg and libpng (or follow the instructions in Pillow's installation documentation).

import os
from StringIO import StringIO

import pyrax
from PIL import Image

# Authenticate with Rackspace
pyrax.set_setting("identity_type", "rackspace")
pyrax.set_credential_file(os.path.expanduser("~/.rax_creds"))
cs = pyrax.cloudservers
cf = pyrax.cloudfiles

# Get the container we'll be uploading to
gallery = cf.get_container("gallery")

# Arbitrarily setting a thumbnail size
maxwidth=64
maxheight=64

infile = os.path.expanduser("~/mommapanda.jpg")
# We'll use StringIO to simulate a file
out = StringIO()

im = Image.open(infile)

im.thumbnail((maxwidth,maxheight), Image.ANTIALIAS)
im.save(out, "PNG")

# Back to the start of our "file"
out.seek(0)

gallery.store_object("mommapanda.thumb.png", out.read(),
                     content_type="image/png")

The above code turns this big image

momma panda

into this thumbnail

momma panda thumbnail

and uploads it to a container called gallery on CloudFiles.

Upvotes: 2

Related Questions