How do I close an image opened in Pillow?

前端 未结 2 1524
轮回少年
轮回少年 2021-01-07 16:32

I have a python file with the Pillow library imported. I can open an image with

Image.open(test.png)

But how do I close that image? I\'m no

相关标签:
2条回答
  • 2021-01-07 16:36

    With Image.close().

    You can also do it in a with block:

    with Image.open('test.png') as test_image:
        do_things(test_image)
    

    An example of using Image.close():

    test = Image.open('test.png')
    test.close()
    
    0 讨论(0)
  • 2021-01-07 16:43

    If you create a PIL object you will see there is no close method.

    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']
    
    0 讨论(0)
提交回复
热议问题