1

I'm hoping someone can help to teach me how I might do the following:

Take one base image, and on top of it overlay a date (Saturday, February 19th, 2022, for ex.) and then write that to a new image file named for the date. Then repeat, but this time incrementing to the next chronological date, so on and so forth. My aim is to create a year's worth of images, one for each day of the calendar year.

The controls I need for the date are to choose a font, a size, and an offset from the upper right-hand corner of the image.

I really appreciate any time you might spend on this. I'm new to Python but want to learn how to deconstruct this kind of problem and code a solution myself. This is my first project.

stunafish
  • 51
  • 7

1 Answers1

1

After Mark Setchell pointed toward a way to do the iteration using datetime and timedelta, I was able to put something rudimentary together using a walkthrough of the pillow library.

Edit: I've tried to take Mark's further advice to load the font and template image into memory first outside the loop, and then to copy the template from memory back into the loop for iteration.

The code for my use-case is as follows:

from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
from datetime import date, timedelta
import copy

def daterange(start_date, end_date):
    for n in range(int((end_date - start_date).days)):
        yield start_date + timedelta(n)


start_date = date(2022, 2, 22)
end_date = date(2022, 3, 1)

img = Image.open("RoomSchedule_template.png")
font = ImageFont.truetype("SF-Pro-Text-Black.ttf", 192)


for single_date in daterange(start_date, end_date):

    imginmem = copy.copy(img)
    draw = ImageDraw.Draw(imginmem)
    draw.text((5000, 360), single_date.strftime("%A, %B %d, %Y"), (0, 0, 0), anchor="rs", font=font)

    imginmem.save(single_date.strftime("RoomSchedule %a, %B %d, %Y.png"))

I'll probably add user input so that it can be run for different date ranges in the future without editing the file again.

stunafish
  • 51
  • 7
  • 1
    Well done. Thank you for sharing back your code. You should be able to load the font just once, before entering the loop as a little improvement. Likewise, you should be able to load the template image from the PNG file into memory before the loop and just **copy** it from memory each time through the loop - it will be faster. – Mark Setchell Feb 21 '22 at 08:46
  • @MarkSetchell I tried to make use of your suggestion and read about shallow versus deep copies. Have I used this correctly? – stunafish Feb 21 '22 at 13:38
  • I was actually suggesting you use the `copy()` method directly from PIL https://pillow.readthedocs.io/en/stable/reference/Image.html#the-image-class So, it would be `imginmem = img.copy()` and no need to `import copy` – Mark Setchell Feb 21 '22 at 13:41
  • @MarkSetchell I wanted to say thank you and share what my project ended up looking like. The assets are all available on Github: https://github.com/TunafishTiger/RoomsCalendar – stunafish Nov 06 '22 at 23:32
  • That looks excellent Well done! – Mark Setchell Nov 07 '22 at 06:36