-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindia.py
87 lines (63 loc) · 2.07 KB
/
india.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
from PIL import Image # For reading image
from time import sleep
from random import randint, shuffle
from turtle import *
sq_side = 7 # Side of square to be drawn for each pixel
margin = 2 # Margin between squares
black = (0,0,0) # Color to be ignored (background color)
size = sq_side + margin * 2 # Total size of square to be drawn (including margin)
random_draw = True # If True, squares will be drawn in random order
img = Image.open('image_processing/india-final.png') # Input image file
# Read the input image
pixels = []
colors = set()
for i in range(img.size[0]):
pixels.append([])
for j in range(img.size[1]):
pixels[i].append(img.getpixel((i,j)))
colors.add(img.getpixel((i,j)))
# Remove background color from colors
colors.remove(black)
colors = list(colors)
# Size of the picture being drawn
xmax, ymax = len(pixels), len(pixels[0])
offset_x = -(xmax * size) // 2
offset_y = -(ymax * size) // 2
# List of coordinates to draw (x, y, color)
# Here, x and y are in turtle coordinates (cartesian form)
coordinates_to_draw = []
for y in range(len(pixels[0])):
for x in range(len(pixels)):
if pixels[x][y] == black:
continue
coordinates_to_draw.append((
offset_x + x * size,
offset_y + (ymax - 1 - y) * size,
pixels[x][y]
))
if random_draw:
shuffle(coordinates_to_draw)
# Initialize turtle and Screen
turtle = Turtle()
screen = Screen()
screen.colormode(255)
turtle.speed(0)
screen.bgcolor(black)
turtle.pendown()
turtle.goto(offset_x, offset_y)
turtle.penup()
# Just for suspense :)
sleep(3)
# Draw the picture
for x, y, col in coordinates_to_draw:
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.color(col)
turtle.begin_fill()
for i in range(4):
turtle.forward(sq_side)
turtle.left(90)
turtle.end_fill()
turtle.hideturtle()
exitonclick()