-
Notifications
You must be signed in to change notification settings - Fork 63
/
app.py
101 lines (76 loc) · 2.65 KB
/
app.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
94
95
96
97
98
99
100
101
###############################################################
########## By: suryaveer @IIT Indore #########
########## GITHUB: https://github.com/surya-veer #########
###############################################################
import pygame
from process_image import get_output_image
# pre defined colors, pen radius and font color
black = [0, 0, 0]
white = [255, 255, 255]
red = [255, 0, 0]
green = [0, 255, 0]
draw_on = False
last_pos = (0, 0)
color = (255, 128, 0)
radius = 7
font_size = 500
#image size
width = 640
height = 640
# initializing screen
screen = pygame.display.set_mode((width*2, height))
screen.fill(white)
pygame.font.init()
def show_output_image(img):
surf = pygame.pixelcopy.make_surface(img)
surf = pygame.transform.rotate(surf, -270)
surf = pygame.transform.flip(surf, 0, 1)
screen.blit(surf, (width+2, 0))
def crope(orginal):
cropped = pygame.Surface((width-5, height-5))
cropped.blit(orginal, (0, 0), (0, 0, width-5, height-5))
return cropped
def roundline(srf, color, start, end, radius=1):
dx = end[0] - start[0]
dy = end[1] - start[1]
distance = max(abs(dx), abs(dy))
for i in range(distance):
x = int(start[0] + float(i) / distance * dx)
y = int(start[1] + float(i) / distance * dy)
pygame.draw.circle(srf, color, (x, y), radius)
def draw_partition_line():
pygame.draw.line(screen, black, [width, 0], [width,height ], 8)
try:
while True:
# get all events
e = pygame.event.wait()
draw_partition_line()
# clear screen after right click
if(e.type == pygame.MOUSEBUTTONDOWN and e.button == 3):
screen.fill(white)
# quit
if e.type == pygame.QUIT:
raise StopIteration
# start drawing after left click
if(e.type == pygame.MOUSEBUTTONDOWN and e.button != 3):
color = black
pygame.draw.circle(screen, color, e.pos, radius)
draw_on = True
# stop drawing after releasing left click
if e.type == pygame.MOUSEBUTTONUP and e.button != 3:
draw_on = False
fname = "out.png"
img = crope(screen)
pygame.image.save(img, fname)
output_img = get_output_image(fname)
show_output_image(output_img)
# start drawing line on screen if draw is true
if e.type == pygame.MOUSEMOTION:
if draw_on:
pygame.draw.circle(screen, color, e.pos, radius)
roundline(screen, color, e.pos, last_pos, radius)
last_pos = e.pos
pygame.display.flip()
except StopIteration:
pass
pygame.quit()