python post 使用 PIL 图片请求
python post request using PIL Image
<PIL.WebPImagePlugin.WebPImageFile image mode=RGB size=1600x1600 at 0x1F4E779BA00>
这是文件类型
当我执行 post 请求时,我收到此错误 TypeError: a bytes-like object is required, not 'WebPImageFile'
。如何将其转换为类似字节的对象?
通过将其保存为某种文件格式。你不能只是通过电线推送抽象像素:)
此示例假定远程端可以接受 PNG 文件。
import io
image = ... # However you get your image
bio = io.BinaryIO() # `BinaryIO` is essentially a file in memory
image.save(bio, format="PNG") # Since there is no filename,
# you need to be explicit about the format
bio.seek(0) # rewind the file we wrote into
requests.post(..., files={'file': bio})
要将文件名也发送到 API,您需要将文件指定为元组。
requests.post(..., files={'file': ('image.png', bio)})
<PIL.WebPImagePlugin.WebPImageFile image mode=RGB size=1600x1600 at 0x1F4E779BA00>
这是文件类型
当我执行 post 请求时,我收到此错误 TypeError: a bytes-like object is required, not 'WebPImageFile'
。如何将其转换为类似字节的对象?
通过将其保存为某种文件格式。你不能只是通过电线推送抽象像素:)
此示例假定远程端可以接受 PNG 文件。
import io
image = ... # However you get your image
bio = io.BinaryIO() # `BinaryIO` is essentially a file in memory
image.save(bio, format="PNG") # Since there is no filename,
# you need to be explicit about the format
bio.seek(0) # rewind the file we wrote into
requests.post(..., files={'file': bio})
要将文件名也发送到 API,您需要将文件指定为元组。
requests.post(..., files={'file': ('image.png', bio)})