-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_custom_image.py
51 lines (40 loc) · 1.28 KB
/
create_custom_image.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
import textwrap
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
counter = 0
def put_message_on_shirt(msg, f):
global counter
x_offset = 125
y_rows = [125, 160, 195, 230, 265] # y coordinate for each line
width = 350
max_line_length = 11 # number of characters on a line
text_color = (0,0,0)
#font = ImageFont.truetype("libresansserifssk.ttf", 40)
font = ImageFont.truetype("SourceCodePro-Bold.otf", 36)
img = Image.open(f)
draw = ImageDraw.Draw(img)
lines = textwrap.wrap(msg, max_line_length)
for row, line in enumerate(lines):
if row < len(y_rows):
y = y_rows[row]
w, h = draw.textsize(line, font)
_x = x_offset + (width-w)/2
draw.text((_x, y), line, text_color, font=font)
else:
print "Message too long, not printing: '%s'" % (line)
fname = 'sample-out%d.jpg'%counter
counter += 1
img.save(fname)
return fname
if __name__ == '__main__':
# simple test
import requests
from StringIO import StringIO
from run import get_tshirt_image_url
from sys import argv
image_url = get_tshirt_image_url('Red')
r = requests.get(image_url)
f = StringIO(r.content)
f.seek(0)
put_message_on_shirt(argv[1], f)