forked from nab331/CertiPy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
133 lines (85 loc) · 2.47 KB
/
main.py
File metadata and controls
133 lines (85 loc) · 2.47 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import pygame
from setup import *
import os
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
def center_xy(text,xy):
fsize_x , fsize_y = PIL_draw.textsize(text,font)
x = int(xy[0]-float((fsize_x))/2)
y = int(xy[1]-float((fsize_y))/2)
#print fsize_x,fsize_y
return (x,y)
def map_xy((x,y)):
x = float(x)
y = float(y)
#x_scale = float(screen_width)/width
#y_scale = float(screen_height)/height
x_scale = scale
y_scale = scale
#print x_scale,y_scale
if y>height/2:
y*=y_scale
if y<=height/2:
y*=y_scale
if x>width/2:
x*=x_scale
if x<=width/2:
x*=x_scale
#print x,y
return (int(x),int(y))
def generate_output(rect_array):
output_folder = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'Output')
with open("input.csv","rb") as f:
reader = csv.reader(f)
count=1
for row in reader:
PIL_image = Image.open(filename)
PIL_draw = ImageDraw.Draw(PIL_image)
for i in range(len(rect_array)):
#Mapping it to the original File Size and centering it with the rectangle
xy = map_xy(rect_array[i].center)
xy = center_xy(row[i],xy )
PIL_draw.text( xy ,row[i],(28,70,150),font)
output_name = os.path.join( output_folder , str(count)+".jpg" )
count+=1;
PIL_image.save(output_name)
#if count==10:
# break
fonts_path = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'fonts')
font = ImageFont.truetype(os.path.join(fonts_path, 'best_font.otf'), 80)
draw_start = False
to_draw = []
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEMOTION:
mouse_pos = mouse_x, mouse_y = pygame.mouse.get_pos()
if event.type == pygame.MOUSEBUTTONDOWN:
pos = mouse_pos
draw_start = True
if event.type == pygame.MOUSEBUTTONUP:
final_pos = mouse_pos
draw_start = False
rect = pygame.Rect(pos,(final_pos[0]- pos[0], final_pos[1]-pos[1]))
rect.normalize()
to_draw+=[rect]
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
if event.key == pygame.K_RETURN:
print to_draw[0]
generate_output(to_draw)
if event.key == pygame.K_BACKSPACE:
to_draw.pop()
#Display
screen.blit(img,(0,0))
if draw_start:
pygame.draw.rect(screen,(255,0,0), pygame.Rect(pos, (mouse_pos[0] - pos[0],mouse_pos[1]- pos[1])))
for item in to_draw:
pygame.draw.rect(screen,(0,255,0),item)
#Logic
#Update
clock.tick(30)
pygame.display.update()