【问题标题】:How to Generate Thumbnail for File Stored on Rackspace CloudFiles如何为存储在 Rackspace CloudFiles 上的文件生成缩略图
【发布时间】:2013-11-06 23:36:08
【问题描述】:

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

【问题讨论】:

  • 你使用什么语言?
  • 答案是否定的。云文件 API [bit.ly/1iCZi7e] 不提供此类选项。这将非常方便,因为有时脚本必须下载全尺寸图像以动态生成缩略图时会出现相当大的延迟。如果您确实需要即时生成缩略图,那么我建议使用服务器端脚本的 URL 设置缩略图的 src 的方法,该脚本会输出缩略图。确保脚本也在与网页不同的域下运行,以避免进程占用当前用户的会话。

标签: image-processing thumbnails rackspace cloudfiles


【解决方案1】:

虽然没有办法使用 Rackspace 或 OpenStack Swift 库执行此操作,但您可以通过编程方式为图像创建缩略图并上传。

Python - pyrax + PIL(枕头)

例如,如果您使用 Python,则可以使用 Pillow (PIL) 创建缩略图和 pyrax to upload。你需要pip install 这两个。在安装 Pillow 之前,请务必安装 libjpeg 和 libpng 的系统包(或按照 Pillow 的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")

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

进入这个缩略图

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

【讨论】:

    猜你喜欢
    • 2014-01-31
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 2012-05-31
    相关资源
    最近更新 更多