-
Notifications
You must be signed in to change notification settings - Fork 3
/
core.py
277 lines (251 loc) · 9.88 KB
/
core.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
from PIL import Image, ImageDraw, ImageFont, ImageOps
from ast import literal_eval
import platform
import os.path
import json
import rreader
import re
import math
import chardet
class canvas:
def __init__(self, font=None, mode="RGBA", size=13, row=10, column=10,
width=-1, height=-1, oline=0,
bgcolor=(0, 0, 0, 0), fcolor=(255, 255, 255, 255),
ocolor=(0, 0, 0, 255)):
"""
font = A font file name
row = Text's row in image (maybe never used)
column = Text's column in image
width = Canvas's width
height = Canvas's height
"""
self.size = size
self.width = width
self.height = height
self.outlinewidth = oline
self.font = font
self.row = row
self.column = column
self.bgcolor = bgcolor
self.fontcolor = fcolor
self.outlinecolor = ocolor
self.mode = mode
self.image = ""
self.data = {}
if bgcolor[0] == "#":
self.bgcolor = self.hex2rgb(bgcolor)
if fcolor[0] == "#":
self.fontcolor = self.hex2rgb(fcolor)
if ocolor[0] == "#":
self.outlinecolor = self.hex2rgb(ocolor)
if mode == "RGB":
bgcolor = (bgcolor[0], bgcolor[1], bgcolor[2])
fcolor = (fcolor[0], fcolor[1], fcolor[2])
ocolor = (ocolor[0], ocolor[1], ocolor[2])
if self.font == None or self.font == "":
system = platform.system()
# Check system neither Windows or Linux
if system == "Windows":
if os.path.exists("C:/Windows/Fonts/gulim.ttc"):
self.font = "C:/Windows/Fonts/gulim.ttc"
else:
self.font = "C:/Windows/Fonts/Arial.ttf"
elif system == "Linux":
if os.path.exists("/usr/share/fonts/truetype/nanum/NanumBarunGothic.ttf"):
self.font = "/usr/share/fonts/truetype/nanum/NanumBarunGothic.ttf"
else:
self.font = "/usr/share/fonts/truetype/freefont/FreeMono.ttf"
def hex2rgb(self, color):
"""
The function for convert hex color to rgb color.
"""
color = color.lstrip("#")
tp = tuple(int(color[i:i + 2], 16) for i in (0, 2, 4))
return (tp[0], tp[1], tp[2], 255)
def setJSON(self, file):
'''
The function for read bffnt font file menifest.
'''
try:
with open("./test_manifest.json", "r", encoding="utf-8") as f:
manifest = json.load(f)
glyphWidth_dict = manifest["glyphWidths"]
glyphMap_dict= manifest["glyphMap"]
self.texture_dict= manifest["textureInfo"]
self.fontInfo_dict = manifest["fontInfo"]
self.glyph_dict = dict()
for key, value in glyphMap_dict.items():
char_data = glyphWidth_dict[str(value)]
self.glyph_dict[key] = char_data
return True
except:
return False
def create(self, text, cwidth=-1, cheight=-1,
xoffset=0, yoffset=0, mode="a", jsonset = 0, sizeOffset = 0):
"""
Create a image file(*.png) with specific text
text = A text that will be written(string or text file)
cwidth, cheight = A character's width and height
xoffset, yoffset = A starting point which font start
jsonset = A bffnt font file's Menifest File. Use setJSON First.
SizeOffset = font Size offset When you use Menifest JSON File.
"""
# Check Menifest File.
if jsonset != 0:
if os.path.exists(jsonset):
json_flag = self.setJSON(jsonset)
if json_flag == False:
print("Wrong JSON File. Ignore this setting.")
jsonset = 0
else:
cwidth = self.texture_dict["glyph"]["width"] + 1
cheight = self.texture_dict["glyph"]["height"] + 1
self.size = self.fontInfo_dict["width"] - sizeOffset
else:
print("Wrong JSON File. Ignore this setting.")
jsonset = 0
# Set character's width
if cwidth < 1:
cwidth = self.size + 3
if cheight < 1:
cheight = self.size + 3
# Set text
if os.path.exists(text):
with open(text, mode='rb') as file:
textencoding = chardet.detect(file.read())['encoding']
with open(text, mode='r', encoding=textencoding) as file:
text = "".join(file.readlines())
# Set image's width and height
if self.width < 1:
if len(text) <= self.column:
self.width = len(text) * cwidth
else:
self.width = self.column * cwidth
if self.height < 1:
self.height = ((len(text) // self.column) + 2) * cheight
img = Image.new(self.mode, (self.width, self.height), color=self.bgcolor)
fnt = ImageFont.truetype(self.font, self.size, encoding="utf-8")
draw = ImageDraw.Draw(img)
# Set non-anti-alias
if mode == "n":
draw.fontmode = "1"
nrow = 0
ncolumn = 0
# Save dict (for json)
self.data['width'] = self.width
self.data['height'] = self.height
self.data['cwidth'] = cwidth
self.data['cheight'] = cheight
self.data['font'] = self.font
self.data['size'] = self.size
self.data['outlinewidth'] = self.outlinewidth
self.data['bgcolor'] = self.bgcolor
self.data['fontcolor'] = self.fontcolor
self.data['outlinecolor'] = self.outlinecolor
self.data['imagemode'] = self.mode
self.data['fontmode'] = mode
self.data['character'] = []
# Draw a chracter into image
for char in text:
chardata = {}
# Get position
if jsonset != 0:
if char in self.glyph_dict.keys() and ncolumn != 0:
'''
for apply font's kerning settings
'''
xcharoffset = self.glyph_dict[char]["left"]
else:
xcharoffset = 0
else:
xcharoffset = 0
xpos = (ncolumn * cwidth) + xoffset - xcharoffset
ypos = (nrow * cheight) + yoffset
draw.text(
xy=(xpos, ypos),
text=char,
fill=self.fontcolor,
font=fnt,
# https://pillow.readthedocs.io/en/stable/handbook/text-anchors.html#text-anchors
anchor="la",
stroke_width=self.outlinewidth,
stroke_fill=self.outlinecolor
)
if ncolumn == (self.column-1):
nrow += 1
ncolumn = 0
else:
ncolumn += 1
# Save chracter data (for json)
chardata['char'] = char
chardata['xpos'] = xpos
chardata['ypos'] = ypos
self.data['character'].append(chardata)
self.image = img
# img.save(outputfile)
return img
def change_palette(self, p):
palette = p
colors = []
# Get palette
# if RIFF, read riff..
if os.path.splitext(palette)[1] == '.pal':
rre = rreader.palette(palette)
colors = rre.getpalette()
# if JSON, read json..
elif os.path.splitext(palette)[1] == '.json':
with open(palette, 'r', encoding="utf-8") as fjson:
json_data = json.load(fjson)
tmp = json_data['color']
for item in tmp:
t = (item[1], item[2], item[3])
colors.append(t)
else:
raise Exception("Unknown file extension")
print("Changing colors may take a while.. Please wait....")
# Linear-searching palettes with each pixel, so the algorithm should be much slow..
# It takes O(MN^2) which M is numbers of color and N of width and height..
# Of course, It should be improved, I'll do that..
im = self.image.load()
DP = [[[-1 for i in range(257)] for j in range(257)] for k in range(257)]
for x in range(0, self.width):
for y in range(0, self.height):
tidx = -1
tdifference = 987654321
if self.mode == "RGBA":
r, g, b, a = self.image.getpixel((x, y))
else:
r, g, b = self.image.getpixel((x, y))
if DP[r][g][b] != -1:
tidx = DP[r][g][b]
else:
for idx, item in enumerate(colors):
bidx, tr, tg, tb, ta = item
tr = int(tr, 16)
tg = int(tg, 16)
tb = int(tb, 16)
ta = int(ta, 16)
td = abs(r - tr) + abs(g - tg) + abs(b - tb)
if td < tdifference:
tidx = idx
tdifference = td
DP[r][g][b] = tidx
idx, ar, ag, ab, aa = colors[tidx]
ar = int(ar, 16)
ag = int(ag, 16)
ab = int(ab, 16)
aa = int(aa, 16)
if self.mode == "RGBA":
im[x, y] = (ar, ag, ab, a)
else:
im[x, y] = (ar, ag, ab)
return
def posterize_palette(self, bit):
im = self.image
self.image = ImageOps.posterize(im, bit)
return self.image
def save(self, o="output.png"):
self.image.save(o)
def dump(self, o="output.json"):
with open(o, 'w', encoding="utf-8") as fjson:
json.dump(self.data, fjson, indent=4)