Removed duplicate code

This commit is contained in:
Andrew Murray 2022-02-28 13:59:52 +11:00
parent 4d868abd8a
commit bb9338e34d

View File

@ -499,44 +499,33 @@ def _save(im, fp, tile, bufsize=0):
try: try:
fh = fp.fileno() fh = fp.fileno()
fp.flush() fp.flush()
except (AttributeError, io.UnsupportedOperation) as exc: exc = None
# compress to Python file-compatible object except (AttributeError, io.UnsupportedOperation) as e:
for e, b, o, a in tile: exc = e
if o > 0: for e, b, o, a in tile:
fp.seek(o) if o > 0:
e = Image._getencoder(im.mode, e, a, im.encoderconfig) fp.seek(o)
try: encoder = Image._getencoder(im.mode, e, a, im.encoderconfig)
e.setimage(im.im, b) try:
if e.pushes_fd: encoder.setimage(im.im, b)
e.setfd(fp) if encoder.pushes_fd:
l, s = e.encode_to_pyfd() encoder.setfd(fp)
else: l, s = encoder.encode_to_pyfd()
else:
if exc:
# compress to Python file-compatible object
while True: while True:
l, s, d = e.encode(bufsize) l, s, d = encoder.encode(bufsize)
fp.write(d) fp.write(d)
if s: if s:
break break
if s < 0:
raise OSError(f"encoder error {s} when writing image file") from exc
finally:
e.cleanup()
else:
# slight speedup: compress to real file object
for e, b, o, a in tile:
if o > 0:
fp.seek(o)
e = Image._getencoder(im.mode, e, a, im.encoderconfig)
try:
e.setimage(im.im, b)
if e.pushes_fd:
e.setfd(fp)
l, s = e.encode_to_pyfd()
else: else:
s = e.encode_to_file(fh, bufsize) # slight speedup: compress to real file object
if s < 0: s = encoder.encode_to_file(fh, bufsize)
raise OSError(f"encoder error {s} when writing image file") if s < 0:
finally: raise OSError(f"encoder error {s} when writing image file") from exc
e.cleanup() finally:
encoder.cleanup()
if hasattr(fp, "flush"): if hasattr(fp, "flush"):
fp.flush() fp.flush()