1

我使用Rackspace CloudFiles存储图像。现在我想在浏览器中将其显示为画廊。有什么方法可以从 Rackspace 端的文件生成缩略图?

4

1 回答 1

2

虽然没有办法使用 Rackspace 或 OpenStack Swift 库来做到这一点,但您可以通过编程方式为图像创建缩略图并上传它们。

Python - pyrax + PIL(枕头)

例如,如果您使用 Python,您可以使用Pillow (PIL) 创建缩略图和使用 pyrax 上传. 你需要pip install这两个。在安装 Pillow 之前,请确保安装 libjpeg 和 libpng 的系统包(或按照 Pillow安装文档中的说明进行操作)。

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")

上面的代码变成了这个大图

熊猫妈妈

进入这个缩略图

熊猫妈妈缩略图

并将其上传到 CloudFiles 上名为 gallery 的容器。

于 2013-10-28T11:39:24.563 回答