-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunctions.py
88 lines (74 loc) · 2.84 KB
/
functions.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
from PIL import Image, ImageDraw, ImageFont
import textwrap
import json
import random
import os
from config import *
def generate():
dir = os.path.dirname(__file__)
if gen:
mg = MaterialGenerator()
qg = QuoteGenerator()
quote = qg.get_quote()
image = mg.draw_image(quote['quoteText'], (4000, 4000))
image.save(os.path.join(dir, 'data/image.jpg'), 'JPEG')
else:
mg = BackgroundGenerator()
qg = QuoteGenerator()
quote = qg.get_quote()
image = mg.draw_image(quote['quoteText'], (4000, 4000))
image.save(os.path.join(dir, 'data/image.jpg'), 'JPEG')
def get_random_color():
dir = os.path.dirname(__file__)
colors = json.load(open(os.path.join(dir, 'data/material-colors.json')))
color = random.choice(list(colors.keys()))
return colors[color]['500']
class MaterialGenerator:
def draw_image(self, text, size):
dir = os.path.dirname(__file__)
im = Image.new('RGB', size)
draw = ImageDraw.Draw(im)
draw.rectangle(((0, 0), size), fill=get_random_color())
font = ImageFont.truetype(os.path.join(
dir, 'fonts/DeFonarts-Bold.otf'), 280)
lines = textwrap.wrap(text, 20)
line_dimensions = [draw.textsize(line, font=font) for line in lines]
offset = (size[1] - sum(h for w, h in line_dimensions)) // 2
for line in lines:
w, h = draw.textsize(line, font=font)
pos = ((size[0] - w) / 2, offset)
draw.text(pos, line, font=font, fill=(255, 255, 255))
offset += font.getsize(line)[1]
del draw
return im
class BackgroundGenerator:
def draw_image(self, text, size):
dir = os.path.dirname(__file__)
im = Image.open('data/result.jpg')
draw = ImageDraw.Draw(im)
font = ImageFont.truetype(os.path.join(
dir, 'fonts/DeFonarts-Bold.otf'), 280)
lines = textwrap.wrap(text, 20)
line_dimensions = [draw.textsize(line, font=font) for line in lines]
offset = (size[1] - sum(h for w, h in line_dimensions)) // 2
for line in lines:
w, h = draw.textsize(line, font=font)
pos = ((size[0] - w) / 2, offset)
draw.text(pos, line, font=font, fill=(255, 255, 255))
offset += font.getsize(line)[1]
del draw
return im
class QuoteGenerator:
def get_quote(self):
dir = os.path.dirname(__file__)
quotes = self.filter_quotes(json.load(
open(os.path.join(dir, 'data/quotes.json'), encoding='latin1')), 150, 15)
quote = random.choice(quotes)
return quote
def filter_quotes(self, quotes, lenQ, lenA):
_quotes = []
for quote in quotes:
lQ = quote['quoteText'].__len__()
if lQ <= lenQ:
_quotes.append(quote)
return _quotes