2

When I posted the image load with StringIO, and usd the web.py to obtain the StringIO object, I could not open it with PIL. My POST code is:

# encoding:utf-8
import requests
from StringIO import StringIO

img = open('test.jpg').read()
img = StringIO(img)
files = {'img': img}
baseUrl = r'http://localhost:8080/test'
requests.post(baseUrl, files = files)

My Web.py files index.py

import web
from PIL import Image
urls = ('/test', 'Test')

class Test:

    def GET(self):
        pass
    def POST(self):
        data = web.input()
        # How: Use PIL to open the data?
        img = Image.open(StringIO(data.img)) # report error

Thks!

1 Answers1

3

Solved!

POST files should be written:

# encoding:utf-8
import requests
from StringIO import StringIO
from PIL import Image

f = StringIO()
img = Image.open('test.jpg')
img.save(f, "JPEG")
f.seek(0)
files = {'img': f}
baseUrl = r'http://localhost:8080/test'
requests.post(baseUrl, files = files)