mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-03 12:00:10 +03:00
Provide way to load big palettes;
This commit is contained in:
parent
1ed6039c85
commit
a12389bf35
|
@ -1,7 +1,7 @@
|
||||||
GIMP Palette
|
GIMP Palette
|
||||||
Name: custompalette
|
Name: custompalette
|
||||||
Columns: 4
|
Columns: 4
|
||||||
#
|
# Original written by David Wetz in https://stackoverflow.com/questions/815836/im-creating-a-program-that-generates-a-palette-from-a-true-color-image-need-hel/815855#815855
|
||||||
0 0 0 Index 0
|
0 0 0 Index 0
|
||||||
65 38 30 Index 1
|
65 38 30 Index 1
|
||||||
103 62 49 Index 2
|
103 62 49 Index 2
|
||||||
|
|
|
@ -20,6 +20,21 @@ def test_sanity():
|
||||||
GimpPaletteFile(fp)
|
GimpPaletteFile(fp)
|
||||||
|
|
||||||
|
|
||||||
|
def test_large_file_is_truncated():
|
||||||
|
import warnings
|
||||||
|
from unittest.mock import patch
|
||||||
|
try:
|
||||||
|
original_value = GimpPaletteFile._max_file_size
|
||||||
|
GimpPaletteFile._max_file_size = 100
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter("error")
|
||||||
|
with pytest.raises(UserWarning):
|
||||||
|
with open("Tests/images/custom_gimp_palette.gpl", "rb") as fp:
|
||||||
|
palette_file = GimpPaletteFile(fp)
|
||||||
|
|
||||||
|
finally:
|
||||||
|
GimpPaletteFile._max_file_size = original_value
|
||||||
|
|
||||||
def test_get_palette():
|
def test_get_palette():
|
||||||
# Arrange
|
# Arrange
|
||||||
with open("Tests/images/custom_gimp_palette.gpl", "rb") as fp:
|
with open("Tests/images/custom_gimp_palette.gpl", "rb") as fp:
|
||||||
|
@ -43,3 +58,13 @@ def test_get_palette():
|
||||||
expected_palette += bytes(color)
|
expected_palette += bytes(color)
|
||||||
assert palette == expected_palette
|
assert palette == expected_palette
|
||||||
assert mode == "RGB"
|
assert mode == "RGB"
|
||||||
|
|
||||||
|
|
||||||
|
def test_n_colors():
|
||||||
|
# Arrange
|
||||||
|
with open("Tests/images/custom_gimp_palette.gpl", "rb") as fp:
|
||||||
|
palette_file = GimpPaletteFile(fp)
|
||||||
|
|
||||||
|
palette, _ = palette_file.getpalette()
|
||||||
|
assert len(palette) == 24
|
||||||
|
assert palette_file.n_colors == 8
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
import warnings
|
||||||
|
|
||||||
from ._binary import o8
|
from ._binary import o8
|
||||||
|
|
||||||
|
@ -24,25 +25,39 @@ class GimpPaletteFile:
|
||||||
|
|
||||||
rawmode = "RGB"
|
rawmode = "RGB"
|
||||||
|
|
||||||
|
#: override if reading larger palettes is needed
|
||||||
|
max_colors = 256
|
||||||
|
_max_line_size = 100
|
||||||
|
_max_file_size = 2 ** 20
|
||||||
|
|
||||||
def __init__(self, fp):
|
def __init__(self, fp):
|
||||||
|
|
||||||
if fp.readline()[:12] != b"GIMP Palette":
|
if fp.readline()[:12] != b"GIMP Palette":
|
||||||
raise SyntaxError("not a GIMP palette file")
|
raise SyntaxError("not a GIMP palette file")
|
||||||
|
|
||||||
self.palette = b""
|
read = 0
|
||||||
while len(self.palette) < 768:
|
|
||||||
|
|
||||||
s = fp.readline()
|
self.palette = b""
|
||||||
|
while len(self.palette) < 3 * self.max_colors:
|
||||||
|
|
||||||
|
s = fp.readline(self._max_file_size)
|
||||||
if not s:
|
if not s:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
read += len(s)
|
||||||
|
if read >= self._max_file_size:
|
||||||
|
warnings.warn(
|
||||||
|
f"Palette file truncated at {self._max_file_size - len(s)} bytes")
|
||||||
|
break
|
||||||
|
|
||||||
# skip fields and comment lines
|
# skip fields and comment lines
|
||||||
if re.match(rb"\w+:|#", s):
|
if re.match(rb"\w+:|#", s):
|
||||||
continue
|
continue
|
||||||
if len(s) > 100:
|
if len(s) > self._max_line_size:
|
||||||
raise SyntaxError("bad palette file")
|
raise SyntaxError("bad palette file")
|
||||||
|
|
||||||
v = s.split()
|
# 4th column is color name and may contain spaces.
|
||||||
|
v = s.split(None, 4)
|
||||||
if len(v) < 3:
|
if len(v) < 3:
|
||||||
raise ValueError("bad palette entry")
|
raise ValueError("bad palette entry")
|
||||||
for i in range(3):
|
for i in range(3):
|
||||||
|
@ -51,3 +66,7 @@ class GimpPaletteFile:
|
||||||
def getpalette(self):
|
def getpalette(self):
|
||||||
|
|
||||||
return self.palette, self.rawmode
|
return self.palette, self.rawmode
|
||||||
|
|
||||||
|
@property
|
||||||
|
def n_colors(self):
|
||||||
|
return len(self.palette) / 3
|
||||||
|
|
Loading…
Reference in New Issue
Block a user