-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_drawer.py
74 lines (57 loc) · 2.05 KB
/
dataset_drawer.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
import random
import pygame
import os
pygame.init()
width, height = 500, 500
grid_size = 10
pixel_size = width // grid_size
white = (255, 255, 255)
black = (0, 0, 0)
number_folder = "nine"
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("PIXELATED DIGITS -- DATASET CREATOR 3000")
grid = [[white for _ in range(grid_size)] for _ in range(grid_size)]
def draw_grid():
for row in range(grid_size):
for col in range(grid_size):
pygame.draw.rect(screen, grid[row][col],
(col * pixel_size, row * pixel_size, pixel_size, pixel_size))
pygame.draw.rect(screen, black,
(col * pixel_size, row * pixel_size, pixel_size, pixel_size), 1)
def save_image():
image_surface = pygame.Surface((grid_size, grid_size))
for row in range(grid_size):
for col in range(grid_size):
image_surface.set_at((col, row), grid[row][col])
file_path = os.path.join("data/training/" + number_folder, number_folder + str(random.randrange(0, 100000)) + ".png")
pygame.image.save(image_surface, file_path)
print(f"Image saved to {file_path}")
reset_grid()
def reset_grid():
global grid
grid = [[white for _ in range(grid_size)] for _ in range(grid_size)]
running = True
drawing = False
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
drawing = True
elif event.type == pygame.MOUSEBUTTONUP:
drawing = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
save_image()
elif event.key == pygame.K_r:
reset_grid()
if drawing:
mouse_x, mouse_y = pygame.mouse.get_pos()
col = mouse_x // pixel_size
row = mouse_y // pixel_size
if 0 <= col < grid_size and 0 <= row < grid_size:
grid[row][col] = black
screen.fill(white)
draw_grid()
pygame.display.flip()
pygame.quit()