-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwritePic.py
72 lines (64 loc) · 2.3 KB
/
writePic.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
import os
#import Image, ImageChops, ImageDraw, ImageFont, ImageFilter
from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw
import pytesseract
def removeSimilarImages(files):
filesToRemove = []
for i in range(len(files)):
try:
if float(abs(os.path.getsize(files[i]) - os.path.getsize(files[i+1]))) < 150:
filesToRemove.append(files[i+1])
except:
print "End of list"
return filesToRemove
'''def WriteToImage(output, txt, size=45, input='background.jpg'):
output = str(output).replace('.png', '').replace('.jpg', '')
i = Image.open(input)
font = ImageFont.load_default()
txt = txt.split(' ')
r = []
for part in txt:
r.append(str(part).replace(' ', ''))
txt = r
a = ''
while len(txt) > 0:
try:
for e in range(7):
item = txt[0]
txt.remove(item)
a = a + ' ' + str(item)
a = a + '\n'
except:
break
text_col = (255, 255, 255) # bright green
halo_col = (0, 0, 0) # black
i2 = draw_text_with_halo(i, (20, 40), a, font, text_col, halo_col)
i2.save('{}.png'.format(output))'''
def addText(image, text, inputs='background.jpg', font='Royal.ttf', fontsize=75, x=300, y=300):
text = text.replace('\n', ' ').replace('\t', ' ')
image = str(image).replace('.png', '').replace('.jpg', '')
saveas = image + '.png'
background = inputs
text = text.upper()
'''if len(text) > 40:
a = [text[i:i+40] for i in range(0, len(text), 40)]
if len(str(a[-1])) < 10:
a.remove(a[-1])
text = '\n'.join(a)'''
if len(text) > 40:
text = text.split(' ')
text = [" ".join(text[i:i+8]) for i in range(0, len(text), 8)]
text = '\n'.join(text)
with Image.open(inputs) as img:
width, height = img.size
y = float(height * .95)
x = width / 2
font = os.getcwd() + "/" + font
font = 'Helvetica-Bold'
os.system('convert -font {} -pointsize {} -fill black -gravity South -annotate +10+10 "{}" {} {}.png.tmp'.format(font, fontsize, text, background, image))
os.system('convert -font {} -pointsize {} -fill white -gravity South -annotate +5+5 "{}" {}.png.tmp {}.png'.format(font, fontsize, text, image, image))
#os.system('convert -font {} -pointsize {} -fill black -draw "text {},{} \'{}\'" -fill white -draw "text {},{} \'{}\'" {} {}.tmp'.format(font, fontsize, x, y, text, x-3, y-3, text, image, image))
os.system('rm {}.png.tmp'.format(image))
os.system('rm {}.jpg'.format(image))