Python 如何关闭在 Pillow 中打开的图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31751464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I close an image opened in Pillow?
提问by Chase Cromwell
I have a python file with the Pillow library imported. I can open an image with
我有一个导入 Pillow 库的 python 文件。我可以打开一个图像
Image.open(test.png)
But how do I close that image? I'm not using Pillow to edit the image, just to show the image and allow the user to choose to save it or delete it.
但是如何关闭该图像?我没有使用 Pillow 来编辑图像,只是为了显示图像并允许用户选择保存或删除它。
采纳答案by Morgan Thrapp
With Image.close().
You can also do it in a with block:
您也可以在 with 块中执行此操作:
with Image.open('test.png') as test_image:
do_things(test_image)
An example of using Image.close()
:
使用示例Image.close()
:
test = Image.open('test.png')
test.close()
回答by tzimhs panousis
If you create a PIL object you will see there is no close method.
如果您创建一个 PIL 对象,您将看到没有 close 方法。
from PIL import Image
img=Image.open("image.jpg")
dir(img)
['_Image__transformer', '_PngImageFile__idat', '__doc__', '__getattr__', '__init__', '__module__', '__repr__', '_copy', '_dump', '_expand', '_makeself', '_new', '_open', 'category', 'convert', 'copy', 'crop', 'decoderconfig', 'decodermaxblock', 'draft', 'filename', 'filter', 'format', 'format_description', 'fp', 'frombytes', 'fromstring', 'getbands', 'getbbox', 'getcolors', 'getdata', 'getextrema', 'getim', 'getpalette', 'getpixel', 'getprojection', 'histogram', 'im', 'info', 'load', 'load_end', 'load_prepare', 'load_read', 'map', 'mode', 'offset', 'palette', 'paste', 'png', 'point', 'putalpha', 'putdata', 'putpalette', 'putpixel', 'quantize', 'readonly', 'resize', 'rotate', 'save', 'seek', 'show', 'size', 'split', 'tell', 'text', 'thumbnail', 'tile', 'tobitmap', 'tobytes', 'tostring', 'transform', 'transpose', 'verify']