forked from kgc/botmily
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakeMacro.py
93 lines (85 loc) · 2.7 KB
/
makeMacro.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
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import textwrap
import os.path, random, string
import urllib2
from cStringIO import StringIO
def drawtext(draw, text, font, color, bbox):
shadowcolor = "black"
x0, y0, x1, y1 = bbox # note: y1 is ignored
space = draw.textsize(" ", font)[0]
#font2 = ImageFont.truetype("Impact.ttf", 13)
words = text.split()
x = x0; y = y0; h = 0
for word in words:
# check size of this word
w, h = draw.textsize(word, font)
# figure out where to draw it
if x > x0:
x += space
if x + w > x1:
# new line
x = x0
y += h
#draw.text((x, y), word, font=font, fill=color)
# thin border
draw.text((x-.5, y-.5), word, font=font, fill=shadowcolor)
draw.text((x-1.5, y-1.5), word, font=font, fill=shadowcolor)
draw.text((x-1.5, y+1.5), word, font=font, fill=shadowcolor)
draw.text((x+1.5, y-1.5), word, font=font, fill=shadowcolor)
#text
draw.text((x,y), word, font=font, fill=color)
x += w
return y + h
def getImageFromUrl(url):
opener1 = urllib2.build_opener()
page1 = opener1.open(url)
data = StringIO(page1.read())
img = Image.open(data)
return img
def makeMacro(imgUrl , text ,fileName):
image = getImageFromUrl(imgUrl)
imgWidth = image.size[0]
imgHeight = image.size[0]
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("impact.ttf", 64)
bbox = (50, 0, imgWidth, imgHeight)
drawtext(draw, text, font, "white", bbox)
image.save(fileName , 'JPEG')
def drawAchieve(text):
img = Image.open('achieve.png')
draw = ImageDraw.Draw(img)
shadowcolor = "black"
x0 = 218 + 10
y0= 128
y1 = 173
x1 = 884
font = ImageFont.truetype("arial.ttf", 45)
space = draw.textsize(" ", font)[0]
words = text.split()
x = x0; y = y0; h = 0
for word in words:
# check size of this word
w, h = draw.textsize(word, font)
# figure out where to draw it
if x > x0:
x += space
if x + w > x1:
# new line
x = x0
y += h
#text
draw.text((x,y), word, font=font, fill='white')
x += w
return img
def overlayAchieve(text,url):
target = getImageFromUrl(url)
y = drawAchieve(text)
basewidth = target.size[0]
wpercent = basewidth/float(y.size[0])
hsize = int((float(y.size[1])*float(wpercent)))
y = y.resize((basewidth,hsize),Image.ANTIALIAS)
target.paste(y ,(0,0) , y)
target.save('achieved.png','PNG' )
return 'achieved.png'