@@ -116,9 +116,6 @@ def build_prototype_image() -> Image.Image:
116
116
117
117
def _save (im : Image .Image , fp : IO [bytes ], filename : str | bytes ) -> None :
118
118
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
-
122
119
rawmode = "P"
123
120
bpp = 8
124
121
version = 1
@@ -172,12 +169,11 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
172
169
compression_type = _COMPRESSION_TYPES ["none" ]
173
170
174
171
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
181
177
else :
182
178
colormapsize = 0
183
179
@@ -196,22 +192,11 @@ def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
196
192
197
193
# now write colormap if necessary
198
194
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 ):
202
198
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 ])
215
200
216
201
# now convert data to raw form
217
202
ImageFile ._save (
0 commit comments