mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-06-29 17:33:08 +03:00
moved normalise_ functions together
This commit is contained in:
parent
abb7b7342e
commit
f6b22bf0ab
|
@ -321,6 +321,38 @@ def _normalize_mode(im, initial_call=False):
|
||||||
return im.convert("P")
|
return im.convert("P")
|
||||||
return im.convert("L")
|
return im.convert("L")
|
||||||
|
|
||||||
|
def _normalize_palette(im, palette, info):
|
||||||
|
"""
|
||||||
|
Normalizes the palette for image.
|
||||||
|
- Sets the palette to the incoming palette, if provided.
|
||||||
|
- Ensures that there's a palette for L mode images
|
||||||
|
- Optimizes the palette if necessary/desired.
|
||||||
|
|
||||||
|
:param im: Image object
|
||||||
|
:param palette: bytes object containing the source palette, or ....
|
||||||
|
:param info: encoderinfo
|
||||||
|
:returns: Image object
|
||||||
|
"""
|
||||||
|
if im.mode == "P":
|
||||||
|
if palette and isinstance(palette, bytes):
|
||||||
|
source_palette = palette[:768]
|
||||||
|
else:
|
||||||
|
source_palette = im.im.getpalette("RGB")[:768]
|
||||||
|
else: # L-mode
|
||||||
|
if palette and isinstance(palette, bytes):
|
||||||
|
source_palette = palette[:768]
|
||||||
|
else:
|
||||||
|
source_palette = bytearray(i//3 for i in range(768))
|
||||||
|
im.palette = ImagePalette.ImagePalette("RGB",
|
||||||
|
palette=source_palette)
|
||||||
|
|
||||||
|
used_palette_colors = _get_optimize(im, info)
|
||||||
|
if used_palette_colors is not None:
|
||||||
|
return im.remap_palette(used_palette_colors, source_palette)
|
||||||
|
|
||||||
|
im.palette.palette = source_palette
|
||||||
|
return im
|
||||||
|
|
||||||
def _write_single_frame(im, fp, palette):
|
def _write_single_frame(im, fp, palette):
|
||||||
im_out = _normalize_mode(im, True)
|
im_out = _normalize_mode(im, True)
|
||||||
im_out = _normalize_palette(im_out, palette, im.encoderinfo)
|
im_out = _normalize_palette(im_out, palette, im.encoderinfo)
|
||||||
|
@ -610,38 +642,6 @@ def _get_header_palette(palette_bytes):
|
||||||
palette_bytes += o8(0) * 3 * actual_target_size_diff
|
palette_bytes += o8(0) * 3 * actual_target_size_diff
|
||||||
return palette_bytes
|
return palette_bytes
|
||||||
|
|
||||||
def _normalize_palette(im, palette, info):
|
|
||||||
"""
|
|
||||||
Normalizes the palette for image.
|
|
||||||
- Sets the palette to the incoming palette, if provided.
|
|
||||||
- Ensures that there's a palette for L mode images
|
|
||||||
- Optimizes the palette if necessary/desired.
|
|
||||||
|
|
||||||
:param im: Image object
|
|
||||||
:param palette: bytes object containing the source palette, or ....
|
|
||||||
:param info: encoderinfo
|
|
||||||
:returns: Image object
|
|
||||||
"""
|
|
||||||
if im.mode == "P":
|
|
||||||
if palette and isinstance(palette, bytes):
|
|
||||||
source_palette = palette[:768]
|
|
||||||
else:
|
|
||||||
source_palette = im.im.getpalette("RGB")[:768]
|
|
||||||
else: # L-mode
|
|
||||||
if palette and isinstance(palette, bytes):
|
|
||||||
source_palette = palette[:768]
|
|
||||||
else:
|
|
||||||
source_palette = bytearray(i//3 for i in range(768))
|
|
||||||
im.palette = ImagePalette.ImagePalette("RGB",
|
|
||||||
palette=source_palette)
|
|
||||||
|
|
||||||
used_palette_colors = _get_optimize(im, info)
|
|
||||||
if used_palette_colors is not None:
|
|
||||||
return im.remap_palette(used_palette_colors, source_palette)
|
|
||||||
|
|
||||||
im.palette.palette = source_palette
|
|
||||||
return im
|
|
||||||
|
|
||||||
def _get_palette_bytes(im):
|
def _get_palette_bytes(im):
|
||||||
"""
|
"""
|
||||||
Gets the palette for inclusion in the gif header
|
Gets the palette for inclusion in the gif header
|
||||||
|
|
Loading…
Reference in New Issue
Block a user