From 44c6467400502b69e48bbbbb0a20a70c564538fb Mon Sep 17 00:00:00 2001 From: Ray Gardner Date: Fri, 13 May 2022 11:38:39 -0600 Subject: [PATCH] Multiple GIF comments in a frame are separated If more than one comment is in a GIF frame, separate them with \r\n in the info dict. --- Tests/images/multiple_comments.gif | Bin 0 -> 1540 bytes Tests/test_file_gif.py | 6 ++++++ src/PIL/GifImagePlugin.py | 13 +++++++++---- 3 files changed, 15 insertions(+), 4 deletions(-) create mode 100644 Tests/images/multiple_comments.gif diff --git a/Tests/images/multiple_comments.gif b/Tests/images/multiple_comments.gif new file mode 100644 index 0000000000000000000000000000000000000000..88b2af800e89f7771c48fa9cb667097608802df0 GIT binary patch literal 1540 zcmZY8eKga190%~%R5o(^^+qKG_ROcP@hGRr((Hc!KJqRi7g z0)t9H`2P?t?onT_wMnUwpc?WA3wkkcn1K= z^xfnqJmjvVqm_%Zg&EEYql*#&0RVv9k#_Nqh(!2a#pC_MA`$34Hy8l94F(HZ0UhBc z{1Di;laW>RcunSRar0Dq(@Ym%hBjxDzm!_O3Tj^G)nOzjHjSBEPwux*NQ&K1C=IvC zg)<-dQK5@QheoO+gYN}j9jj&X`Dr2T4Y2#CIdN4jii?@7xyS>5nRZNA#C)l!-x6;d zKr6KQOePmByY-Ps?;-BUs-T~Yj3_TF+qtke+fZ3Mh@Ho0LfUGHcE$6K7trgQr#S(^ z_QpjV)?f&Y&zINfvbq#X%z(4IhMvET1uGrbcX;g^_rRHhKk~|R_=n8nrrn-lVHS+| zkHgXy+jYt(R3hB3I%WNF;WkC4r1bJxa?&IStq}x=B*iu_hJQj$!2=2ODoyoyH<<24 z%K#!W>1cWd9iq)}dAEFoYNj369$dHv8?x~TP!A2%t9WwDcP z7;HjD;`RLMo7w0h6+SfoF4YG>NK1)La~FD2u-3jC1v;wX!xCvhN4=`XI46xMcF74m zBmzt09=NN25Bh8(>NFqE$a#AsnjDj0O_0EvC6Lsp0Qp9;lMYA?3Izj9DCO3n9C2nL zv#6L|9FlBcRtPSuFRp7SKU`ZYYAFhccChLc5kR2`wynLbi(P#zz61WKv*V-zRuRG~ z6B!*d=mpPCvL5%$F20%9o>?keURi8>IPJ(=8&lHSaNPR1iQHCk|1>_Sfl@%95(O#O zY&fZO%gF8>OuhdR)d1zMh=~Fp0bh4_w?Iq4Mo6H46BBu5*fG6>$^_2RCn>6yAa?}< z_>E`j0i3)9#9u2R*x!if`gTi^)jYT!!NJEjLAigzLcJTFCY{rayThyy*Z&NvIjB-< z-j>u}N zpB0f@s%Lz^^dqo`8NQV+vsY zlM#prQvZzw(OCU$q(pWEGNwQmbTQ2pgv5-)#lV(`Yp(}FFIsn7cEVepHP=8U*6V28;QeuWRf&R z>`R(Ojx0Z(_ZiUT&SiPG{8qN+SmSMy2Y`@))9vZ&+RcEN4aB;8lR(4)R@tg;3 zvN9|*sq+JpM_dI(iuFo*W6Rz}N*$)RWT0)wCbB%F{L=gS8SmE;^G3bOx8Yb0gT0;P zs?-{>|5V0PKl{a;F>D|q@b~kO)(!O!Om$q~Gfjx9w;{^t#b`y!wS$%Z)+B|gyqp#1 z`NDC{-0GvQFXhGB^HUBEtSpmPiVm&zi#3o+iWE^pC&_Y>_M4sD5hKmPV+OCjIPXo2 HK4APe#uKGI literal 0 HcmV?d00001 diff --git a/Tests/test_file_gif.py b/Tests/test_file_gif.py index 3c2fab722..9fb171411 100644 --- a/Tests/test_file_gif.py +++ b/Tests/test_file_gif.py @@ -813,6 +813,12 @@ def test_zero_comment_subblocks(): assert_image_equal_tofile(im, TEST_GIF) +def test_read_multiple_comments(): + with Image.open("Tests/images/multiple_comments.gif") as im: + # Multiple comments in a frame are separated not concatenated + assert im.info["comment"] == b"Test comment 1\r\nTest comment 2" + + def test_version(tmp_path): out = str(tmp_path / "temp.gif") diff --git a/src/PIL/GifImagePlugin.py b/src/PIL/GifImagePlugin.py index 9b34a3b0e..6c2b1dedf 100644 --- a/src/PIL/GifImagePlugin.py +++ b/src/PIL/GifImagePlugin.py @@ -228,12 +228,17 @@ class GifImageFile(ImageFile.ImageFile): # # comment extension # + # Collect one comment block + comment = b"" while block: - if "comment" in info: - info["comment"] += block - else: - info["comment"] = block + comment += block block = self.data() + + # If multiple comments in frame, separate in info with \r\n + if "comment" in info: + info["comment"] += b"\r\n" + comment + else: + info["comment"] = comment s = None continue elif s[0] == 255: