通过 Python 个请求发送 PIL 图片
Sending a PIL image through Python requests
我需要创建一个图像并将其保存在数据库中。我正在使用 python Pillow 创建它并将其保存为 JPEG。
image = Process().mandelbrot(width, height, max_iter)
mem_file = BytesIO()
image.save(mem_file, "JPEG", quality=100)
mem_file.seek(0)
创建后,我试图通过python-requests
将其保存在数据库中
form_data = {'image': ('mandelbrot'+str(uuid.uuid4().hex), mem_file)}
requests.post(url='http://database:5000/images', files=form_data)
我正在尝试发送表单数据请求,但是当我检查进入我的数据库的数据时,找不到该文件,它的值为 None
ImmutableMultiDict([('image', <FileStorage: 'mandelbrot8a9275e838a04a0f94b731c47bc6bdd4' (None)>)])
有谁知道如何使用表单数据 mimetype 发送 PIL 文件抛出 python 请求而不获取具有 None 值的文件?
正如@mugiseyebrows 所说我做错了,表单数据应该如下
form_data= {"image": ("image.jpeg", mem_file, "image/jpeg")}
这样就可以发送 mime 类型,我可以使用表单数据请求
发送我的图像 throw python 请求
洞口代码变成了下面
image = Process().mandelbrot(width, height, max_iter)
mem_file = BytesIO()
image.save(mem_file, "PNG", quality=100)
mem_file.seek(0)
id = 'mandelbrot'+str(uuid.uuid4().hex)
requests.post(url='http://database:5000/images', files={'image': ('file.PNG', mem_file, 'image/png')}, params={'id': id})
我需要创建一个图像并将其保存在数据库中。我正在使用 python Pillow 创建它并将其保存为 JPEG。
image = Process().mandelbrot(width, height, max_iter)
mem_file = BytesIO()
image.save(mem_file, "JPEG", quality=100)
mem_file.seek(0)
创建后,我试图通过python-requests
将其保存在数据库中form_data = {'image': ('mandelbrot'+str(uuid.uuid4().hex), mem_file)}
requests.post(url='http://database:5000/images', files=form_data)
我正在尝试发送表单数据请求,但是当我检查进入我的数据库的数据时,找不到该文件,它的值为 None
ImmutableMultiDict([('image', <FileStorage: 'mandelbrot8a9275e838a04a0f94b731c47bc6bdd4' (None)>)])
有谁知道如何使用表单数据 mimetype 发送 PIL 文件抛出 python 请求而不获取具有 None 值的文件?
正如@mugiseyebrows 所说我做错了,表单数据应该如下
form_data= {"image": ("image.jpeg", mem_file, "image/jpeg")}
这样就可以发送 mime 类型,我可以使用表单数据请求
发送我的图像 throw python 请求洞口代码变成了下面
image = Process().mandelbrot(width, height, max_iter)
mem_file = BytesIO()
image.save(mem_file, "PNG", quality=100)
mem_file.seek(0)
id = 'mandelbrot'+str(uuid.uuid4().hex)
requests.post(url='http://database:5000/images', files={'image': ('file.PNG', mem_file, 'image/png')}, params={'id': id})