Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bb54c50

Browse files
authored
Use walrus operator (#9448)
2 parents e96c5a5 + 26c7095 commit bb54c50

File tree

2 files changed

+8
-13
lines changed

2 files changed

+8
-13
lines changed

src/PIL/MpoImagePlugin.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,10 @@ def _save_all(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
5959
+ b"MPF\0"
6060
+ b" " * ifd_length
6161
)
62-
exif = im_frame.encoderinfo.get("exif")
63-
if isinstance(exif, Image.Exif):
64-
exif = exif.tobytes()
65-
im_frame.encoderinfo["exif"] = exif
66-
if exif:
62+
if exif := im_frame.encoderinfo.get("exif"):
63+
if isinstance(exif, Image.Exif):
64+
exif = exif.tobytes()
65+
im_frame.encoderinfo["exif"] = exif
6766
mpf_offset += 4 + len(exif)
6867

6968
JpegImagePlugin._save(im_frame, fp, filename)

src/PIL/PngImagePlugin.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,8 +1403,7 @@ def _save(
14031403

14041404
chunks = [b"cHRM", b"cICP", b"gAMA", b"sBIT", b"sRGB", b"tIME"]
14051405

1406-
icc = im.encoderinfo.get("icc_profile", im.info.get("icc_profile"))
1407-
if icc:
1406+
if icc := im.encoderinfo.get("icc_profile", im.info.get("icc_profile")):
14081407
# ICC profile
14091408
# according to PNG spec, the iCCP chunk contains:
14101409
# Profile name 1-79 bytes (character string)
@@ -1419,8 +1418,7 @@ def _save(
14191418
# Disallow sRGB chunks when an iCCP-chunk has been emitted.
14201419
chunks.remove(b"sRGB")
14211420

1422-
info = im.encoderinfo.get("pnginfo")
1423-
if info:
1421+
if info := im.encoderinfo.get("pnginfo"):
14241422
chunks_multiple_allowed = [b"sPLT", b"iTXt", b"tEXt", b"zTXt"]
14251423
for info_chunk in info.chunks:
14261424
cid, data = info_chunk[:2]
@@ -1472,8 +1470,7 @@ def _save(
14721470
alpha_bytes = colors
14731471
chunk(fp, b"tRNS", alpha[:alpha_bytes])
14741472

1475-
dpi = im.encoderinfo.get("dpi")
1476-
if dpi:
1473+
if dpi := im.encoderinfo.get("dpi"):
14771474
chunk(
14781475
fp,
14791476
b"pHYs",
@@ -1490,8 +1487,7 @@ def _save(
14901487
chunks.remove(cid)
14911488
chunk(fp, cid, data)
14921489

1493-
exif = im.encoderinfo.get("exif")
1494-
if exif:
1490+
if exif := im.encoderinfo.get("exif"):
14951491
if isinstance(exif, Image.Exif):
14961492
exif = exif.tobytes(8)
14971493
if exif.startswith(b"Exif\x00\x00"):

0 commit comments

Comments
 (0)