@@ -116,9 +116,6 @@ def build_prototype_image() -> Image.Image:
116116
117117def _save (im : Image .Image , fp : IO [bytes ], filename : str | bytes ) -> None :
118118 if im .mode == "P" :
119- # we assume this is a color Palm image with the standard colormap,
120- # unless the "info" dict has a "custom-colormap" field
121-
122119 rawmode = "P"
123120 bpp = 8
124121 version = 1
@@ -172,12 +169,11 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
172169 compression_type = _COMPRESSION_TYPES ["none" ]
173170
174171 flags = 0
175- if im .mode == "P" and "custom-colormap" in im .info :
176- assert im .palette is not None
177- flags = flags & _FLAGS ["custom-colormap" ]
178- colormapsize = 4 * 256 + 2
179- colormapmode = im .palette .mode
180- colormap = im .getdata ().getpalette ()
172+ if im .mode == "P" :
173+ flags |= _FLAGS ["custom-colormap" ]
174+ colormap = im .im .getpalette ()
175+ colors = len (colormap ) // 3
176+ colormapsize = 4 * colors + 2
181177 else :
182178 colormapsize = 0
183179
@@ -196,22 +192,11 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
196192
197193 # now write colormap if necessary
198194
199- if colormapsize > 0 :
200- fp .write (o16b (256 ))
201- for i in range (256 ):
195+ if colormapsize :
196+ fp .write (o16b (colors ))
197+ for i in range (colors ):
202198 fp .write (o8 (i ))
203- if colormapmode == "RGB" :
204- fp .write (
205- o8 (colormap [3 * i ])
206- + o8 (colormap [3 * i + 1 ])
207- + o8 (colormap [3 * i + 2 ])
208- )
209- elif colormapmode == "RGBA" :
210- fp .write (
211- o8 (colormap [4 * i ])
212- + o8 (colormap [4 * i + 1 ])
213- + o8 (colormap [4 * i + 2 ])
214- )
199+ fp .write (colormap [3 * i : 3 * i + 3 ])
215200
216201 # now convert data to raw form
217202 ImageFile ._save (
0 commit comments