我无法使用枕头中的 close() 关闭列表中的图像
I can't close images from list using close() from pillow
我用 Image.open() 打开图像并将它们存储在二维列表“图像”中。但是现在我正在尝试使用 close() 函数关闭所有图像并且代码编译但它不会关闭文件
for i in range(len(images)):
for j in range(len(images[i])):
print(images[i][j])
images[i][j].close()
print(images)
output:
<PIL.Image.Image image mode=RGBA size=1080x1080 at 0x7F9BD4A6A280>
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4A6A3D0>
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4DACCA0>
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4DB7430>
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4DB7610>
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4DB76A0>
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4DB7730>
[[<PIL.Image.Image image mode=RGBA size=1080x1080 at 0x7F9BD4A6A280>], [<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4A6A3D0>, <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4DACCA0>, <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4DB7430>, <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4DB7610>], [<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4DB76A0>, <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4DB7730>]]
关闭图像会使其从那时起无法使用,但不会将其从列表中删除。如果您尝试对这些图像做任何实际操作,您几乎肯定会遇到错误。也许关闭后,您应该将其替换为 None
:
images[i][j] = None
我用 Image.open() 打开图像并将它们存储在二维列表“图像”中。但是现在我正在尝试使用 close() 函数关闭所有图像并且代码编译但它不会关闭文件
for i in range(len(images)):
for j in range(len(images[i])):
print(images[i][j])
images[i][j].close()
print(images)
output:
<PIL.Image.Image image mode=RGBA size=1080x1080 at 0x7F9BD4A6A280>
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4A6A3D0>
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4DACCA0>
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4DB7430>
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4DB7610>
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4DB76A0>
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4DB7730>
[[<PIL.Image.Image image mode=RGBA size=1080x1080 at 0x7F9BD4A6A280>], [<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4A6A3D0>, <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4DACCA0>, <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4DB7430>, <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4DB7610>], [<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4DB76A0>, <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=1080x1080 at 0x7F9BD4DB7730>]]
关闭图像会使其从那时起无法使用,但不会将其从列表中删除。如果您尝试对这些图像做任何实际操作,您几乎肯定会遇到错误。也许关闭后,您应该将其替换为 None
:
images[i][j] = None