diff --git a/src/PIL/ImageFile.py b/src/PIL/ImageFile.py index daf732de1..43d2bf0cc 100644 --- a/src/PIL/ImageFile.py +++ b/src/PIL/ImageFile.py @@ -493,7 +493,11 @@ def _save(im, fp, tile, bufsize=0): # But, it would need at least the image size in most cases. RawEncode is # a tricky case. bufsize = max(MAXBLOCK, bufsize, im.size[0] * 4) # see RawEncode.c - if fp == sys.stdout or (hasattr(sys.stdout, "buffer") and fp == sys.stdout.buffer): + try: + stdout = fp == sys.stdout or fp == sys.stdout.buffer + except (OSError, AttributeError): + stdout = False + if stdout: fp.flush() return try: