forked from leonardcj/pointillism
-
Notifications
You must be signed in to change notification settings - Fork 0
/
puntillism.py
executable file
·237 lines (193 loc) · 7.9 KB
/
puntillism.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Pointillism
# Copyright (C) 2008, Nirav Patel
# Copyright (C) 2011, 2012, Alan Aguiar
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Contact information:
# Alan Aguiar <[email protected]>
# Nirav Patel <[email protected]>
from gettext import gettext as _
import random
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
try:
import pygame
from pygame import camera
except (ImportError, ModuleNotFoundError):
print('Error in import Pygame. This activity requires Pygame 1.9')
class Puntillism():
def __init__(self, parent):
self.parent = parent
self.file_path = "NULL"
self._paused = False
def poner_radio1(self, radio):
self.radio1 = radio
def poner_radio2(self, radio):
self.radio2 = radio
def set_paused(self, paused):
self._paused = paused
def run(self):
pygame.init()
pygame.camera.init()
self.radio1 = 2
self.radio2 = 12
# create a local variable to check camera not found and in loop
self.load_image_loop = False
# Declare a font
font = pygame.font.Font('freesansbold.ttf', 32)
screen = pygame.display.get_surface()
screen.fill((0, 0, 0))
pygame.display.flip()
x_s, y_s = (1200, 900)
x_s, y_s = screen.get_size()
clock = pygame.time.Clock()
self.running = True
self.has_camera = False
try:
cam = camera.Camera("/dev/video0", (640, 480), "RGB")
cam.start()
self.has_camera = True
except SystemError:
self.running = False
self.has_camera = False
try:
cam.set_controls(hflip=True)
except SystemError:
pass
cap = pygame.surface.Surface((640, 480), 0, screen)
frames = 0
if self.running:
screen.fill((0, 0, 0))
pygame.display.update()
while self.running:
cap = cam.get_image(cap)
rect = []
self.create_rect(cap, rect, frames, clock, screen, x_s, y_s)
# GTK events
while Gtk.events_pending():
Gtk.main_iteration()
events = pygame.event.get()
self.read_events(events, screen, font, x_s, y_s)
# define some colors for not cam found
message = _('Camera not found')
if not self.has_camera:
print("LOG: No /dev/video0 found")
self.load_image_loop = True
# after checking if camera exists, cam is not accepted
# if /dev/video0 > null ually null gives a black screen,
# before initiating the display, A black screen is filled
screen.fill((0, 0, 0))
pygame.display.update()
if self.load_image_loop:
self.image_load_handler(screen, frames,
x_s, y_s, clock, message, font)
def create_rect(self, cad, rect, frames, clock, screen, x_size, y_size):
for z in range(max(20, int(frames) * 10)):
if self._paused:
break
x = random.random()
y = random.random()
if self.radio1 > self.radio2:
aux = self.radio2
self.radio2 = self.radio1
self.radio1 = aux
elif self.radio1 == self.radio2:
self.radio2 = self.radio2 + 1
num = random.randrange(self.radio1, self.radio2, 1)
rect.append(pygame.draw.circle(
screen,
cad.get_at((int(x * 640), int(y * 480))),
(int(x * x_size), int(y * y_size)),
num, 0))
pygame.display.update()
pygame.display.update(rect)
clock.tick()
frames = clock.get_fps()
def read_events(self, events, screen, font, x_s, y_s):
for event in events:
if event.type == pygame.QUIT:
self.running = False
self.load_image_loop = False
elif event.type == pygame.VIDEORESIZE:
pygame.display.set_mode(event.size, pygame.RESIZABLE)
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.running = False
self.load_image_loop = False
elif event.key == pygame.K_s:
self.parent.save_image(screen)
elif event.type == pygame.USEREVENT:
if hasattr(event, 'action'):
if event.action == 'savebutton':
self.parent.save_image(screen)
if event.action == 'openbutton':
# ObjectChooser might take a few seconds to load
# A text message might be needed to reflect
# the loading ObjectChooser
text = font.render(
("Loading Image Selector..."),
True, (255, 255, 255), (0, 0, 0))
text_frame = text.get_rect()
screen.fill((0, 0, 0))
screen.blit(text, text_frame)
text_frame.center = (x_s // 2, y_s // 2)
pygame.display.update()
# Get Image file Path from ObjectChooser
self.file_path = self.parent.return_image_to_pygame()
if self.file_path is not None:
screen.fill((0, 0, 0))
pygame.display.update()
self.running = False
self.load_image_loop = True
else:
# The jobject either didn't return anything
# Some error might have happened,
# while selecting the image
# Just leave it with the current view,
# if nothings gone wrong
pygame.display.update()
pass
def image_load_handler(
self, screen, frames, x_s, y_s, clock, message, font):
screen.fill((0, 0, 0))
pygame.display.update()
if not self.has_camera:
text = font.render(message, True, (255, 255, 255), (0, 0, 0))
text_frame = text.get_rect()
text_frame.center = (x_s // 2, y_s // 2)
else:
text = font.render(_("Click the Load Image Button"),
True, (255, 255, 255), (0, 0, 0))
text_frame = text.get_rect()
text_frame.center = (x_s // 2, y_s // 2)
while self.load_image_loop:
if (self.file_path is not None) and (self.file_path != "NULL"):
cad = pygame.image.load(self.file_path).convert()
cad = pygame.transform.scale(cad, (640, 480))
rect = []
self.create_rect(cad, rect, frames, clock, screen, x_s, y_s)
else:
screen.fill((0, 0, 0))
screen.blit(text, text_frame)
pygame.display.update()
# GTK events
while Gtk.events_pending():
Gtk.main_iteration()
events = pygame.event.get()
self.read_events(events, screen, font, x_s, y_s)