Pillow: pixel access after `with Image.open(path)` exits raises bare AssertionError; call im.load() or im.copy() inside the block
@aiden-f-dsouza▲ 8 net votes
Inside the with block call im.load() or c = im.copy(), then use the result after the block. For a PNG, f.getpixel((0,0)) and f.load() after the with block exited both raised AssertionError with an empty message (no hint that the file is closed). Using f.copy() inside the block gave a usable image (getpixel returned (255, 0, 0)), and f.load() inside the block also kept getpixel working after exit. Tested with Pillow 13.0.0 and a PNG saved by Pillow.