-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
GimpPaletteFile.py
69 lines (53 loc) · 1.67 KB
/
GimpPaletteFile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#
# Python Imaging Library
# $Id$
#
# stuff to read GIMP palette files
#
# History:
# 1997-08-23 fl Created
# 2004-09-07 fl Support GIMP 2.0 palette files.
#
# Copyright (c) Secret Labs AB 1997-2004. All rights reserved.
# Copyright (c) Fredrik Lundh 1997-2004.
#
# See the README file for information on usage and redistribution.
#
import re
import warnings
from ._binary import o8
class GimpPaletteFile:
"""File handler for GIMP's palette format."""
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):
if fp.readline()[:12] != b"GIMP Palette":
raise SyntaxError("not a GIMP palette file")
read = 0
self.palette = b""
while len(self.palette) < 3 * self.max_colors:
s = fp.readline(self._max_file_size)
if not s:
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
if re.match(rb"\w+:|#", s):
continue
if len(s) > self._max_line_size:
raise SyntaxError("bad palette file")
# 4th column is color name and may contain spaces.
v = s.split(maxsplit=3)
if len(v) < 3:
raise ValueError("bad palette entry")
for i in range(3):
self.palette += o8(int(v[i]))
def getpalette(self):
return self.palette, self.rawmode