forked from superjamie/snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevery.py
29 lines (23 loc) · 940 Bytes
/
every.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
#!/usr/bin/python
# every.py
# script to generate every 2-color image of a certain dimension
# if you want these in another format, then afterwards:
# for file in *.ppm; do convert "$file" "${file%.ppm}.png"; rm "$file"; done
# dimension is the image dimensions
# for example, dimension = 5 will make every 5x5 image
dimension = 3
number_of_images = ((2**dimension)**dimension)
namepadlen = len(str(number_of_images - 1))
pixpadlen = len(str(bin(number_of_images - 1)[2:]))
for imagenum in range(number_of_images):
filename = "img" + str(imagenum).zfill(namepadlen) + ".ppm"
file = open(filename, "w")
file.write("P3 {0} {0} 1".format(dimension))
pixels = bin(imagenum)[2:].zfill(pixpadlen)
for pixel in range(len(pixels)):
if ((pixel % dimension) == 0):
file.write("\n")
thisrow = " {0} {0} {0} ".format(pixels[pixel])
file.write(thisrow)
file.write("\n")
file.close()