This repository was archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_processing.py
More file actions
85 lines (68 loc) · 2.36 KB
/
image_processing.py
File metadata and controls
85 lines (68 loc) · 2.36 KB
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
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import textwrap
# Resources and Output
BASE_IMAGE_PATH = "./assets/base_image.jpg"
FONT_PATH = "./assets/pixChicago.ttf"
RESULT_IMAGE_PATH = "./output/{}.jpg"
# Parameters for drawing text
TEXT_START_LOCATION = (370, 90)
COLOR = (0,0,0)
LINE_SPACING = 10
MAX_WIDTH = 560
MAX_HEIGHT = 270
# Defaults for wrapping text
MAX_FONT = 35
MIN_FONT = 20
MAX_WRAP = 35
MIN_WRAP = 30
STEP = 1
def make_image(text, filename='result'):
path = RESULT_IMAGE_PATH.format(filename)
with Image.open(BASE_IMAGE_PATH) as img:
# Get a drawing context
draw = ImageDraw.Draw(img)
text, font_size = find_text_parameters(draw, text)
if not text:
return
# Uncomment to check bounding box for text
# draw.rectangle(
# (TEXT_START_LOCATION,
# (TEXT_START_LOCATION[0] + MAX_WIDTH, TEXT_START_LOCATION[1] + MAX_HEIGHT)
# ), fill=(255,0,0), outline=None)
font = ImageFont.truetype(FONT_PATH, font_size)
draw.multiline_text(TEXT_START_LOCATION,
text,
COLOR,
font=font,
spacing=LINE_SPACING,
align='left')
img.save(path)
return path
def find_text_parameters(draw, text):
font_size = MAX_FONT
wrap = MAX_WRAP
doesFit = False
while not doesFit:
wrapped = textwrap.fill(text, wrap)
doesFit, tooWide, tooTall = check_fit(draw, wrapped, font_size)
# if we bottom out on a lower limit of font or wrap, log that we can't do it
# along with the text
if font_size <= MIN_FONT and wrap <= MAX_WRAP:
print("Error: Text doesn't fit! Text: {}".format(text))
return None, None
# if it's too tall, reduce font size and check again
if tooTall:
font_size -= STEP
# if it's too wide, reduce wrap and check again
elif tooWide:
wrap -= STEP
return wrapped, font_size
def check_fit(draw, text, font_size):
font = ImageFont.truetype(FONT_PATH, font_size)
w,h = draw.multiline_textsize(text, font=font, spacing=LINE_SPACING)
tooWide = w > MAX_WIDTH
tooTall = h > MAX_HEIGHT
doesFit = not tooWide and not tooTall
return doesFit, tooWide, tooTall