-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemeifyhelpers.py
134 lines (117 loc) · 4.75 KB
/
memeifyhelpers.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
from os import getcwd
from os.path import join
from textwrap import wrap
import numpy as np
from colour import Color as asciiColor
from PIL import Image, ImageDraw, ImageFont
from wand.color import Color
from wand.drawing import Drawing
from wand.image import Image as zedimage
from .utils import _zedutils
MARGINS = [50, 150, 250, 350, 450]
def asciiart(in_f, SC, GCF, out_f, color1, color2, bgcolor="black"):
chars = np.asarray(list(" .,:irs?@9B&#"))
font = ImageFont.load_default()
letter_width = font.getsize("x")[0]
letter_height = font.getsize("x")[1]
WCF = letter_height / letter_width
img = Image.open(in_f)
widthByLetter = round(img.size[0] * SC * WCF)
heightByLetter = round(img.size[1] * SC)
S = (widthByLetter, heightByLetter)
img = img.resize(S)
img = np.sum(np.asarray(img), axis=2)
img -= img.min()
img = (1.0 - img / img.max()) ** GCF * (chars.size - 1)
lines = ("\n".join(("".join(r) for r in chars[img.astype(int)]))).split("\n")
nbins = len(lines)
colorRange = list(asciiColor(color1).range_to(asciiColor(color2), nbins))
newImg_width = letter_width * widthByLetter
newImg_height = letter_height * heightByLetter
newImg = Image.new("RGBA", (newImg_width, newImg_height), bgcolor)
draw = ImageDraw.Draw(newImg)
leftpadding = 0
y = 0
for lineIdx, line in enumerate(lines):
color = colorRange[lineIdx]
draw.text((leftpadding, y), line, color.hex, font=font)
y += letter_height
if newImg.mode != "RGB":
newImg = newImg.convert("RGB")
newImg.save(out_f)
def get_warp_length(width):
return int((20.0 / 1024.0) * (width + 0.0))
async def zed_meme(CNG_FONTS, topString, bottomString, filename, endname):
img = Image.open(filename)
imageSize = img.size
# find biggest font size that works
fontSize = int(imageSize[1] / 20)
font = ImageFont.truetype(CNG_FONTS, fontSize)
topTextSize = font.getsize(topString)
bottomTextSize = font.getsize(bottomString)
while topTextSize[0] > imageSize[0] - 20 or bottomTextSize[0] > imageSize[0] - 20:
fontSize -= 1
font = ImageFont.truetype(CNG_FONTS, fontSize)
topTextSize = font.getsize(topString)
bottomTextSize = font.getsize(bottomString)
# find top centered position for top text
topTextPositionX = (imageSize[0] / 2) - (topTextSize[0] / 2)
topTextPositionY = 0
topTextPosition = (topTextPositionX, topTextPositionY)
# find bottom centered position for bottom text
bottomTextPositionX = (imageSize[0] / 2) - (bottomTextSize[0] / 2)
bottomTextPositionY = imageSize[1] - bottomTextSize[1]
bottomTextPosition = (bottomTextPositionX, bottomTextPositionY)
draw = ImageDraw.Draw(img)
# draw outlines
# there may be a better way
outlineRange = fontSize // 15
for x in range(-outlineRange, outlineRange + 1):
for y in range(-outlineRange, outlineRange + 1):
draw.text(
(topTextPosition[0] + x, topTextPosition[1] + y),
topString,
(0, 0, 0),
font=font,
)
draw.text(
(bottomTextPosition[0] + x, bottomTextPosition[1] + y),
bottomString,
(0, 0, 0),
font=font,
)
draw.text(topTextPosition, topString, (255, 255, 255), font=font)
draw.text(bottomTextPosition, bottomString, (255, 255, 255), font=font)
img.save(endname)
async def zed_meeme(upper_text, lower_text, CNG_FONTS, picture_name, endname):
main_image = zedimage(filename=picture_name)
main_image.resize(
1024, int(((main_image.height * 1.0) / (main_image.width * 1.0)) * 1024.0)
)
upper_text = "\n".join(wrap(upper_text, get_warp_length(main_image.width))).upper()
lower_text = "\n".join(wrap(lower_text, get_warp_length(main_image.width))).upper()
lower_margin = MARGINS[lower_text.count("\n")]
text_draw = Drawing()
text_draw.font = join(getcwd(), CNG_FONTS)
text_draw.font_size = 20
text_draw.text_alignment = "center"
text_draw.stroke_color = Color("black")
text_draw.stroke_width = 3
text_draw.fill_color = Color("white")
if upper_text:
text_draw.text((main_image.width) // 2, 80, upper_text)
if lower_text:
text_draw.text(
(main_image.width) // 2, main_image.height - lower_margin, lower_text
)
text_draw(main_image)
main_image.save(filename=endname)
async def silently_send_message(conv, text):
await conv.send_message(text)
response = await conv.get_response()
await conv.mark_read(message=response)
return response
async def thumb_from_audio(audio_path, output):
await _zedutils.runcmd(
f"ffmpeg -i {audio_path} -filter:v scale=500:500 -an {output}"
)