简体   繁体   English

使用Python Base64压缩GAE Blob图像?

[英]Compress GAE Blob images using Python Base64?

I'm pulling a JSON request of 20 random thumbnails to an iPhone application. 我将20个随机缩略图的JSON请求拉到iPhone应用程序。 At the moment I'm simply including the image thumb URLs in the JSON array (see below), then the iPhone goes out to get each image. 目前,我只是将图像缩略图URL包含在JSON数组中(见下文),然后iPhone出去获取每张图像。 This is really slow. 这真的很慢。

Original JSON Request: 原始JSON请求:

{
  "item_list": [
    {
      "item_name": "Item One", 
      "user_item_thumb": "http://localhost:8080/i/agpwaGluZ28tYXBwcg4LEghJdGVtVXNlchgPDA/67x67", 
    }, 
    {
      "item_name": "Item Two", 
      "user_item_thumb": "http://localhost:8080/i/agpwaGluZ28tYXBwcg4LEghJdGVtVXNlchgQDA/67x67", 
    }, 
    {
      "item_name": "Item Three", 
      "user_item_thumb": "http://localhost:8080/i/agpwaGluZ28tYXBwcg4LEghJdGVtVXNlchgRDA/67x67", 
    }
  ]
}

So, what I was thinking was using Base64 on the image data and actually including them in the JSON request, so the iPhone only needs one request instead of 21 requests. 因此,我当时想在图像数据上使用Base64,实际上将它们包括在JSON请求中,因此iPhone仅需要一个请求,而不是21个请求。 Make sense? 说得通?

So, how do I do this? 那么,我该怎么做呢?
I tried to simply print the below to JSON, but those are the full size images, I need to push a Base64 version of the Thumbnails. 我尝试将以下内容简单地打印为JSON,但是这些是全尺寸图片,我需要推送Base64版本的Thumbnails。

Not working: 无法运作:

f = item.image f = item.image
f_enc = f.encode('base64') f_enc = f.encode('base64')

This is how I get my thumbs at the moment, just creating them on the fly. 这就是我现在获得拇指的方式,只是即时创建它们。

http://localhost:8080/i/agpwaGluZ28tYXBwcg4LEghJdGVtVXNlchgSDA/67x67 HTTP://本地主机:8080 / I / agpwaGluZ28tYXBwcg4LEghJdGVtVXNlchgSDA / 67x67

This is what renders the above image request: 这就是呈现上述图像请求的原因:

class Image(webapp.RequestHandler):
    def get(self, image_id):
        user = db.get(image_id)

        if user.image:
            picture = user.image
            self.response.headers['Content-Type'] = "image/png"
            self.response.out.write(picture)
        else:
            self.response.out.write("No image")

Any ideas would be amazing. 任何想法都将是惊人的。
If there's a better way to do this, I'm all ears. 如果有更好的方法可以做到这一点,我全神贯注。

My problems: 我的问题:
- The iPhone is slow pulling in each of these 20 images -iPhone缓慢拉入这20张图像中的每张
- The images are random, so caching is probably not an option. -图片是随机的,因此缓存可能不是一种选择。
- Is Base64 the way to go? -要走Base64吗?

Thanks, 谢谢,
Danny 丹尼

Yes, encoding the images directly using base64 seems like a reasonable approach, given they're only thumbnails. 是的,鉴于它们只是缩略图,因此直接使用base64直接编码图像似乎是一种合理的方法。 This is similar to using data: URLs in a webpage for the same reason. 这与使用data:网页中的URL出于相同的原因类似。

Doing it should be as simple as calling the same Images API transforms you do in your regular code to generate the thumbnail, then serializing the resulting output as base64. 这样做应该像调用常规代码中执行的相同Images API转换一样简单,以生成缩略图,然后将结果输出序列化为base64。 You probably want to cache the result in memcache - in both handlers - to prevent redundant calls to the Images API. 您可能希望在两个处理程序中将结果缓存在memcache中,以防止对Images API的多余调用。 Alternately, you could calculate the thumbnail when the image is first uploaded, and store it alongside the full-size image. 或者,您可以在首次上传图像时计算缩略图,并将其存储在全尺寸图像旁边。

Perhaps you just need to call read() 也许您只需要调用read()

encoded = item.image.read().encode('base64')

If you're dealing with images of any great size, you'll want to encode in chunks (ie read(4096) multiple times, encoding each piece). 如果要处理任何大尺寸的图像,则需要按块进行编码(即多次read(4096) ,对每块进行编码)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM