-
Notifications
You must be signed in to change notification settings - Fork 8
/
generate_logo.py
67 lines (53 loc) · 1.81 KB
/
generate_logo.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
import io
from bs4 import BeautifulSoup as bs
import pathlib
from os import path
from PIL import Image
from wordcloud import STOPWORDS, ImageColorGenerator, WordCloud
import httpx
import matplotlib.pyplot as plt
import numpy as np
def generate_fig(url, mask_path):
logo_path = mask_path.parent / "logo.png"
if not logo_path.exists():
parsed_content = parse_content(url)
wc = generate_wordcloud(parsed_content,np.array(Image.open(mask_path)))
generate_image(logo_path, wc, np.array(Image.open(mask_path)))
return "/static/images/logo.png"
def parse_content(url):
parsed_content = bs(httpx.get(url).content, features="html.parser")
clean_raw_content = "".join(parsed_content.findAll(text=True))
return clean_raw_content
def generate_wordcloud(content, mask=None):
stopwords = STOPWORDS | {
"see",
"use",
"using",
"tutorial",
"Node",
"js",
"file",
}
wc = WordCloud(
background_color="black",
max_words=2000,
mask=mask,
contour_width=10,
contour_color="white",
stopwords=stopwords,
)
return wc.generate(content)
def generate_image(logo_path, wc, mask=None):
fig, axes = plt.subplots(1, 1)
plt.axis("off")
if mask is not None:
image_colors = ImageColorGenerator(mask)
wc = wc.recolor(color_func=image_colors)
axes.imshow(wc, interpolation="bilinear")
plt.savefig(logo_path, format="png", facecolor="black")
if __name__ == "__main__":
mask_path = (
pathlib.Path(__file__).parent / "logo_app" / "static" / "images" / "python-colored-mask.png"
)
generate_fig("https://code.visualstudio.com/docs/python/python-tutorial",mask_path)
print("Logo generated!")