它适用于其他 JPG,但不适用于这个,我想知道出了什么问题以及如何改进。
import io
from aiohttp import ClientSession
from PIL import Image
session = ClientSession
async with session.get(f'https://rocket-league.com/content/media/itemshopPreviewDetailed/2024-12-12.jpg') as r:
if r.status == 200:
try:
img = Image.open(io.BytesIO(await r.read()))
except Exception as e:
logging.warning(e)
错误是:
WARNING: cannot identify image file <_io.BytesIO object at 0x7f216a624810>
。
它对我有效:
import asyncio
import io
from aiohttp import ClientSession
from PIL import Image
async def main():
session = ClientSession()
async with session.get('https://rocket-league.com/content/media/itemshopPreviewDetailed/2024-12-12.jpg') as r:
if r.status == 200:
image = Image.open(io.BytesIO(await r.read()))
print(image)
asyncio.run(main())
=>
<PIL.JpegImagePlugin.JpegImageFile image mode=RGB size=2048x1024 at 0x1014480D0>
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x10126d0a0>
Unclosed connector
connections: ['deque([(<aiohttp.client_proto.ResponseHandler object at 0x1012d9e20>, 0.155102541)])']
connector: <aiohttp.connector.TCPConnector object at 0x10126d520>
详情:
% python --version
Python 3.9.20
% pip freeze
aiohappyeyeballs==2.4.4
aiohttp==3.11.10
aiosignal==1.3.2
async-timeout==5.0.1
attrs==24.2.0
frozenlist==1.5.0
idna==3.10
multidict==6.1.0
pillow==11.0.0
propcache==0.2.1
typing-extensions==4.12.2
yarl==1.18.3