Home > Software engineering >  How do I create a calendar using Pillow in python?
How do I create a calendar using Pillow in python?

Time:12-20

I am working on a project where I would like to create a calendar that will be displayed on a e-paper display. I have managed to make the grid but don't know how to fill the grid with calendar dates. The grid is located on the bottom half of the display because I am using the top half to display weather information.

this code is used to create the image that will then later be transferred to the display.

If possible I would like to display the dates of the current month I the grid starting on the left with Sunday.

please advise

here is my code:

from PIL import Image, ImageDraw, ImageFont
import datetime
from datetime import date
from calendar import monthrange 

import calendar



w, h = 480, 800

img = Image.new("RGB",(w,h), (255,255,255))
draw = ImageDraw.Draw(img)

boarder = 9
h_start= int(h/2)
h_end = int(h-boarder)
w_start = boarder
w_end = w-boarder
stepsizeV = int((w-2*boarder)/7)
stepsizeH = int((h_start-boarder)/5)

#draw.rectangle((10,h_start,w-10,h_end),outline=1,width=5,)
for x in range (boarder,w,stepsizeV):
    line = ((x,h_start),(x,h_end))
    draw.line(line,fill=1,width=3)
for x in range (h_start,h,stepsizeH):
    line = ((w_start,x),(w_end,x))
    draw.line(line,fill=50, width=3)

Curdate = date.today() 
date =int(Curdate.strftime('%d'))
month = int(Curdate.strftime('%m'))
year = int(Curdate.strftime('%y'))

monthlen = calendar.monthrange(year,month)

for i in range (monthlen):
    for j in range(7):
    

CodePudding user response:

from PIL import Image, ImageDraw, ImageFont
import datetime
from datetime import date
from calendar import monthrange 

import calendar

w, h = 480, 800

img = Image.new("RGB",(w,h), (255,255,255))
draw = ImageDraw.Draw(img)

boarder = 9
h_start= int(h/2)
h_end = int(h-boarder)
w_start = boarder
w_end = w-boarder
stepsizeV = int((w-2*boarder)/7)
stepsizeH = int((h_start-boarder)/5)

#draw.rectangle((10,h_start,w-10,h_end),outline=1,width=5,)
cols=[]
rows=[]
days = { 0:'Sun', 1:'Mon', 2:'Tue', 3:'Wed', 4:'Thu', 5:'Fri', 6:'Sat'}
i=0
for x in range (boarder,w,stepsizeV):
    line = ((x,h_start),(x,h_end))
    draw.line(line,fill=1,width=3)
    cols.append(x stepsizeV/2)
    if i<7:
        draw.text((x stepsizeV/2 -10, h_start-stepsizeV/2), days[i], fill=(0,0,0))
        i =1

for x in range (h_start,h,stepsizeH):
    line = ((w_start,x),(w_end,x))
    draw.line(line,fill=50, width=3)
    rows.append(x stepsizeH/2)

Curdate = date.today() 
date =int(Curdate.strftime('%d'))
month = int(Curdate.strftime('%m'))
year = int(Curdate.strftime('%y'))

monthlen = calendar.monthrange(year,month)

k = monthlen[0]   1
i=1
j=0
r = rows[j]
while i<= monthlen[1]:
    c = cols[k]
    draw.text((c,r), str(i), fill=(0,0,0))
    i =1
    k = (k 1)%7
    if not k:
        j =1
        r = rows[j]
img.show()
  • Related