马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
r = requests.get('被和谐://cn.bing.com/fd/ls/l?IG=CBBE9FF0F2364C838A3CC6AF191D09D4&TYPE=Event.ClientInst&DATA=%5B%7B%22width%22%3A%221920%22%2C%22T%22%3A%22CI.Info%22%2C%22TS%22%3A1691340568572%2C%22Name%22%3A%22XW%22%2C%22FID%22%3A%22BRW%22%7D%2C%7B%22height%22%3A%22273%22%2C%22T%22%3A%22CI.Info%22%2C%22TS%22%3A1691340568572%2C%22Name%22%3A%22S%22%2C%22FID%22%3A%22BRH%22%7D%2C%7B%22T%22%3A%22CI.Info%22%2C%22TS%22%3A1691340568573%2C%22Name%22%3A%22H%22%2C%22FID%22%3A%22BRResize%22%7D%5D')
i = Image.open(BytesIO(r.content))
i.show()
运行上面的代码报错Traceback (most recent call last):
File "C:\Users\YJS\PycharmProjects\Requests\Requests.py", line 95, in <module>
i = Image.open(BytesIO(r.content))
File "C:\Users\YJS\PycharmProjects\Requests\venv\lib\site-packages\PIL\Image.py", line 3280, in open
raise UnidentifiedImageError(msg)
PIL.UnidentifiedImageError: cannot identify image file <_io.BytesIO object at 0x00000198D6CF2B60>
请问如何正确从网上下载图片 并打开显示?谢谢,麻烦举一个能直接在PyCharm上使用的代码案例 谢谢
根据您提供的代码和错误信息,问题出在无法识别图像文件。这可能是因为您尝试打开的URL返回的内容并不是有效的图像文件。
要正确从网上下载图片并打开显示,您可以使用以下代码:
import requests
from PIL import Image
from io import BytesIO
# 下载图片并保存到本地
url = 'https://cn.bing.com/fd/ls/l?IG=CBBE9FF0F2364C838A3CC6AF191D09D4&TYPE=Event.ClientInst&DATA=%5B%7B%22width%22%3A%221920%22%2C%22T%22%3A%22CI.Info%22%2C%22TS%22%3A1691340568572%2C%22Name%22%3A%22XW%22%2C%22FID%22%3A%22BRW%22%7D%2C%7B%22height%22%3A%22273%22%2C%22T%22%3A%22CI.Info%22%2C%22TS%22%3A1691340568572%2C%22Name%22%3A%22S%22%2C%22FID%22%3A%22BRH%22%7D%2C%7B%22T%22%3A%22CI.Info%22%2C%22TS%22%3A1691340568573%2C%22Name%22%3A%22H%22%2C%22FID%22%3A%22BRResize%22%7D%5D'
response = requests.get(url)
image = Image.open(BytesIO(response.content))
# 在PyCharm中显示图片
image.show()
这段代码使用了 requests 库发送GET请求获取图片的内容,并使用 PIL 库中的 Image 模块打开和显示图像。请确保您已经安装了 requests 和 Pillow 库。
请注意,您需要将URL替换为有效的图片URL才能成功下载和打开图像。
|