阅读背景:

如何在使用PIL修改文件后从文件上传表单上传文件到s3?

来源:互联网 

Here is the code stuff:

这是代码的东西:

app.config['DEBUG']= True


if request.method == 'POST' and request.form['file_submit']:
        print request.form
        print request.files['image']
        if request.files['image']:
            print 'foshhh'
            image_file = request.files['image']
            img = PIL.Image.open(image_file.stream)
            print img
            if request.form['make_transparent']:
                threshold=100
                print 'changin sizesd'
                dist=5
                # np.asarray(img) is read only. Wrap it in np.array to make it modifiable.
                arr=np.array(np.asarray(img))
                r,g,b,a=np.rollaxis(arr,axis=-1)
                mask=((r>threshold)
                    & (g>threshold)
                    & (b>threshold)
                    & (np.abs(r-g)<dist)
                    & (np.abs(r-b)<dist)
                    & (np.abs(g-b)<dist)
                    )
                arr[mask,3]=0
                img=Image.fromarray(arr,mode='RGBA')
            if request.form['change_size']:
                img = Image.open('out.png')
                img.thumbnail(size,Image.ANTIALIAS)
                img.save('out.png',"PNG")

            img.save('out.png',"PNG")
            print os.path.getsize("out.png")   #from answer
            assert os.path.isfile("out.png")   #from answer
            conn = S3Connection(AWS_ACCESS_KEY_ID,AWS_SECRET_ACCESS_KEY)
            b = conn.get_bucket('snappie.watermarks')
            k = Key(b)
            k.key = "test.png"
            k.set_metadata('Content-Type', 'image/png')
            k.set_contents_from_filename("out.png")
            print "got file"
            return redirect("https://s3.amazonaws.com/snappie.watermarks/"+filename)
        else:
            print 'please upload a file to submit the form!'
app.confi



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: