4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the Revised BSD License.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 Revised BSD License for more details.
12 Copyright 2013 Cool Dude 2k - http://idb.berlios.de/
13 Copyright 2013 Game Maker 2k - http://intdb.sourceforge.net/
14 Copyright 2013 Kazuki Przyborowski - https://github.com/KazukiPrzyborowski
16 $FileInfo: biorhythm.py - Last Update: 11/14/2013 Ver. 1.2.4 RC 2 - Author: cooldude2k $
19 from __future__
import division
, absolute_import
, print_function
;
20 import sys
, os
, re
, time
, datetime
, math
, cmath
, decimal
, argparse
;
21 from PIL
import Image
, ImageDraw
, ImageFont
;
22 if(__name__
== "__main__"):
23 sys
.tracebacklimit
= 0;
24 __version_info__
= (1, 2, 4, "RC 2");
25 if(__version_info__
[3]!=None):
26 __version__
= str(__version_info__
[0])+"."+str(__version_info__
[1])+"."+str(__version_info__
[2])+" "+str(__version_info__
[3]);
27 if(__version_info__
[3]==None):
28 __version__
= str(__version_info__
[0])+"."+str(__version_info__
[1])+"."+str(__version_info__
[2]);
30 def drawColorLine( ctx
, x1
, y1
, x2
, y2
, color
):
31 ctx
.line((x1
, y1
, x2
, y2
), fill
= color
);
32 def drawColorRectangle( ctx
, x1
, y1
, x2
, y2
, color
):
33 ctx
.rectangle([(x1
, y1
), (x2
, y2
)], fill
= color
);
34 #def drawColorText( ctx, size, x, y, text, color ):
35 # font = ImageFont.truetype(os.path.dirname(__file__)+os.sep+"OCRB.otf", size);
37 # ctx.text((x, y), text, font = font, fill = color);
39 def drawColorRectangleAlt( ctx
, x1
, y1
, x2
, y2
, color
):
40 ctx
.rectangle([(x1
, y1
), (x2
, y2
)], outline
= color
);
41 def CalcRhythm(daysAlive
, period
, multi
= 100):
42 return decimal
.Decimal(1 - math
.sin((daysAlive
% period
) / period
* 2 * math
.pi
) * multi
);
43 def CalcRhythmAlt(daysAlive
, period
):
44 return decimal
.Decimal(1 - math
.sin((daysAlive
% period
) / period
* 2 * math
.pi
));
45 def CalcRoundRhythm(daysAlive
, period
, multi
= 100):
46 return decimal
.Decimal(1 - math
.sin((daysAlive
% period
) / period
* 2 * math
.pi
) * multi
).quantize(decimal
.Decimal(1.0));
47 def CalcRoundRhythmAlt(daysAlive
, period
, multi
= 100):
48 return decimal
.Decimal(1 - math
.sin((daysAlive
% period
) / period
* 2 * math
.pi
)).quantize(decimal
.Decimal(1.0));
50 return map(str, value
.split(","))
51 parser
= argparse
.ArgumentParser(conflict_handler
= "resolve", add_help
= True);
52 parser
.add_argument("birthday", help = "enter your birthday in MM/DD/YYYY format");
53 parser
.add_argument("-c", "--cdate", default
= None, help = "enter center date");
54 parser
.add_argument("-b", "--backward", default
= 15, help = "number of days to show before center date");
55 parser
.add_argument("-f", "--forward", default
= 15, help = "number of days to show affter center date");
56 parser
.add_argument("-x", "--scalex", default
= 10, help = "number of pixels per periods");
57 parser
.add_argument("-s", "--show", default
= "emotional,physical,intellectual", type=csv
, help = "show theses on chart in csv format");
58 parser
.add_argument("-v", "--version", action
= "version", version
= __version__
);
59 parser
.add_argument("-V", "--verbose", action
= "store_true", help = "print various debugging information");
60 parser
.add_argument("-o", "--output", default
= None, help = "input name of output image");
61 parser
.add_argument("-d", "--display", action
= "store_true", help = "display image");
62 getargs
= parser
.parse_args();
64 bdayinfo
= time
.strptime(getargs
.birthday
, "%m/%d/%y");
66 bdayinfo
= time
.strptime(getargs
.birthday
, "%m/%d/%Y");
67 numdaysbackward
= int(getargs
.backward
);
68 numdaysforward
= int(getargs
.forward
);
69 pre_biorhythm
= Image
.new("RGB", (((numdaysbackward
+ numdaysforward
) - 1) * int(getargs
.scalex
), 210));
70 biorhythm_img
= ImageDraw
.Draw(pre_biorhythm
);
71 biorhythm_img
.rectangle([(0, 0), (((numdaysbackward
+ numdaysforward
) - 1) * int(getargs
.scalex
), 210)], fill
= (255, 255, 255));
72 drawColorLine(biorhythm_img
, 0, 105, ((numdaysbackward
+ numdaysforward
) - 1) * int(getargs
.scalex
), 105, (0, 0, 0));
73 drawColorLine(biorhythm_img
, ((numdaysbackward
+ numdaysforward
) * int(getargs
.scalex
)) / 2, 0, ((numdaysbackward
+ numdaysforward
) * int(getargs
.scalex
)) / 2, 210, (0, 0, 0));
75 endloop
= numdaysbackward
+ numdaysforward
;
76 while(startloop
<endloop
):
77 if(startloop
==(numdaysbackward
- 1)):
78 startloop
= startloop
+ 1;
79 drawColorLine(biorhythm_img
, (startloop
+ 1) * int(getargs
.scalex
), 0, (startloop
+ 1) * int(getargs
.scalex
), 5, (0, 0, 0));
80 drawColorLine(biorhythm_img
, (startloop
+ 1) * int(getargs
.scalex
), 205, (startloop
+ 1) * int(getargs
.scalex
), 210, (0, 0, 0));
81 startloop
= startloop
+ 1;
84 while(startloop
<endloop
):
86 startloop
= startloop
+ 1;
87 drawColorLine(biorhythm_img
, 0, (startloop
+ 1) * 15, 5, (startloop
+ 1) * 15, (0, 0, 0));
88 drawColorLine(biorhythm_img
, (((numdaysbackward
+ numdaysforward
) - 1) * int(getargs
.scalex
)) - 5, (startloop
+ 1) * 15, ((numdaysbackward
+ numdaysforward
) - 1) * int(getargs
.scalex
), (startloop
+ 1) * 15, (0, 0, 0));
89 startloop
= startloop
+ 1;
90 if(not getargs
.cdate
==None):
92 curdayinfo
= time
.strptime(getargs
.cdate
, "%m/%d/%y");
94 curdayinfo
= time
.strptime(getargs
.cdate
, "%m/%d/%Y");
95 currentdate
= datetime
.date(curdayinfo
[0], curdayinfo
[1], curdayinfo
[2]);
96 if(getargs
.cdate
==None):
97 currentdaytime
= datetime
.datetime
.now();
98 currentdate
= datetime
.date(currentdaytime
.year
, currentdaytime
.month
, currentdaytime
.day
);
99 birthdate
= datetime
.date(bdayinfo
[0], bdayinfo
[1], bdayinfo
[2]);
100 startdate
= currentdate
- datetime
.timedelta(days
= numdaysbackward
);
101 enddate
= currentdate
+ datetime
.timedelta(days
= numdaysforward
);
104 if(getargs
.verbose
==True):
105 print("birthday: "+str(birthdate
.month
)+"/"+str(birthdate
.day
)+"/"+str(birthdate
.year
));
107 curyear
=str(curdate
.year
);
108 curmonth
=str(curdate
.month
);
109 if(len(curmonth
)==1):
110 curmonth
= "0"+curmonth
;
111 curday
=str(curdate
.day
);
112 if(len(curmonth
)==1):
113 curday
= "0"+curmonth
;
114 endyear
=str(enddate
.year
);
115 endmonth
=str(enddate
.month
);
116 if(len(endmonth
)==1):
117 endmonth
= "0"+endmonth
;
118 endday
=str(enddate
.day
);
121 while(int(curyear
+curmonth
+curday
)<int(endyear
+endmonth
+endday
)):
122 birthdays
= abs((curdate
-birthdate
).days
);
123 if(getargs
.verbose
==True):
124 print("number: "+str(curnum
));
125 curpos
= (curnum
-1) * int(getargs
.scalex
);
126 if(getargs
.verbose
==True):
127 print("date: "+str(curdate
.month
)+"/"+str(curdate
.day
)+"/"+str(curdate
.year
));
128 emotional
= CalcRoundRhythm(birthdays
, 28);
129 if(curnum
==1 and "emotional" in getargs
.show
):
130 drawColorLine(biorhythm_img
, curpos
, (emotional
+ 104), curpos
, (emotional
+ 104), (51, 128, 51));
131 if(curnum
>1 and "emotional" in getargs
.show
):
132 drawColorLine(biorhythm_img
, oldpos
, (oldemotional
+ 104), curpos
, (emotional
+ 104), (51, 128, 51));
133 if(getargs
.verbose
==True and "emotional" in getargs
.show
):
134 print("emotional: "+str(emotional
));
135 oldemotional
= emotional
;
136 physical
= CalcRoundRhythm(birthdays
, 23);
137 if(curnum
==1 and "physical" in getargs
.show
):
138 drawColorLine(biorhythm_img
, curpos
, (physical
+ 104), curpos
, (physical
+ 104), (153, 51, 51));
139 if(curnum
>1 and "physical" in getargs
.show
):
140 drawColorLine(biorhythm_img
, oldpos
, (oldphysical
+ 104), curpos
, (physical
+ 104), (153, 51, 51));
141 if(getargs
.verbose
==True and "physical" in getargs
.show
):
142 print("physical: "+str(physical
));
143 oldphysical
= physical
;
144 intellectual
= CalcRoundRhythm(birthdays
, 33);
145 if(curnum
==1 and "intellectual" in getargs
.show
):
146 drawColorLine(biorhythm_img
, curpos
, (intellectual
+ 104), curpos
, (intellectual
+ 104), (51, 51, 170));
147 if(curnum
>1 and "intellectual" in getargs
.show
):
148 drawColorLine(biorhythm_img
, oldpos
, (oldintellectual
+ 104), curpos
, (intellectual
+ 104), (51, 51, 170));
149 if(getargs
.verbose
==True and "intellectual" in getargs
.show
):
150 print("intellectual: "+str(intellectual
));
151 oldintellectual
= intellectual
;
152 average
= decimal
.Decimal((physical
+ emotional
+ intellectual
) / 3).quantize(decimal
.Decimal(1.0));
153 if(curnum
==1 and "average" in getargs
.show
):
154 drawColorLine(biorhythm_img
, curpos
, (average
+ 104), curpos
, (average
+ 104), (0, 0, 0));
155 if(curnum
>1 and "average" in getargs
.show
):
156 drawColorLine(biorhythm_img
, oldpos
, (oldaverage
+ 104), curpos
, (average
+ 104), (0, 0, 0));
157 if(getargs
.verbose
==True and "average" in getargs
.show
):
158 print("average: "+str(average
));
159 oldaverage
= average
;
160 spiritual
= CalcRoundRhythm(birthdays
, 53);
161 if(curnum
==1 and "spiritual" in getargs
.show
):
162 drawColorLine(biorhythm_img
, curpos
, (spiritual
+ 104), curpos
, (spiritual
+ 104), (89, 51, 189));
163 if(curnum
>1 and "spiritual" in getargs
.show
):
164 drawColorLine(biorhythm_img
, oldpos
, (oldspiritual
+ 104), curpos
, (spiritual
+ 104), (89, 51, 189));
165 if(getargs
.verbose
==True and "spiritual" in getargs
.show
):
166 print("spiritual: "+str(spiritual
));
167 oldspiritual
= spiritual
;
168 intuition
= CalcRoundRhythm(birthdays
, 38);
169 if(curnum
==1 and "intuition" in getargs
.show
):
170 drawColorLine(biorhythm_img
, curpos
, (intuition
+ 104), curpos
, (intuition
+ 104), (100, 60, 51));
171 if(curnum
>1 and "intuition" in getargs
.show
):
172 drawColorLine(biorhythm_img
, oldpos
, (oldintuition
+ 104), curpos
, (intuition
+ 104), (100, 60, 51));
173 if(getargs
.verbose
==True and "intuition" in getargs
.show
):
174 print("intuition: "+str(intuition
));
175 oldintuition
= intuition
;
176 awareness
= CalcRoundRhythm(birthdays
, 48);
177 if(curnum
==1 and "awareness" in getargs
.show
):
178 drawColorLine(biorhythm_img
, curpos
, (awareness
+ 104), curpos
, (awareness
+ 104), (51, 138, 144));
179 if(curnum
>1 and "awareness" in getargs
.show
):
180 drawColorLine(biorhythm_img
, oldpos
, (oldawareness
+ 104), curpos
, (awareness
+ 104), (51, 138, 144));
181 if(getargs
.verbose
==True and "awareness" in getargs
.show
):
182 print("awareness: "+str(awareness
));
183 oldawareness
= awareness
;
184 aesthetic
= CalcRoundRhythm(birthdays
, 43);
185 if(curnum
==1 and "aesthetic" in getargs
.show
):
186 drawColorLine(biorhythm_img
, curpos
, (aesthetic
+ 104), curpos
, (aesthetic
+ 104), (171, 51, 141));
187 if(curnum
>1 and "aesthetic" in getargs
.show
):
188 drawColorLine(biorhythm_img
, oldpos
, (oldaesthetic
+ 104), curpos
, (aesthetic
+ 104), (171, 51, 141));
189 if(getargs
.verbose
==True and "aesthetic" in getargs
.show
):
190 print("aesthetic: "+str(aesthetic
));
191 oldaesthetic
= aesthetic
;
193 if(getargs
.verbose
==True):
195 curdate
= curdate
+ datetime
.timedelta(days
= 1);
196 curyear
=str(curdate
.year
);
197 curmonth
=str(curdate
.month
);
198 if(len(curmonth
)==1):
199 curmonth
= "0"+curmonth
;
200 curday
=str(curdate
.day
);
201 if(len(curmonth
)==1):
202 curday
= "0"+curmonth
;
204 if(getargs
.display
==True):
205 pre_biorhythm
.show();
206 if(not getargs
.output
==None):
207 pre_biorhythm
.save(getargs
.output
);