-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgimpPatPattern.py
234 lines (213 loc) · 6.83 KB
/
gimpPatPattern.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env
# -*- coding: utf-8 -*-
"""
Pure python implementation of a gimp pattern file
"""
import typing
import PIL.Image
from gimpFormats.binaryIO import IO
class GimpPatPattern:
"""
Pure python implementation of a gimp pattern file
See:
https://gitlab.gnome.org/GNOME/gimp/blob/master/devel-docs/pat.txt
Format:
name: GimpPatPattern
description: Gimp pattern
guid: {45129576-6728-4967-8888-6b9082862ca5}
parentNames: Image
#mimeTypes: application/jpeg
filenamePatterns: *.pat
"""
MAGIC_NUMBER=(0,'GPAT')
COLOR_MODES=(None,'L','LA','RGB','RGBA')
def __init__(self,
filename:typing.Union[None,str,typing.BinaryIO]=None):
""" """
self.filename:typing.Optional[str]=None
self.version:float=1
self.width:int=0
self.height:int=0
self.bpp:int=4
self.mode:typing.Optional[str]=self.COLOR_MODES[self.bpp]
self.name:str=''
self._rawImage:typing.Optional[bytes]=None
self._image:typing.Optional[PIL.Image.Image]=None
if filename is not None:
self.load(filename)
def load(self,filename:typing.Union[str,typing.BinaryIO])->None:
"""
load a gimp file
:param filename: can be a file name or a file-like object
"""
if isinstance(filename,str):
self.filename=filename
f=open(filename,'rb')
data=f.read()
f.close()
else:
self.filename=filename.name
data=filename.read()
self._decode_(data)
def _decode_(self,data:bytes,index:int=0)->int:
"""
decode a byte buffer
:param data: data buffer to decode
:param index: index within the buffer to start at
"""
io=IO(data,index)
headerSize=io.u32
self.version=io.u32
self.width=io.u32
self.height=io.u32
self.bpp=io.u32
self.mode=self.COLOR_MODES[self.bpp]
magic=io.getBytes(4)
if magic.decode('ascii')!='GPAT':
raise Exception('File format error. Magic value mismatch.')
nameLen=headerSize-io.index
self.name=io.getBytes(nameLen).decode('UTF-8')
self._rawImage=io.getBytes(self.width*self.height*self.bpp)
self._image=None
return io.index-index
def toBytes(self)->bytes:
"""
encode to a byte buffer
"""
if self.image is None:
return bytes()
io=IO()
io.u32=24+len(self.name)
io.u32=self.version
io.u32=self.width
io.u32=self.height
mode=self.image.mode
if mode is None:
mode='L'
io.u32=len(mode)
io.addBytes('GPAT')
io.addBytes(self.name.encode('utf-8'))
if self._rawImage is None:
rawImage=self.image.tobytes(encoder_name='raw')
else:
rawImage=self._rawImage
io.addBytes(rawImage)
return io.data
@property
def size(self)->typing.Tuple[int,int]:
"""
the size of the pattern
"""
return (self.width,self.height)
@property
def image(self)->typing.Optional[PIL.Image.Image]:
"""
get a final, compiled image
"""
if self._image is None:
if self._rawImage is None:
return None
raw=bytes(self._rawImage)
mode=self.mode
if mode is None:
mode='L'
self._image=PIL.Image.frombytes(
mode,self.size,raw,decoder_name='raw')
return self._image
@image.setter
def image(self,image:PIL.Image.Image):
self._image=image
self._rawImage=None
def save(self,
toFilename:typing.Optional[str]=None,
toExtension:typing.Optional[str]=None
)->None:
"""
save this gimp image to a file
"""
asImage=False
f=None
if toFilename is None:
if self.filename is None:
self.filename='untitled.pat'
toFilename=self.filename
elif isinstance(toFilename,str):
self.filename=str(toFilename)
else:
f=toFilename
toFilename=toFilename.name
self.filename=toFilename
if toExtension is None:
if toFilename is not None:
ext=toFilename.rsplit('.',1)
if len(ext)>1:
toExtension=ext[-1]
else:
toExtension=None
if toExtension is not None and toExtension!='pat':
asImage=True
if asImage:
if self.image is not None:
self.image.save(toFilename)
else:
if f is None:
f=open(toFilename,'wb')
f.write(self.toBytes())
def __repr__(self,indent:str='')->str:
"""
Get a textual representation of this object
"""
ret=[]
if self.filename is not None:
ret.append('Filename: '+self.filename)
ret.append('Name: '+str(self.name))
ret.append('Version: '+str(self.version))
ret.append('Size: '+str(self.width)+' x '+str(self.height))
ret.append('BPP: '+str(self.bpp))
ret.append('Mode: '+str(self.mode))
return '\n'.join(ret)
def cmdline(args:typing.Iterable[str])->int:
"""
Run the command line
:param args: command line arguments (WITHOUT the filename)
"""
printhelp=False
if not args:
printhelp=True
else:
g=None
for arg in args:
if arg.startswith('-'):
kv=[a.strip() for a in arg.split('=',1)]
if kv[0] in ('-h','--help'):
printhelp=True
elif kv[0]=='--dump':
print(g)
elif kv[0]=='--show':
if g is None:
print('ERR: No pattern to show')
else:
g.image.show()
elif kv[0]=='--save':
if g is None:
print('ERR: No pattern to save')
else:
g.image.save(kv[1])
else:
print(f'ERR: unknown argument "{arg}"')
else:
g=GimpPatPattern(arg)
if printhelp:
print('Usage:')
print(' gimpPatPattern.py file.xcf [options]')
print('Options:')
print(' -h, --help ............ this help screen')
print(' --dump ................ dump info about this file')
print(' --show ................ show the pattern image')
print(' --save=out.jpg ........ save out the pattern image')
print(' --register ............ register this extension')
return -1
return 0
if __name__=='__main__':
import sys
cmdline(sys.argv[1:])