Python PIL 错误:'coroutine' object is not callable 解決方法
这个错误通常是因为在调用某个函数时,将一个协程对象作为参数传递给了该函数,但该函数不支持协程对象作为参数。
解决方法是将协程对象转换为普通函数,可以使用 asyncio.run() 函数来运行协程,并将其结果作为参数传递给函数。
示例代码:
import asyncio
from PIL import Image
async def open_image(filename):
return Image.open(filename)
async def main():
image = await open_image('test.jpg')
print(image.format)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
image = loop.run_until_complete(open_image('test.jpg')) # 运行协程,获取结果
print(image.format)
loop.close()
原文地址: https://www.cveoy.top/t/topic/oN1f 著作权归作者所有。请勿转载和采集!