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.

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

This code is created by tiid (
This work is licensed under the Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit 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 ="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 =, 9, 1)
end_date =, 8, 31)
current_date = start_date
oneday = timedelta(1)

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

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


Popular posts from this blog

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

Calling OpenCV functions via Cython from Python 3.X.

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