出现"OSError: [Errno 9] 文件描述符错误"错误通常是由于文件描述符被关闭或不可用导致的。下面是一些可能的解决方法:
image = Image.open('image.jpg')
file = open('output.jpg', 'wb') # 打开一个文件描述符
image.save(file)
file.close() # 关闭文件描述符
with open('output.jpg', 'wb') as file:
image.save(file)
io.BytesIO
作为文件描述符:import io
image = Image.open('image.jpg')
output = io.BytesIO() # 创建一个字节流文件描述符
image.save(output, format='JPEG')
output.seek(0) # 将文件指针移动到开头
with open('output.jpg', 'wb') as file:
file.write(output.read()) # 将字节流写入文件
确保保存图像的目录存在并且有适当的权限。
如果您正在使用操作系统的临时目录进行保存,请确保临时目录可用。
请根据您的具体情况选择适当的解决方法。如果问题仍然存在,请提供更多的代码和错误信息,以便我们能够更具体地帮助您解决问题。