@@ -153,65 +153,66 @@ def image(self, user, width, height, variant):
153153 event_counts = self ._event_counts (time , user )
154154
155155 # Create a blank image.
156- image = Image .new (mode = 'RGB' , size = (width , height ),
157- color = BACKGROUND_COLOR )
158- draw = Draw (image )
159-
160- # Get this month's calendar.
161- calendar = Calendar (firstweekday = SUNDAY )
162- weeks = calendar .monthdayscalendar (time .year , time .month )
163-
164- # Determine the spacing of the days in the image.
165- x_stride = width // (DAYS_IN_WEEK + 1 )
166- y_stride = height // (len (weeks ) + 1 )
167-
168- # Draw each week in a row.
169- for week_index in range (len (weeks )):
170- week = weeks [week_index ]
171-
172- # Draw each day in a column.
173- for day_index in range (len (week )):
174- day = week [day_index ]
175-
176- # Ignore days from other months.
177- if day == 0 :
178- continue
179-
180- # Determine the position of this day in the image.
181- x = (day_index + 1 ) * x_stride
182- y = (week_index + 1 ) * y_stride
183-
184- # Mark the current day with a squircle.
185- if day == time .day :
186- with Image .open (SQUIRCLE_FILE ).convert (mode = 'RGBA' ) as squircle :
187- squircle_xy = (x - squircle .width // 2 ,
188- y - squircle .height // 2 )
189- draw .bitmap (squircle_xy , squircle , HIGHLIGHT_COLOR )
190- number_color = TODAY_COLOR
191- event_color = TODAY_COLOR
192- else :
193- number_color = NUMBER_COLOR
194- event_color = HIGHLIGHT_COLOR
195-
196- # Draw the day of the month number.
197- number = str (day )
198- draw_text (number , SUBVARIO_CONDENSED_MEDIUM , number_color ,
199- xy = (x , y - NUMBER_Y_OFFSET ), image = image )
200-
201- # Draw a dot for each event.
202- num_events = min (MAX_EVENTS , event_counts [day ])
203- if num_events > 0 :
204- with Image .open (DOT_FILE ).convert (mode = 'RGBA' ) as dot :
205- events_width = (num_events * dot .width +
206- (num_events - 1 ) * DOT_MARGIN )
207- for event_index in range (num_events ):
208- event_offset = (event_index * (dot .width +
209- DOT_MARGIN ) - events_width // 2 )
210- dot_xy = [x + event_offset ,
211- y + DOT_OFFSET - dot .width // 2 ]
212- draw .bitmap (dot_xy , dot , event_color )
213-
214- # The calendar image is already quantized (no dithering).
215- image = image .convert ('P' , dither = None , palette = Image .ADAPTIVE )
216-
217- return image
156+ with Image .new (mode = 'RGB' ,
157+ size = (width , height ),
158+ color = BACKGROUND_COLOR ) as image :
159+ draw = Draw (image )
160+
161+ # Get this month's calendar.
162+ calendar = Calendar (firstweekday = SUNDAY )
163+ weeks = calendar .monthdayscalendar (time .year , time .month )
164+
165+ # Determine the spacing of the days in the image.
166+ x_stride = width // (DAYS_IN_WEEK + 1 )
167+ y_stride = height // (len (weeks ) + 1 )
168+
169+ # Draw each week in a row.
170+ for week_index in range (len (weeks )):
171+ week = weeks [week_index ]
172+
173+ # Draw each day in a column.
174+ for day_index in range (len (week )):
175+ day = week [day_index ]
176+
177+ # Ignore days from other months.
178+ if day == 0 :
179+ continue
180+
181+ # Determine the position of this day in the image.
182+ x = (day_index + 1 ) * x_stride
183+ y = (week_index + 1 ) * y_stride
184+
185+ # Mark the current day with a squircle.
186+ if day == time .day :
187+ with Image .open (SQUIRCLE_FILE ) as squircle :
188+ squircle = squircle .convert (mode = 'RGBA' )
189+ squircle_xy = (x - squircle .width // 2 ,
190+ y - squircle .height // 2 )
191+ draw .bitmap (squircle_xy , squircle , HIGHLIGHT_COLOR )
192+ number_color = TODAY_COLOR
193+ event_color = TODAY_COLOR
194+ else :
195+ number_color = NUMBER_COLOR
196+ event_color = HIGHLIGHT_COLOR
197+
198+ # Draw the day of the month number.
199+ number = str (day )
200+ draw_text (number , SUBVARIO_CONDENSED_MEDIUM , number_color ,
201+ xy = (x , y - NUMBER_Y_OFFSET ), image = image )
202+
203+ # Draw a dot for each event.
204+ num_events = min (MAX_EVENTS , event_counts [day ])
205+ if num_events > 0 :
206+ with Image .open (DOT_FILE ) as dot :
207+ dot = dot .convert (mode = 'RGBA' )
208+ events_width = (num_events * dot .width +
209+ (num_events - 1 ) * DOT_MARGIN )
210+ for event_index in range (num_events ):
211+ event_offset = (event_index * (dot .width +
212+ DOT_MARGIN ) - events_width // 2 )
213+ dot_xy = [x + event_offset ,
214+ y + DOT_OFFSET - dot .width // 2 ]
215+ draw .bitmap (dot_xy , dot , event_color )
216+
217+ # The calendar image is already quantized (no dithering).
218+ return image .convert ('P' , dither = None , palette = Image .ADAPTIVE )
0 commit comments