-
Notifications
You must be signed in to change notification settings - Fork 0
/
PiMondrian.py
187 lines (133 loc) · 5.07 KB
/
PiMondrian.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
#!/usr/bin/python
#coding: utf-8
# CopyRight 2013 Allan Psicobyte ([email protected])
#
# 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/>.
import hashlib, ImageDraw
import Image, ImageFont
Iterations= 4
Gallery= 20
WIDTH= 800
HEIGHT= 800
LINE= 2
TableauName= "PiMondrian"
ArrayCuadros= []
class SubRectangle:
"""crea un rectángulo con coordenadas y color como subdivisión de un rectágulo padre"""
def __init__(self, orden, division, color, parent= None):
self.Parent= parent
self.Color= color
if parent is None:
self.ZeroX= 0
self.ZeroY= 0
self.EndX= WIDTH
self.EndY= HEIGHT
self.VorH= "V"
self.Generation= 0
else:
self.Generation= parent.Generation + 1
if parent.VorH == "V":
self.VorH= "H"
if orden == 1:
self.ZeroX= parent.ZeroX
self.ZeroY= parent.ZeroY
self.EndX= Division(parent.ZeroX, parent.EndX, division)
self.EndY= parent.EndY
elif orden == 2:
self.ZeroX= Division(parent.ZeroX, parent.EndX, division)
self.ZeroY= parent.ZeroY
self.EndX= parent.EndX
self.EndY= parent.EndY
elif parent.VorH == "H":
self.VorH= "V"
if orden == 1:
self.ZeroX= parent.ZeroX
self.ZeroY= parent.ZeroY
self.EndX= parent.EndX
self.EndY= Division(parent.ZeroY, parent.EndY, division)
elif orden == 2:
self.ZeroX= parent.ZeroX
self.ZeroY= Division(parent.ZeroY, parent.EndY, division)
self.EndX= parent.EndX
self.EndY= parent.EndY
def DivideRectangle(parent):
"""Toma los siguientes tres dígitos de Pi, y divide el cuadro en dos usando como proporción el primero de esos tres dígitos. Los otros dos digitos asignan el color de los cuadros """
global PI, ArrayCuadros
division= int(PI.pop(0))
color1= int(PI.pop(0))
color2= int(PI.pop(0))
ArrayCuadros.append(SubRectangle(1,division,color1,parent))
if division > 0:
ArrayCuadros.append(SubRectangle(2,division,color2,parent))
def Division(corta,larga,division):
"""Calcula el punto que coresponde a 'división' décimas partes entre 'corta' y 'larga' """
if division == 0:
division = 10
#resultado= larga
#else:
resultado= corta + ((larga - corta) * division / 10)
resultado= round(resultado,0)
return resultado
def PintaCuadro(generation,name):
global WIDTH, HEIGHT, LINE, ArrayCuadros
img = Image.new("RGB", (WIDTH, HEIGHT), "#000000")
draw = ImageDraw.Draw(img)
for elemento in ArrayCuadros:
if elemento.Color == 0:
FillColor= "#FFFFFF"
if elemento.Color == 1:
FillColor= "#FFFFFF"
if elemento.Color == 2:
FillColor= "#FFFFFF"
if elemento.Color == 3:
FillColor= "#AA0000"
if elemento.Color == 4:
FillColor= "#AA0000"
if elemento.Color == 5:
FillColor= "#0000AA"
if elemento.Color == 6:
FillColor= "#0000AA"
if elemento.Color == 7:
FillColor= "#000000"
if elemento.Color == 8:
FillColor= "#000000"
if elemento.Color == 9:
FillColor= "#AAAA00"
linea= round(LINE / 2,0)
if elemento.Generation == generation:
draw.rectangle([(elemento.ZeroX + linea, elemento.ZeroY + linea), (elemento.EndX - linea, elemento.EndY - linea)], outline="#000000", fill= FillColor)
# name = name + ".png"
return img
def Inspiration(Iterations):
global ArrayCuadros
ArrayCuadros= []
# Creamos el rectángulo raiz
ArrayCuadros.append(SubRectangle(1,0,0))
# Creamos el resto de rectángulos
i = 0
while i < Iterations:
for elemento in ArrayCuadros:
if elemento.Generation == i:
DivideRectangle(elemento)
i = i + 1
def numerical_hash(key):
hash = hashlib.sha256(key)
return str(int(hash.hexdigest(),base=16))
def mondrian(entrada):
global PI
Cadena = numerical_hash(entrada)
PI = list(Cadena)
Inspiration(Iterations)
salida = PintaCuadro(Iterations,TableauName)
return salida