Please help using the Images Python API (cannot write or read image from datastore with simple script)

3 views
Skip to first unread message

Zeynel

unread,
Dec 3, 2010, 11:12:03 PM12/3/10
to Google App Engine
Hello:

I have been trying to learn how to save and retrieve an image from
datastore with no success so far. I copied the code from the
documentation and tried to adapt it to my script. I only see a broken
image link and I don't see the blob written to datastore in datastore
viewer.

My question at StackOverflow is here:
http://stackoverflow.com/questions/4351375/how-to-use-images-python-api-correctly-to-display-an-avatar-with-user-comments


Thank your for your help. This is my model:

class User(db.Model):
userEmail = db.StringProperty()
userName = db.StringProperty()
userLatestComment = db.StringProperty(multiline=True)
userScore = db.IntegerProperty(default=0)
avatar = db.BlobProperty()

And this is the script:

from model import User
from model import Comment
from model import Venue

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class MainPage(webapp.RequestHandler):
def get(self):
siteUser = users.get_current_user()
greeting = None
if siteUser:
greeting = ("Welcome, %s! (<a href=\"%s\">sign out</a>)" %
(siteUser.nickname(),
users.create_logout_url("/")))
else:
greeting = ("<a href=\"%s\">Sign in or register</a>" %
users.create_login_url("/"))

self.response.out.write(greeting)

self.response.out.write("""
<form action="/avatar-save" enctype="multipart/form-
data" method="post">
<div><label>Avatar:</label></div>
<div><input type="file" name="img"/></div>
</form>
</body>
</html>""")

query = User.all()
query.filter("userEmail =", "a...@example.com")
results = query.fetch(10)
self.response.out.write("""<html><head><style>
body {font-size: small;
font-family: Verdana,
Helvetica, sans-serif;
}</style>
</head><body><ol>""")
for result in results:
self.response.out.write("<li>")
self.response.out.write("<b>%s</b> %s " %
(result.userName, result.userLatestComment))
self.response.out.write("<div><img src='img?img_id=%s'></
img>" % result.key())
self.response.out.write("</li>")
self.response.out.write("</ol></body></html>")

class Image (webapp.RequestHandler):
def get(self):
greeting = db.get(self.request.get("img_id"))
if greeting.avatar:
self.response.headers['Content-Type'] = "image/png"
self.response.out.write(greeting.avatar)
else:
self.response.out.write("No image")

class AvatarSave(webapp.RequestHandler):
def post(self):
q = User.all()
q.filter("userEmail =", "a...@example.com")
qTable = q.fetch(1)
if qTable:
for row in qTable:
avatar = images.resize(self.request.get("img"), 32,
32)
row.avatar = db.Blob(avatar)
db.put(qTable)
else:
self.response.out.write("user not found")

self.redirect('/')


application = webapp.WSGIApplication(
[('/', MainPage),
('/img', Image),
('/avatar-save',
AvatarSave),
],
debug=True)

def main():
run_wsgi_app(application)

if __name__ == "__main__":
main()

Zeynel

unread,
Dec 4, 2010, 11:11:35 AM12/4/10
to Google App Engine
Issue resolved: http://stackoverflow.com/questions/4353939/how-to-write-an-image-to-datastore

On Dec 3, 11:12 pm, Zeynel <azeyn...@gmail.com> wrote:
> Hello:
>
> I have been trying to learn how to save and retrieve an image from
> datastore with no success so far. I copied the code from the
> documentation and tried to adapt it to my script. I only see a broken
> image link and I don't see the blob written to datastore in datastore
> viewer.
>
> My question at StackOverflow is here:http://stackoverflow.com/questions/4351375/how-to-use-images-python-a...
Reply all
Reply to author
Forward
0 new messages