Calendar Maker Script (for Python) Pythonでカレンダーを作る

My wife pushed me to create a calendar of her design, but I couldn't find a good image, so I decided to make one as a programmer! Here is the image.
And this is a code.

#Attention!
Holidays are Japanese ones. To know which day is holiday, I got "jholiday.py" from here.
Thank you "AddingBox"!!
Also you need to install PIL(Python Image Library).

""" CREATE LARGE CALENDER IMAGE
This code is created by tiid (http://makerwannabe.blogspot.com/)
This work is licensed under the Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.
"""
 
import Image
from PIL import ImageFont, ImageDraw
from jholiday import holiday_name
from datetime import timedelta
import datetime

class CalenderImage() :
    def __init__(self, width, height):
        self.image = Image.new("RGB", (width,height), (255,255,255))
        self.draw = ImageDraw.Draw(self.image)
        self.font = ImageFont.truetype("/Library/Fonts/OsakaMono.ttf", 30)
        self.monthFont = ImageFont.truetype("/Library/Fonts/OsakaMono.ttf", 40)

    def setMergin(self, leftright, topbottom, month_height):
        self.currenttop = topbottom + month_height
        self.topbottom = topbottom
        self.monthHeight = month_height
        w,h = self.image.size
        self.delta_h = (h - 2*topbottom - self.monthHeight)/31
        self.delta_w = (w - 2*leftright) / 12
        self.currentleft = leftright - self.delta_w
        
    def drawDate(self, date, color):
        self.draw.text((self.currentleft, self.currenttop), str(date),
            fill=[(155,155,155), (155,155,255), (255,155,155)][color],
            font = self.font)

        font_w,font_h = self.font.getsize(str(date))
        self.draw.line([(self.currentleft, self.currenttop+font_h),
            (self.currentleft + self.delta_w*0.8 , self.currenttop+font_h)], fill=(155,155,155))

        self.currenttop += self.delta_h

    def changeMonth(self, date):
        self.currentleft += self.delta_w
        monthstr =  [None,"Jan","Feb","Mar","Apr","May","Jun","Jly","Aug","Sep","Oct","Nov","Dec"][date.month]
        font_w,font_h = self.monthFont.getsize(str(date.year))
        if(date.month==1 or date.month==9):
            self.draw.text((self.currentleft, self.topbottom),
                str(date.year), fill=(155,155,155), font = self.monthFont)
        self.draw.text((self.currentleft, self.topbottom+font_h),
            monthstr, fill=(155,155,155), font = self.monthFont)
            
        self.currenttop = self.topbottom + self.monthHeight
        
start_date = datetime.date(2011, 9, 1)
end_date = datetime.date(2012, 8, 31)
current_date = start_date
oneday = timedelta(1)

ci = CalenderImage(2481, 1755)
ci.setMergin(200, 100, 100)

while(current_date <= end_date):
    if(current_date.day==1): ci.changeMonth(current_date) 
    holiday = holiday_name(date = current_date)
    if(holiday != None):    ## red color
        ci.drawDate(current_date.day, 2)
    elif(current_date.weekday()==5): ## saturday
        ci.drawDate(current_date.day, 1)
    elif(current_date.weekday()==6): ## sunday
        ci.drawDate(current_date.day, 2)
    else:
        ci.drawDate(current_date.day, 0)
    
    current_date += oneday

ci.image.show()
ci.image.save("calendar.bmp", "BMP")


Comments

Popular posts from this blog

Calling OpenCV functions via Cython from Python 3.X.

Showing CPU/Memory usage on tmux status bar(tmuxのステータスバーにCPUとMemoryの使用状況を表示する)

Subclassing and Signal connect on a same widget crashes PySide application on exit.