フラスコ(パイソン)を使用して、Googleのデータストアに格納する画像

StackOverflow https://stackoverflow.com/questions/4022974

質問

私は、GoogleのApp Engine上でフラスコを使用していますが、必死にこれを解決する助けを探しています。

- :このような何かを行うべきであるBlobPropertyを使用して、データストア内の画像を保存するのGAEのドキュメント会談、
class MyPics(db.Model):
      name=db.StringProperty()
      pic=db.BlobProperty()

さて、画像はこれを行うことによって、データストアに格納する必要があります: -

def storeimage():
    pics=MyPics()
    pics.name=request.form['name']
    uploadedpic=request.files['file']  #where file is the fieldname in the form of the                
                                        file uploaded
    pics.pic=db.Blob(uploadedpic)
    pics.put()
    redirect ... etc etc

しかし、これを実行することができません。私が得るようdb.Blobは、文字列を受け付けますが、これで...缶誰かのヘルプファイル記憶オブジェクト私に与えられました。また、誰もがアップロードした後、画像バックをストリーミングする方法で私をほのめかすことができれば。

役に立ちましたか?

解決

[OK]をので、これは私が最終的にそれを解決する方法である: -

@userreg.route('/mypics',methods=['GET','POST'])
def mypics():    
   if request.method=='POST':
      mydata=MyPics()
      mydata.name=request.form['myname']
      file=request.files['file']
      filedata=file.read()
      if file:
         mydata.pic=db.Blob(filedata)
      mydata.put()
      return redirect(url_for('home'))
   return render_template('mypicform.html')

上記格納データストア内のブロブとしてファイルと、それは以下FUNCによって取得することができる: -

@userreg.route('/pic/<name>')
def getpic(name):
     qu=db.Query(MyPics).filter('name =',name).get()
     if qu.pic is None:
         return "hello"
     else:
         mimetype = 'image/png'
         return current_app.response_class(qu.pic,mimetype=mimetype,direct_passthrough=False)

他のヒント

私はモデルを次のようしている。

class Picture(db.Model):    
    data = db.BlobProperty()
    ext = db.StringProperty()
    content_type = db.StringProperty()

、次のコードを使用して保存

def create_picture():
    if request.files['file']:                       
        picture.data = request.files['file'].read()
        picture.ext = request.files['file'].filename.rsplit('.', 1)[1]
        picture.content_type = request.files['file'].content_type
        picture.put()
        flash(u'File uploaded', 'correct')
        return redirect(url_for("edit_picture", id=picture.key()))
    else:
        return render_template('admin/pictures/new.html', title=u'Upload a picture', message=u'No picture selected')

:次のコードを使用することができますサムネールをレンダリングするために

@frontend.route('/thumbnail/<id>/<width>x<height>.<ext>')
def thumbnail(id, width, height, ext):
    key = db.Key(id)
    picture = Picture.get(key)
    img = images.Image(picture.data)

    if width != '0':
        w = int(width)
    else:
        w = img.width

    if height != '0':
        h = int(height)
    else:
        h = img.height

    if img.height > h and h != 0:
        w = (int(width) * img.width) / img.height;

    if img.width > w:
        h = (int(height)  * img.height) / img.width;

    thumb = images.resize(picture.data, width=w, height=h)    
    response = make_response(thumb)
    response.headers['Content-Type'] = picture.content_type
    return response
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top