在Python中显示Blob图像(App Engine) [英] Displaying Blob Images in Python (App Engine)

查看:236
本文介绍了在Python中显示Blob图像(App Engine)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将图片显示在网页上。

这些是处理程序:

  class disp_image(webapp.RequestHandler):
def get(self):
key = self.request.get('key')
image = Images.get(key)
if image:
self.response.headers ['Content-Type'] =image / png
return self.response.out.write(images.image)
else:
self.response.headers ['Content-Type'] =image / png
return self.response.out.write(/ static / unknown.gif)

类配置文件(MainHandler):
def get(self):
如果self.user:
self.render('profile.html',username = self.user.name,email = self.user.email,first_name = self.user.first_name,last_name = self.user.last_name,country = self.user.country,prov_state = self.user.prov_state,city_town = self.user.city_town)
否则:
self.redirect('/ register')

class Change_Profile_Image(MainHandler):
def get(self):
如果self.user:
self.render('change_profile_image.html',username = self.user.name ,firstname = self.user.first_name)
else:
self.render('change_profile_image.html')

def post(self):
images =图片()
imageupl = self.request.get('img')
images.image = db.Blob(imageupl)
images.put()
self.redirect(' / profile')

db的内容如下:

  class图像(db.Model):
image = db.BlobProperty()

class User(db.Model):
name = db.StringProperty(required = True)
pw_hash = db.StringProperty(required = True)
email = db.StringProperty(required = True)
first_name = db.StringProperty )
last_name = db.StringProperty()
country = db.StringProperty()
prov_state = d b.StringProperty()
city_town = db.StringProperty()
clan = db.StringProperty()
handle = db.StringProperty()

表格:

 < form enctype = multipart / form-datamethod =post> 
< input type =filename =img/>
< / form>

我的模板替换位代码:

 < img src =/ disp?key = {{image}}/>< / img> 


解决方案

图像键和图像二进制数据。

模板

您应该将图像键(字符串)您的模板:

 < img src =/ disp?key = {{image_key}}/> 

即如果您只是将image.key()传递给您的模板,您的最终html应该如下所示:

 < img src = / disp?key = AC3CK3333KCCK2K213/> 

您可以将其视为一个两步过程。你呈现你的模板。用户浏览器呈现html并执行到您的图像处理程序。然后,图像处理程序发送二进制数据。



(顺便说一句,我觉得你用< img ..关闭你的html标签。 。/> < img ...>< / img> ,不是< img ... />< / img>

图像处理程序 b

在图像处理程序disp_image中,您应该返回图像的二进制数据。它看起来像你的第一个条件是好的,但我认为你的其他条件将只返回文本/static/unknown.gif,而不是gif文件的实际二进制数据。

  else:
self.response.headers ['Content-Type'] =image / png
return self.response.out.write( /static/unknown.gif)

您应该将数据存储中的unknown.gif存储并引用或者您应该将/ static / unknown.gif url写入您的模板以代替/ disp?key = ...



最后,测试图像处理程序是否正常工作的最简单方法是插入 < domain> / disp?key = ... 到您的浏览器网址栏中,它应该加载所需的图片。

I am unable to get the image to display on the page. I can store it just fine.

These are the handlers:

class disp_image(webapp.RequestHandler):
    def get(self):
        key = self.request.get('key')
        image = Images.get(key)
        if image:
            self.response.headers['Content-Type'] = "image/png"
            return self.response.out.write(images.image)
        else:
            self.response.headers['Content-Type'] = "image/png"
            return self.response.out.write("/static/unknown.gif")

class Profile(MainHandler):
    def get(self):      
        if self.user:
            self.render('profile.html', username = self.user.name, email = self.user.email, first_name = self.user.first_name, last_name = self.user.last_name, country = self.user.country, prov_state = self.user.prov_state, city_town = self.user.city_town)
        else:
            self.redirect('/register')

class Change_Profile_Image(MainHandler):
    def get(self):
        if self.user:
            self.render('change_profile_image.html', username = self.user.name, firstname=self.user.first_name)
        else:
            self.render('change_profile_image.html')

    def post(self):
        images = Images()
        imageupl = self.request.get('img')
        images.image = db.Blob(imageupl)
        images.put()
        self.redirect('/profile')

db stuff is a follows:

class Images(db.Model):
    image = db.BlobProperty()

class User(db.Model):
    name = db.StringProperty(required=True)
    pw_hash = db.StringProperty(required=True)
    email = db.StringProperty(required=True)
    first_name = db.StringProperty()
    last_name = db.StringProperty()
    country = db.StringProperty()
    prov_state = db.StringProperty()
    city_town= db.StringProperty()
    clan= db.StringProperty()
    handle= db.StringProperty()

The form:

<form enctype="multipart/form-data" method="post">
       <input type="file" name="img" />
</form>

My template substitution bit of code:

<img src="/disp?key={{image}}" /></img>

解决方案

For one, I think you're mixing up the image key and the image binary data.

The Template

You should be writing the image key (string) to your template:

 <img src="/disp?key={{image_key}}" />

i.e. your final html should look like the following if you just pass image.key() to your template:

 <img src="/disp?key=AC3CK3333KCCK2K213" />

You can think of it as a two step process. You render your template. The user browser renders the html and does a get to your image handler. The image handler then sends the binary data.

(Btw, I was under the impression you close your html tags with either <img ... /> or <img ...></img>, not both <img .../></img>)

Image Handler

In your image handler, disp_image, you should be returning the binary data of the image. It looks like you first condition is okay, but I think your else condition will just return the text "/static/unknown.gif" and not the actual binary data of the gif file.

    else:
        self.response.headers['Content-Type'] = "image/png"
        return self.response.out.write("/static/unknown.gif")

You should either store unknown.gif in your datastore and refer to it by key or you should be writing the "/static/unknown.gif" url to your template in lieu of "/disp?key=..."

Lastly, the simplest way to test whether your image handler is working is when you plug in <domain>/disp?key=... into your browser URL bar, it should load the desired image.

这篇关于在Python中显示Blob图像(App Engine)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆