gpt4 book ai didi

python - POST StringIO 对象并使用 PIL 打开

转载 作者:太空宇宙 更新时间:2023-11-04 01:06:23 25 4
gpt4 key购买 nike

当我用StringIO加载图片,用web.py获取StringIO对象时,用PIL打不开。我的 POST 代码是:

# 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)

我的Web.py文件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

谢谢!

最佳答案

解决了!

POST 文件应该这样写:

# 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)

关于python - POST StringIO 对象并使用 PIL 打开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30158814/

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