且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

python PIL图像如何将图像保存到缓冲区以便以后使用?

更新时间:2023-11-13 09:19:04

使用 getvalue 方法而非读取

with fs.new_file(filename="test.png") as fp:
    fp.write(fake_file.getvalue())

或者,您可以使用 如果你第一次=https://docs.python.org/2/library/stdtypes.html#file.seek =nofollow,请阅读 > seek(0) 从StringIO的开头读取。

Alternatively, you could use read if you first seek(0) to read from the beginning of the StringIO.

with fs.new_file(filename="test.png") as fp:
    fake_file.seek(0)
    fp.write(fake_file.read())