meteortools.utils.annotateImage

 1# Copyright (C) 2018-2023 Mark McIntyre
 2
 3from PIL import Image, ImageFont, ImageDraw 
 4import datetime
 5
 6
 7def annotateImage(img_path, statid, metcount, rundate=None):
 8    """
 9    Annotate an image with the station ID and date in the bottom left and meteor count in the  
10    bottom right  
11
12    Arguments:  
13        img_path:   [str] full path and filename of the image to be annotated  
14        statid:     [str] station ID string to use  
15        metcount:   [int] number of meteors in the image  
16
17    
18    Keyword Args:  
19        rundate:    [str] rundate in 'YYYYMM' or 'YYYYMMDD' format. Default is today.   
20
21    """
22    if rundate is not None:
23        if len(rundate) > 6:
24            now = datetime.datetime.strptime(rundate, '%Y%m%d')
25            title = '{} {}'.format(statid, now.strftime('%Y-%m-%d'))
26        else:
27            now = datetime.datetime.strptime(rundate, '%Y%m')
28            title = '{} {}'.format(statid, now.strftime('%Y-%m'))
29    else:
30        now = datetime.datetime.now()
31        title = '{} {}'.format(statid, now.strftime('%Y-%m-%d'))
32
33    my_image = Image.open(img_path)
34    width, height = my_image.size
35    image_editable = ImageDraw.Draw(my_image)
36    fntheight=30
37    try:
38        fnt = ImageFont.truetype("arial.ttf", fntheight)
39    except:
40        fnt = ImageFont.truetype("DejaVuSans.ttf", fntheight)
41    #fnt = ImageFont.load_default()
42    image_editable.text((15,height-fntheight-15), title, font=fnt, fill=(255))
43    metmsg = 'meteors: {:04d}'.format(metcount)
44    image_editable.text((width-7*fntheight-15,height-fntheight-15), metmsg, font=fnt, fill=(255))
45    my_image.save(img_path)
46
47
48def annotateImageArbitrary(img_path, message, color='#000'):
49    """
50    Annotate an image with an arbitrary message in the selected colour at the bottom left  
51
52    Arguments:  
53        img_path:   [str] full path and filename of the image to be annotated  
54        message:    [str] message to put on the image  
55        color:      [str] hex colour string, default '#000' which is black  
56
57    """
58    my_image = Image.open(img_path)
59    width, height = my_image.size
60    image_editable = ImageDraw.Draw(my_image)
61    fntheight=30
62    try:
63        fnt = ImageFont.truetype("arial.ttf", fntheight)
64    except:
65        fnt = ImageFont.truetype("DejaVuSans.ttf", fntheight)
66    #fnt = ImageFont.load_default()
67    image_editable.text((15,height-fntheight-15), message, font=fnt, fill=color)
68    my_image.save(img_path)
def annotateImage(img_path, statid, metcount, rundate=None):
 8def annotateImage(img_path, statid, metcount, rundate=None):
 9    """
10    Annotate an image with the station ID and date in the bottom left and meteor count in the  
11    bottom right  
12
13    Arguments:  
14        img_path:   [str] full path and filename of the image to be annotated  
15        statid:     [str] station ID string to use  
16        metcount:   [int] number of meteors in the image  
17
18    
19    Keyword Args:  
20        rundate:    [str] rundate in 'YYYYMM' or 'YYYYMMDD' format. Default is today.   
21
22    """
23    if rundate is not None:
24        if len(rundate) > 6:
25            now = datetime.datetime.strptime(rundate, '%Y%m%d')
26            title = '{} {}'.format(statid, now.strftime('%Y-%m-%d'))
27        else:
28            now = datetime.datetime.strptime(rundate, '%Y%m')
29            title = '{} {}'.format(statid, now.strftime('%Y-%m'))
30    else:
31        now = datetime.datetime.now()
32        title = '{} {}'.format(statid, now.strftime('%Y-%m-%d'))
33
34    my_image = Image.open(img_path)
35    width, height = my_image.size
36    image_editable = ImageDraw.Draw(my_image)
37    fntheight=30
38    try:
39        fnt = ImageFont.truetype("arial.ttf", fntheight)
40    except:
41        fnt = ImageFont.truetype("DejaVuSans.ttf", fntheight)
42    #fnt = ImageFont.load_default()
43    image_editable.text((15,height-fntheight-15), title, font=fnt, fill=(255))
44    metmsg = 'meteors: {:04d}'.format(metcount)
45    image_editable.text((width-7*fntheight-15,height-fntheight-15), metmsg, font=fnt, fill=(255))
46    my_image.save(img_path)

Annotate an image with the station ID and date in the bottom left and meteor count in the
bottom right

Arguments:
img_path: [str] full path and filename of the image to be annotated
statid: [str] station ID string to use
metcount: [int] number of meteors in the image

Keyword Args:
rundate: [str] rundate in 'YYYYMM' or 'YYYYMMDD' format. Default is today.

def annotateImageArbitrary(img_path, message, color='#000'):
49def annotateImageArbitrary(img_path, message, color='#000'):
50    """
51    Annotate an image with an arbitrary message in the selected colour at the bottom left  
52
53    Arguments:  
54        img_path:   [str] full path and filename of the image to be annotated  
55        message:    [str] message to put on the image  
56        color:      [str] hex colour string, default '#000' which is black  
57
58    """
59    my_image = Image.open(img_path)
60    width, height = my_image.size
61    image_editable = ImageDraw.Draw(my_image)
62    fntheight=30
63    try:
64        fnt = ImageFont.truetype("arial.ttf", fntheight)
65    except:
66        fnt = ImageFont.truetype("DejaVuSans.ttf", fntheight)
67    #fnt = ImageFont.load_default()
68    image_editable.text((15,height-fntheight-15), message, font=fnt, fill=color)
69    my_image.save(img_path)

Annotate an image with an arbitrary message in the selected colour at the bottom left

Arguments:
img_path: [str] full path and filename of the image to be annotated
message: [str] message to put on the image
color: [str] hex colour string, default '#000' which is black