如何使用带有请求的多部分POST发布Image对象?(How do I post an Image object with a multi
我正在尝试将我编写的一些javascript代码转换为Python ,但我仍然坚持传递数据b / t PIL和Requests对象。
我的python脚本将图像下载到内存:
from PIL import Image import urllib2 import cStringIO def fetch_image_to_memory(url): req = urllib2.Request(url, headers={ 'User-Agent': "Mozilla / 5.0 (X11; U; Linux i686) Gecko / 20071127 Firefox / 2.0.0.11"}) con = urllib2.urlopen(req) imgData = con.read() return (cStringIO.StringIO(imgData))然后我想将它添加到POST操作的form data中。 当文件在磁盘上时,此代码成功:
from requests_toolbelt import MultipartEncoder import requests url = 'https://us-west-2.api.scaphold.io/graphql/some-gql-endpoint' multipart_data = MultipartEncoder( fields={ 'query':'some-graphql-specific-query-string', 'variables': '{ "input": {"blobFieldame": "myBlobField" }}', ## `variables.input.blobFieldame` must hold name ## of Form field w/ the file to be uploaded 'type': 'application/json', 'myBlobField': ('example.jpg', img, 'image/jpeg') } ) req_headers = {'Content-Type':multipart__type, 'Authorization':'Bearer secret-bearer-token'} r = requests.post(url, data=multipart_data, headers=req_headers)但是,尝试从fetch_image_to_memory函数传入Image对象时:
'myBlobField': ('example.jpg', image_object, 'image/jpeg')......我收到错误:
Traceback (most recent call last): File "test-gql.py", line 8, in <module> 'myBlobField': img File "/home/bmp/code/wayhome/python-phash/requests_toolbelt/multipart/encoder.py", line 119, in __init__ self._prepare_parts() File "/home/bmp/code/wayhome/python-phash/requests_toolbelt/multipart/encoder.py", line 240, in _prepare_ parts self.parts = [Part.from_field(f, enc) for f in self._iter_fields()] File "/home/bmp/code/wayhome/python-phash/requests_toolbelt/multipart/encoder.py", line 488, in from_fiel d body = coerce_data(field.data, encoding) File "/home/bmp/code/wayhome/python-phash/requests_toolbelt/multipart/encoder.py", line 466, in coerce_da ta return CustomBytesIO(data, encoding) File "/home/bmp/code/wayhome/python-phash/requests_toolbelt/multipart/encoder.py", line 529, in __init__ buffer = encode_with(buffer, encoding) File "/home/bmp/code/wayhome/python-phash/requests_toolbelt/multipart/encoder.py", line 410, in encode_wi th return (encoding) AttributeError: 'JpegImageFile' object has no attribute 'encode'我从open()文档中知道它返回一个类型为file的对象,但是我在PIL看到的从Image转换为file的唯一方法是使用save()将它写入磁盘。 我可以写入磁盘,但我宁愿避免这一步,因为我正在处理大量图像。
是否可以将Image对象转换为file类型? 或者其他类似效果的解决方法?
I'm trying to convert some javascript code I wrote to Python, but am getting stuck on passing data b/t PIL and Requests objects.
My python script downloads an image to memory:
from PIL import Image import urllib2 import cStringIO def fetch_image_to_memory(url): req = urllib2.Request(url, headers={ 'User-Agent': "Mozilla / 5.0 (X11; U; Linux i686) Gecko / 20071127 Firefox / 2.0.0.11"}) con = urllib2.urlopen(req) imgData = con.read() return (cStringIO.StringIO(imgData))I'd then like to add it to the form data for a POST operation. This code succeeds when the file is on disk:
from requests_toolbelt import MultipartEncoder import requests url = 'https://us-west-2.api.scaphold.io/graphql/some-gql-endpoint' multipart_data = MultipartEncoder( fields={ 'query':'some-graphql-specific-query-string', 'variables': '{ "input": {"blobFieldame": "myBlobField" }}', ## `variables.input.blobFieldame` must hold name ## of Form field w/ the file to be uploaded 'type': 'application/json', 'myBlobField': ('example.jpg', img, 'image/jpeg') } ) req_headers = {'Content-Type':multipart__type, 'Authorization':'Bearer secret-bearer-token'} r = requests.post(url, data=multipart_data, headers=req_headers)However, when trying to pass in an Image object from the fetch_image_to_memory function:
'myBlobField': ('example.jpg', image_object, 'image/jpeg')...I get the error:
Traceback (most recent call last): File "test-gql.py", line 8, in <module> 'myBlobField': img File "/home/bmp/code/wayhome/python-phash/requests_toolbelt/multipart/encoder.py", line 119, in __init__ self._prepare_parts() File "/home/bmp/code/wayhome/python-phash/requests_toolbelt/multipart/encoder.py", line 240, in _prepare_ parts self.parts = [Part.from_field(f, enc) for f in self._iter_fields()] File "/home/bmp/code/wayhome/python-phash/requests_toolbelt/multipart/encoder.py", line 488, in from_fiel d body = coerce_data(field.data, encoding) File "/home/bmp/code/wayhome/python-phash/requests_toolbelt/multipart/encoder.py", line 466, in coerce_da ta return CustomBytesIO(data, encoding) File "/home/bmp/code/wayhome/python-phash/requests_toolbelt/multipart/encoder.py", line 529, in __init__ buffer = encode_with(buffer, encoding) File "/home/bmp/code/wayhome/python-phash/requests_toolbelt/multipart/encoder.py", line 410, in encode_wi th return (encoding) AttributeError: 'JpegImageFile' object has no attribute 'encode'I know from the open() docs that it returns an object of type file, but the only way I can see in PIL to convert from Image to file is by using save(), which writes it to the disk. I can write to disk, but I'd rather avoid the step, since I am handling a lot of images.
Is it possible to convert the Image object to file type? Or some other workaround with similar effect?
最满意答案
MultipartEncoder可以采用字节字符串或文件对象,但两者都不是PIL图像对象。
您必须首先创建内存中的文件对象:
from io import BytesIO image_file = BytesIO() img.save(image_file, "JPEG") image_file.seek(0)然后在帖子中使用image_file :
'myBlobField': ('example.jpg', image_file, 'image/jpeg')MultipartEncoder can take a byte string or a file object, but a PIL image object is neither.
You'd have to create an in-memory file object first:
from io import BytesIO image_file = BytesIO() img.save(image_file, "JPEG") image_file.seek(0)then use image_file in the post:
'myBlobField': ('example.jpg', image_file, 'image/jpeg')#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
推荐阅读
留言与评论(共有 17 条评论) |
本站网友 为什么上不了网 | 12分钟前 发表 |
'image/jpeg') } ) req_headers = {'Content-Type' | |
本站网友 手机卡数据恢复 | 10分钟前 发表 |
when trying to pass in an Image object from the fetch_image_to_memory function | |
本站网友 作家冯唐 | 17分钟前 发表 |
headers=req_headers) However | |
本站网友 国产什么奶粉好 | 0秒前 发表 |
in encode_wi th return (encoding) AttributeError | |
本站网友 如何投资房地产 | 28分钟前 发表 |
headers={ 'User-Agent' | |
本站网友 千山暮 | 12分钟前 发表 |
line 240 | |
本站网友 百度快照查询 | 1分钟前 发表 |
"Mozilla / 5.0 (X11; U; Linux i686) Gecko / 20071127 Firefox / 2.0.0.11"}) con = urllib2.urlopen(req) imgData = con.read() return (cStringIO.StringIO(imgData)) I'd then like to add it to the form data for a POST operation. This code succeeds when the file is on disk | |
本站网友 生男生女秘诀 | 24分钟前 发表 |
## `variables.input.blobFieldame` must hold name ## of Form field w/ the file to be uploaded 'type' | |
本站网友 商标数据库 | 28分钟前 发表 |
headers={ 'User-Agent' | |
本站网友 短贷宝 | 29分钟前 发表 |
encoding) File "/home/bmp/code/wayhome/python-phash/requests_toolbelt/multipart/encoder.py" | |
本站网友 宜搜首页 | 17分钟前 发表 |
data=multipart_data | |
本站网友 泸州二手房 | 28分钟前 发表 |
line 240 | |
本站网友 孕妇梦见下雪 | 5分钟前 发表 |
因为我正在处理大量图像 | |
本站网友 昆明美食 | 13分钟前 发表 |
image_file | |
本站网友 傲然屹立 | 3分钟前 发表 |
因为我正在处理大量图像 | |
本站网友 质数币 | 16分钟前 发表 |
'image/jpeg') } ) req_headers = {'Content-Type' |