当我试图上传一个图片到imgur时,我得到了以下错误。
b'{"data":{"error":"Invalid URL (<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=1600x6495 at 0x10E726050>)","request":"\\/3\\/upload","method":"POST"},"success":false,"status":400}'下面给出了我的代码。修改了客户ID。
#!/usr/bin/env python3
import io
from PIL import Image
import requests
import json
import base64
url = "http://www.tallheights.com/wp-content/uploads/2016/06/background_purple.jpg"
r = requests.get(url)
image = Image.open(io.BytesIO(r.content))
imagestring = str(image)
url = 'https://api.imgur.com/3/upload'
body = {'type':'file','image': imagestring , 'name' : 'abc.jpeg'}
headers = {'Authorization': 'Client-ID <redacted>'}
req = requests.post(url, data=body, headers=headers)
print (req.content)我的代码是用Python3编写的,我没有使用Imgur提供的官方客户库,原因有两个。
发布于 2020-03-13 03:13:21
这样做是可行的:
# imports
import requests
import io
from PIL import Image
# Get Image from request
img_response = requests.get(
"https://encrypted-tbn0.gstatic.com/images? q=tbn%3AANd9GcTVYP3ZsF72FSKPzxJghYkjz_-a1op7YxBK45O0Y4nTjAQ7PZKD"
)
# Some Pillow Resizing
img = Image.open(io.BytesIO(img_response.content))
img_width, img_height = img.size
crop = min(img.size)
square_img = img.crop(
(
(img_width - crop) // 2,
(img_height - crop) // 2,
(img_width + crop) // 2,
(img_height + crop) // 2,
)
) # Square Image is of type Pil Image
imgByteArr = io.BytesIO()
square_img.save(imgByteArr, format="PNG")
imgByteArr = imgByteArr.getvalue()
url = "https://api.imgur.com/3/image"
payload = {"image": imgByteArr}
headers = {"Authorization": "Client-ID xxxxxxxxx"}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text.encode("utf8"))发布于 2019-11-27 10:01:31
查看API文档,您的url的/upload部分似乎被更改为/image。
https://apidocs.imgur.com/?version=latest#c85c9dfc-7487-4de2-9ecd-66f727cf3139
(请参见右侧的“示例请求”)
但似乎这一切都是不可取的,在同一页上的信息是自相矛盾的。
https://stackoverflow.com/questions/59062553
复制相似问题