gpt4 book ai didi

image - 如何在应用引擎中使用 get_serving_url?

转载 作者:太空宇宙 更新时间:2023-11-03 15:30:08 25 4
gpt4 key购买 nike

到目前为止,以下是我的 main.py。

import cgi
import datetime
import logging

from google.appengine.ext import db
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.api import images

logging.getLogger().setLevel(logging.DEBUG)


class Greeting(db.Model):
author = db.UserProperty()
content = db.StringProperty(multiline=True)
imageblob = blobstore.BlobReferebceProperty()
date = db.DateTimeProperty(auto_now_add=True)

class MainPage(webapp.RequestHandler):
def get(self):
self.response.out.write('<html><body>')
query_str = "SELECT * FROM Greeting ORDER BY date DESC LIMIT 10"
greetings = db.GqlQuery (query_str)

for greeting in greetings:
if greeting.author:
self.response.out.write('<b>%s</b> wrote:' % greeting.author.nickname())
else:
self.response.out.write('An anonymous person wrote:')
self.response.out.write("<div><img src='img?img_id=%s'></img>" %
greeting.key())
self.response.out.write(' %s</div>' %
cgi.escape(greeting.content))

self.response.out.write("""
<form action="/sign" enctype="multipart/form-data" method="post">
<div><label>Message:</label></div>
<div><textarea name="content" rows="3" cols="60"></textarea></div>
<div><label>Avatar:</label></div>
<div><input type="file" name="img"/></div>
<div><input type="submit" value="Sign Guestbook"></div>
</form>
</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 Guestbook(webapp.RequestHandler):
def post(self):
greeting = Greeting()
if users.get_current_user():
greeting.author = users.get_current_user()
greeting.content = self.request.get("content")
avatar = get_serving_url(self.request.get("img"), size=None, crop=False)
#avatar = images.crop(self.request.get("img"), 0.0, 0.0,1.0,0.5)
greeting.avatar = db.Blob(avatar)
greeting.put()
self.redirect('/')


application = webapp.WSGIApplication([
('/', MainPage),
('/img', Image),
('/sign', Guestbook)
], debug=True)


def main():
run_wsgi_app(application)


if __name__ == '__main__':
main()

最佳答案

就像 the docs 中所说的那样:

Returns a URL that serves the image. This URL format allows dynamic resizing and cropping, so you don't need to store different image sizes on the server. Images are served with low latency from a highly optimized, cookieless infrastructure.

在您的代码中,您似乎正在使用 get_serving_url() 来构造一个 Blob。这将不起作用,因为 get_serving_url() 返回一个 URL,而不是图像数据。

你似乎也混淆了 blobstore.BlobReferenceProperty和一个 db.BlobProperty -- BlobReferenceProperty 引用上传并存储在 blobstore 中的对象而 BlobProperty 将 blob 数据直接存储在数据存储中。

解决您的问题的更好方法是将您在请求中获得的图像(作为 BlobProperty)存储在您的模型中,然后使用 get_serving_url().

或者,如果您希望存储大图像,请将图像存储在 blobstore 中并使用 BlobReferenceProperty——无论哪种方式,您都应该只使用 get_serving_url() 提供图像,而不是存储图像。

关于image - 如何在应用引擎中使用 get_serving_url?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3864045/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com