小编zur*_*roc的帖子

使用PIL调整png图像大小会失去透明度

我用这个方法来调整png图像的大小:

将PNG转换为预乘alpha的方法

但这张图片仍然失去透明度:

import Image, numpy

def resize(filename, img,height, width):
    if filename.endswith(".png"):
        img = img.convert('RGBA')
        premult = numpy.fromstring(img.tostring(), dtype=numpy.uint8)
        alphaLayer = premult[3::4] / 255.0
        premult[::4] *= alphaLayer
        premult[1::4] *= alphaLayer
        premult[2::4] *= alphaLayer
        img = Image.fromstring("RGBA", img.size, premult.tostring())
    img = img.resize((height,width), Image.ANTIALIAS)
    return img
Run Code Online (Sandbox Code Playgroud)

http://i.stack.imgur.com/oPV5q.pnghttp://i.stack.imgur.com/jzmT9.png

python python-imaging-library

5
推荐指数
1
解决办法
5487
查看次数

如何在integer_id中使用ndb键?

我看到了文档 https://developers.google.com/appengine/docs/python/ndb/keyclass#Key_integer_id

返回最后一个(kind,id)对中的整数id,如果密钥具有字符串id或不完整,则返回None.

看,我认为一个键的id可以是一个int; 所以我写

    r = ndb.Key(UserSession, int(id)).get()
    if r:
        return r.session
Run Code Online (Sandbox Code Playgroud)

但是dev_server.py会一直提升

  File "/home/bitcoin/down/google_appengine/google/appengine/datastore/datastore_stub_util.py", line 346, in CheckReference
    raise datastore_errors.BadRequestError('missing key id/name')
BadRequestError: missing key id/name
Run Code Online (Sandbox Code Playgroud)

我支持int(id) - > str(id)

似乎是正确的

所以我的问题是,如何使用整数_id的ndb键?

这个模型是

class UserSession(ndb.Model):
    session = ndb.BlobProperty()
Run Code Online (Sandbox Code Playgroud)

google-app-engine app-engine-ndb google-cloud-datastore

2
推荐指数
1
解决办法
3249
查看次数

ndb get&get_or_insert怎么用?(总是提高例外)

我写代码如下

from google.appengine.ext import ndb

__metaclass__ = type


class UserSession(ndb.Model):
    session = ndb.BlobProperty()


class KV:
    @staticmethod
    def get(id):
        r = ndb.Key(UserSession, int(id)).get()
        if r:
            return r.session

    @staticmethod
    def set(id, value):
        return UserSession.get_or_insert(int(id), session=value)

    @staticmethod
    def delete(id):
        ndb.Key(UserSession, int(id)).delete()
Run Code Online (Sandbox Code Playgroud)

我写的地方

id = 1
key = ndb.Key(UserSession, int(id))
UserSession.get_or_insert(key, session=1)
Run Code Online (Sandbox Code Playgroud)

sdk加注

TypeError: name must be a string; received Key('UserSession', 1)
Run Code Online (Sandbox Code Playgroud)

当我打电话给KV.get()

sdk加注

文件"/home/bitcoin/42btc/zapp/_plugin/auth/model/gae/user.py",第14行,获取

    r = ndb.Key(UserSession,int(id)).get()
Run Code Online (Sandbox Code Playgroud)

...

BadRequestError:缺少密钥ID /名称

那么,如何使用NDB?

google-app-engine app-engine-ndb google-cloud-datastore

1
推荐指数
1
解决办法
2732
查看次数