没有403会创建类似的行为? (Python Wget)

时间:2017-07-03 07:18:26

标签: python python-3.x wget

所以我有一些代码可以下载图像,覆盖它并显示结果。但是,当我尝试从特定站点下载时,我得到403(可能来自用户代理)。如何创建与此相似的代码?

from PIL import Image
import os, sys
import wget
import requests


url = "https://cdn.discordapp.com/avatars/247096918923149313/34a66572b9339acdaa1dedbcb63bc90a.png?size=256"
filename = wget.download(url)

pp = Image.open(filename)
pp.save("image2c.png")
pp = Image.open("image2c.png").convert("LA")
pp.save("image2c.png")

background = Image.open("image1.png").convert("RGBA")
foreground = Image.open("image2c.png").convert("RGBA")
foreground = foreground.resize((256, 256), Image.BILINEAR)

background.paste(foreground, (125, 325), foreground)
background.show()
os.remove(filename)

1 个答案:

答案 0 :(得分:0)

似乎wget python库在https或参数方面存在一些问题......您可以使用请求(您已经导入了它)。

Store ID

参见:How do I read image data from a URL in Python?

对于Python3:

from PIL import Image
import os, sys
import requests
from StringIO import StringIO

url = "https://cdn.discordapp.com/avatars/247096918923149313/34a66572b9339acdaa1dedbcb63bc90a.png?size=256"
response = requests.get(url)
pp = Image.open(StringIO(response.content))
pp.save("image1.png")

pp = Image.open("image2c.png").convert("LA")
pp.save("image2c.png")

background = Image.open("image1.png").convert("RGBA")
foreground = Image.open("image2c.png").convert("RGBA")
foreground = foreground.resize((256, 256), Image.BILINEAR)
background.paste(foreground, (125, 325), foreground)
background.show()

请参阅:https://stackoverflow.com/a/31067445/8221879