gpt4 book ai didi

python - 我应该合并内存 (.py) 中的图像还是 View (HTML) 中的图像?

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

我的facebook app让用户上传图像,选择图像是什么(眼睛、 Nose 、嘴巴或其他 body 部位),然后可以通过按类别选择三个随机图像来组合,这样效果很好,代码看起来不错且可读,但不是很高级:

class CyberFazeHandler(BaseHandler):

def get_random_image(self, category):
fileinfos = FileInfo.all().filter("category =", category)
return fileinfos[random.randint(0, fileinfos.count()-1)]

def get(self):
eyes_image = self.get_random_image(category="eyes")
nose_image = self.get_random_image(category="nose")
mouth_image = self.get_random_image(category="mouth")
eyes_data = None
try:
eyes_data = blobstore.fetch_data(eyes_image.blob.key(), 0, 50000)
except Exception, e:
self.set_message(type=u'error',
content=u'Could not find eyes data for file '+str(eyes_image.key().id())+' (' + unicode(e) + u')')

eyes_img = None

try:
eyes_img = images.Image(image_data=eyes_data)

...现在我只获取 3 个随机图像,然后在模板中组合:

<a href="/file/{{eyes_image.key.id}}"><img src="{{eyes_url}}"></a><br>
<a href="/file/{{nose_image.key.id}}"><img src="{{nose_url}}"></a><br>
<a href="/file/{{mouth_image.key.id}}"><img src="{{mouth_url}}"></a>

是否可以通过发送将三幅图像合并为一幅的合成图像来改进这一点?优点是图像上的所有内容都将同时加载,并且如果下次结果已保存时出现随机化,则图像将已保存。你觉得怎么样?

谢谢(应用程序是apps.facebook.com/cyberfaze,您可以检查我这样做是为了娱乐和学习)

全类同学

class CyberFazeHandler(BaseHandler):

def get_random_image(self, category):
fileinfos = FileInfo.all().filter("category =", category)
return fileinfos[random.randint(0, fileinfos.count()-1)] #optimize

def get(self):
eyes_image = self.get_random_image(category="eyes")
nose_image = self.get_random_image(category="nose")
mouth_image = self.get_random_image(category="mouth")
eyes_data = None
try:
eyes_data = blobstore.fetch_data(eyes_image.blob.key(), 0, 50000)
except Exception, e:
self.set_message(type=u'error',
content=u'Could not find eyes data for file '+str(eyes_image.key().id())+' (' + unicode(e) + u')')

eyes_img = None

try:
eyes_img = images.Image(image_data=eyes_data)
except Exception, e:
self.set_message(type=u'error',
content=u'Could not find eyes img for file '+str(eyes_image.key().id())+' (' + unicode(e) + u')')


nose_data = None
try:
nose_data = blobstore.fetch_data(nose_image.blob.key(), 0, 50000)
except Exception, e:
self.set_message(type=u'error',
content=u'Could not find nose data for file '+str(nose_image.key().id())+' (' + unicode(e) + u')')


nose_img = None

try:
nose_img = images.Image(image_data=nose_data)
except Exception, e:
self.set_message(type=u'error',
content=u'Could not find nose img for file '+str(nose_image.key().id())+' (' + unicode(e) + u')')


mouth_data = None
try:
mouth_data = blobstore.fetch_data(mouth_image.blob.key(), 0, 50000)
except Exception, e:
self.set_message(type=u'error',
content=u'Could not find mouth data for file '+str(eyes_image.key().id())+' (' + unicode(e) + u')')


mouth_img = None

try:
mouth_img = images.Image(image_data=mouth_data)
except Exception, e:
self.set_message(type=u'error',
content=u'Could not find mouth img for file '+str(mouth_image.key().id())+' (' + unicode(e) + u')')


minimum = min(int(eyes_img.width), int(nose_img.width), int(mouth_img.width))

eyes_url = images.get_serving_url(str(eyes_image.blob.key()), size=minimum)
nose_url = images.get_serving_url(str(nose_image.blob.key()), size=minimum)
mouth_url = images.get_serving_url(str(mouth_image.blob.key()), size=minimum)

self.render(u'cyberfaze', minimum=minimum, eyes_image=eyes_image, eyes_url=eyes_url, nose_image=nose_image, nose_url=nose_url, mouth_image=mouth_image, mouth_url=mouth_url, form_url = blobstore.create_upload_url('/upload'),)

重写后,它的工作方式如下:

class CyberFazeHandler(BaseHandler):

def get_random_image(self, category):

q = FileInfo.all()
q.filter('category =', category)
q.filter('randomvalue >=', random.random())
return q.get()

def get_random_image_legacy(self, category):
fileinfos = FileInfo.all().filter('category =', category)
return fileinfos[random.randint(0, fileinfos.count() - 1)]

def get(self):

eyes_image = self.get_random_image(category='eyes')
if not eyes_image:
logging.debug("getting eyes failed, trying legacy method")
eyes_image = self.get_random_image_legacy(category='eyes')
nose_image = self.get_random_image(category='nose')
if not nose_image:
nose_image = self.get_random_image_legacy(category='nose')

mouth_image = self.get_random_image(category='mouth')
if not mouth_image:
mouth_image = self.get_random_image_legacy(category='mouth')

eyes_data = None
try:
eyes_data = blobstore.fetch_data(eyes_image.blob.key(), 0,
50000)
except Exception, e:
self.set_message(type=u'error',
content=u'Could not find eyes data for file '
+ str(eyes_image.key().id()) + ' ('
+ unicode(e) + u')')

eyes_img = None

try:
eyes_img = images.Image(image_data=eyes_data)
except Exception, e:
self.set_message(type=u'error',
content=u'Could not find eyes img for file '
+ str(eyes_image.key().id()) + ' ('
+ unicode(e) + u')')

nose_data = None
try:
nose_data = blobstore.fetch_data(nose_image.blob.key(), 0,
50000)
except Exception, e:
self.set_message(type=u'error',
content=u'Could not find nose data for file '
+ str(nose_image.key().id()) + ' ('
+ unicode(e) + u')')

nose_img = None

try:
nose_img = images.Image(image_data=nose_data)
except Exception, e:
self.set_message(type=u'error',
content=u'Could not find nose img for file '
+ str(nose_image.key().id()) + ' ('
+ unicode(e) + u')')

mouth_data = None
try:
mouth_data = blobstore.fetch_data(mouth_image.blob.key(),
0, 50000)
except Exception, e:
self.set_message(type=u'error',
content=u'Could not find mouth data for file '
+ str(eyes_image.key().id()) + ' ('
+ unicode(e) + u')')

mouth_img = None

try:
mouth_img = images.Image(image_data=mouth_data)
except Exception, e:
self.set_message(type=u'error',
content=u'Could not find mouth img for file '
+ str(mouth_image.key().id()) + ' ('
+ unicode(e) + u')')

minimum = min(int(eyes_img.width), int(nose_img.width),
int(mouth_img.width))

eyes_url = images.get_serving_url(str(eyes_image.blob.key()),
size=minimum)
nose_url = images.get_serving_url(str(nose_image.blob.key()),
size=minimum)
mouth_url = images.get_serving_url(str(mouth_image.blob.key()),
size=minimum)

self.render(
u'cyberfaze',
minimum=minimum,
eyes_image=eyes_image,
eyes_url=eyes_url,
nose_image=nose_image,
nose_url=nose_url,
mouth_image=mouth_image,
mouth_url=mouth_url,
form_url=blobstore.create_upload_url('/upload'),
)

最佳答案

哪个更有效取决于它的使用方式。如果用户将加载大量此类混搭,则将它们作为单独的图像发送更有意义,因为浏览器缓存的图像会更少(a+b+c 图像而不是 a*b*c)。

但是,您的代码存在更严重的性能问题:

def get_random_image(self, category):
fileinfos = FileInfo.all().filter("category =", category)
return fileinfos[random.randint(0, fileinfos.count()-1)]

每次调用此函数时,它都会执行一次计数操作,其时间复杂度为 FileInfo 实体的数量,然后执行一次偏移查询,其时间复杂度为 O(n)偏移量。这是极其缓慢且低效的,并且随着图像数量的增加,这种情况会变得更加严重。

如果您希望图像集很小(少于几千)并且相当恒定,只需将它们存储在代码中,这将比任何其他选项更快。如果集合较大,或在运行时发生更改,请为每个实体分配一个 0 到 1 之间的随机值,并使用如下查询来检索随机选择的实体:

q = FileInfo.all()
q.filter('category =', category)
q.filter('random >=', random.random())
return q.get()

关于python - 我应该合并内存 (.py) 中的图像还是 View (HTML) 中的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7552604/

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