Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -18,17 +18,26 @@ BASE_GD_W_IMAGE = Image.open("images/base-gd-w.png")
|
|
| 18 |
BASE_RV_W_IMAGE = Image.open("images/base-gd-w-rv.png")
|
| 19 |
|
| 20 |
BASE_IMAGE = Image.open("images/base.png")
|
|
|
|
|
|
|
| 21 |
MPLUS_FONT = ImageFont.truetype("fonts/MPLUSRounded1c-Regular.ttf", size=16)
|
|
|
|
| 22 |
# BRAND = "https://hisoka.net\nhttp://s.id/MaiSakurajima"
|
|
|
|
| 23 |
BRAND = "http://s.id/MaiSakurajima"
|
| 24 |
|
| 25 |
def getsize(font, text):
|
| 26 |
left, top, right, bottom = font.getbbox(text)
|
| 27 |
return right - left, bottom
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
fontObj = ImageFont.truetype(font, size=size)
|
|
|
|
| 32 |
|
| 33 |
pure_lines = []
|
| 34 |
pos = 0
|
|
@@ -40,50 +49,83 @@ def drawText(im, ofs, string, font="fonts/MPLUSRounded1c-Regular.ttf", size=16,
|
|
| 40 |
pure_lines.append(l)
|
| 41 |
l = ""
|
| 42 |
pos += 1
|
| 43 |
-
elif char
|
| 44 |
-
pure_lines.append(l +
|
| 45 |
-
l = ""
|
| 46 |
-
pos += 1
|
| 47 |
-
elif char == "。" or char == ".":
|
| 48 |
-
pure_lines.append(l + ("。" if char == "。" else "."))
|
| 49 |
l = ""
|
| 50 |
pos += 1
|
| 51 |
else:
|
| 52 |
l += char
|
| 53 |
pos += 1
|
| 54 |
-
|
| 55 |
if l:
|
| 56 |
pure_lines.append(l)
|
| 57 |
else:
|
| 58 |
pure_lines = string.split("\n")
|
| 59 |
|
| 60 |
lines = []
|
| 61 |
-
|
| 62 |
for line in pure_lines:
|
| 63 |
lines.extend(fw_wrap(line, width=split_len))
|
| 64 |
|
| 65 |
dy = 0
|
| 66 |
-
|
| 67 |
draw_lines = []
|
| 68 |
|
| 69 |
for line in lines:
|
| 70 |
tsize = getsize(fontObj, line)
|
| 71 |
-
print(tsize)
|
| 72 |
ofs_y = ofs[1] + dy
|
| 73 |
t_height = tsize[1]
|
| 74 |
-
|
| 75 |
-
x = int(ofs[0] - (tsize[0]/2))
|
| 76 |
draw_lines.append((x, ofs_y, line))
|
| 77 |
ofs_y += t_height + padding
|
| 78 |
dy += t_height + padding
|
| 79 |
|
| 80 |
-
adj_y = -30 * (len(draw_lines)-1)
|
|
|
|
| 81 |
for dl in draw_lines:
|
| 82 |
with Pilmoji(im) as p:
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
real_y = ofs[1] + adj_y + dy
|
| 86 |
-
|
| 87 |
return (0, dy, real_y)
|
| 88 |
|
| 89 |
def make(name, id, content, icon):
|
|
|
|
| 18 |
BASE_RV_W_IMAGE = Image.open("images/base-gd-w-rv.png")
|
| 19 |
|
| 20 |
BASE_IMAGE = Image.open("images/base.png")
|
| 21 |
+
if BASE_IMAGE.mode != "RGBA":
|
| 22 |
+
BASE_IMAGE = BASE_IMAGE.convert("RGBA")
|
| 23 |
MPLUS_FONT = ImageFont.truetype("fonts/MPLUSRounded1c-Regular.ttf", size=16)
|
| 24 |
+
MPLUS_FONTBOLD = ImageFont.truetype("fonts/MPLUSRounded1c-Bold.ttf", size=16)
|
| 25 |
# BRAND = "https://hisoka.net\nhttp://s.id/MaiSakurajima"
|
| 26 |
+
# print(MPLUS_FONT.get_variation_names())
|
| 27 |
BRAND = "http://s.id/MaiSakurajima"
|
| 28 |
|
| 29 |
def getsize(font, text):
|
| 30 |
left, top, right, bottom = font.getbbox(text)
|
| 31 |
return right - left, bottom
|
| 32 |
|
| 33 |
+
from PIL import ImageFont, ImageDraw
|
| 34 |
+
from pilmoji import Pilmoji
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def drawText(im, ofs, string, font="fonts/MPLUSRounded1c-Regular.ttf", bold_font="fonts/MPLUSRounded1c-Bold.ttf", size=16, color=(0, 0, 0, 255), split_len=None, padding=4, disable_dot_wrap=False):
|
| 38 |
+
v = ImageDraw.Draw(im)
|
| 39 |
fontObj = ImageFont.truetype(font, size=size)
|
| 40 |
+
boldFontObj = ImageFont.truetype(bold_font, size=size)
|
| 41 |
|
| 42 |
pure_lines = []
|
| 43 |
pos = 0
|
|
|
|
| 49 |
pure_lines.append(l)
|
| 50 |
l = ""
|
| 51 |
pos += 1
|
| 52 |
+
elif char in ["、", ",", "。", "."]:
|
| 53 |
+
pure_lines.append(l + char)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
l = ""
|
| 55 |
pos += 1
|
| 56 |
else:
|
| 57 |
l += char
|
| 58 |
pos += 1
|
|
|
|
| 59 |
if l:
|
| 60 |
pure_lines.append(l)
|
| 61 |
else:
|
| 62 |
pure_lines = string.split("\n")
|
| 63 |
|
| 64 |
lines = []
|
|
|
|
| 65 |
for line in pure_lines:
|
| 66 |
lines.extend(fw_wrap(line, width=split_len))
|
| 67 |
|
| 68 |
dy = 0
|
|
|
|
| 69 |
draw_lines = []
|
| 70 |
|
| 71 |
for line in lines:
|
| 72 |
tsize = getsize(fontObj, line)
|
|
|
|
| 73 |
ofs_y = ofs[1] + dy
|
| 74 |
t_height = tsize[1]
|
| 75 |
+
x = int(ofs[0] - (tsize[0] / 2))
|
|
|
|
| 76 |
draw_lines.append((x, ofs_y, line))
|
| 77 |
ofs_y += t_height + padding
|
| 78 |
dy += t_height + padding
|
| 79 |
|
| 80 |
+
adj_y = -30 * (len(draw_lines) - 1)
|
| 81 |
+
|
| 82 |
for dl in draw_lines:
|
| 83 |
with Pilmoji(im) as p:
|
| 84 |
+
parts = dl[2].split('*') # * for bold
|
| 85 |
+
xPos = dl[0]
|
| 86 |
+
yPos = adj_y + dl[1]
|
| 87 |
+
|
| 88 |
+
for i, part in enumerate(parts):
|
| 89 |
+
if i % 2 == 1:
|
| 90 |
+
p.text((xPos, yPos), part, font=boldFontObj, fill=color)
|
| 91 |
+
xPos += v.textlength(part, font=boldFontObj)
|
| 92 |
+
else:
|
| 93 |
+
subparts = part.split('_') # _ for italic
|
| 94 |
+
for j, subpart in enumerate(subparts):
|
| 95 |
+
if j % 2 == 1:
|
| 96 |
+
text_width, text_height = getsize(fontObj, subpart)
|
| 97 |
+
|
| 98 |
+
padding = 15
|
| 99 |
+
italic_text_image = Image.new("RGBA", (text_width + padding * 2, text_height + padding * 2), (0, 0, 0, 0))
|
| 100 |
+
italic_draw = ImageDraw.Draw(italic_text_image)
|
| 101 |
+
italic_draw.text((padding, padding), subpart, font=fontObj, fill=color)
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
slant_amount = 0.2
|
| 105 |
+
italic_text_image = italic_text_image.transform(
|
| 106 |
+
italic_text_image.size,
|
| 107 |
+
Image.AFFINE,
|
| 108 |
+
(1, slant_amount, 0, 0, 1, 0),
|
| 109 |
+
resample=Image.BICUBIC
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
im.alpha_composite(italic_text_image, dest=(int(xPos), int(yPos - padding)))
|
| 113 |
+
|
| 114 |
+
xPos += text_width + int(slant_amount * size)
|
| 115 |
+
else:
|
| 116 |
+
strikethrough_parts = subpart.split('~')
|
| 117 |
+
for k, s_part in enumerate(strikethrough_parts):
|
| 118 |
+
if k % 2 == 1:
|
| 119 |
+
p.text((xPos, yPos), s_part, font=fontObj, fill=color)
|
| 120 |
+
text_width = v.textlength(s_part, font=fontObj)
|
| 121 |
+
line_y = yPos + text_height // 2 # Middle of the text
|
| 122 |
+
v.line((xPos, line_y, xPos + text_width, line_y), fill=color, width=1)
|
| 123 |
+
xPos += text_width
|
| 124 |
+
else:
|
| 125 |
+
p.text((xPos, yPos), s_part, font=fontObj, fill=color)
|
| 126 |
+
xPos += v.textlength(s_part, font=fontObj)
|
| 127 |
|
| 128 |
real_y = ofs[1] + adj_y + dy
|
|
|
|
| 129 |
return (0, dy, real_y)
|
| 130 |
|
| 131 |
def make(name, id, content, icon):
|