-
Notifications
You must be signed in to change notification settings - Fork 0
/
markGraphic.py
107 lines (77 loc) · 4.54 KB
/
markGraphic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import logging
import argparse
import time
import datetime
import pytz
import json
from PIL import Image, ImageDraw
import datetime, pytz
# Returns the current UTC offset as integer value.
def getCurrentUtcOffset():
utcOffset = datetime.datetime.now(pytz.timezone('Europe/Zurich')).strftime('%z')
utcOffset = int(int(utcOffset)/100)
print("Current UTC Offset: %d" % utcOffset)
return utcOffset
def markGraphic(inputFile, outputFile, metaFile, x, y, w, h, fakeTime, test, utcOffset=None):
logging.debug("X: %d, Y: %d, width: %d, height: %d" % (x, y, w, h))
pixelPerMinute = float(w) / (24 * 60)
logging.debug("Pixel per Minute: %f, per hour: %f" % (pixelPerMinute, pixelPerMinute * 60))
im = Image.open(inputFile)
imageWidth, imageHeight = im.size
logging.debug("Dimension of the graph: %d, %d" % (imageWidth, imageHeight))
d = ImageDraw.Draw(im)
y = imageHeight - y # swap top/bottom coordination system
h *= -1
if test:
logging.info("Going to draw a red frame around the first day")
d.line([(x, y), (x, y + h)], fill='red', width=1) # left side
d.line([(x + w, y), (x + w, y + h)], fill='red', width=1) # right side
d.line([(x, y), (x + w, y)], fill='red', width=1) # top
d.line([(x, y + h), (x + w, y + h)], fill='red', width=1) # bottom
if utcOffset == None:
# Get offset from local time to UTC, see also https://stackoverflow.com/questions/3168096/getting-computers-utc-offset-in-python
ts = time.time()
utcOffset = (datetime.datetime.fromtimestamp(ts) -
datetime.datetime.utcfromtimestamp(ts)).total_seconds()
utcOffset = int(utcOffset / 3600) # in hours
else:
utcOffset = utcOffset
logging.debug("UTC offset: %d" % utcOffset)
if metaFile:
with open(metaFile) as f:
metaData = json.load(f)
logging.debug("%d seconds since last model generation" % (datetime.datetime.utcnow().timestamp() + utcOffset * 3600 - metaData['modelTimestamp']))
# TODO check if mode got generated yesterday. if so, the current time must be shown on the 2nd day shown instead of the first!
if fakeTime:
hour = int(fakeTime[0:2])
minute = int(fakeTime[3:5])
else: # use current time
hour = datetime.datetime.now().hour
minute = datetime.datetime.now().minute
logging.debug("Time: %02d:%02d" % (hour, minute))
pixelPerTime = pixelPerMinute * ((hour + utcOffset) * 60 + minute)
d.line([(x + pixelPerTime, y - 1), (x + pixelPerTime, y + h)], fill='red', width=2)
im.save(outputFile)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Script to take a forecast graph image file (png) and add a mark to the current time')
parser.add_argument('-v', action='store_true', help='Verbose output')
parser.add_argument('-i', action='store', type=argparse.FileType('r'), required=True, help='Input file name of the graph image (png)')
parser.add_argument('-m', action='store', type=argparse.FileType('r'), default=None, help='Meta file to be used (json)')
parser.add_argument('-o', action='store', type=argparse.FileType('w'), required=True, help='Output file name of the graph image (png)')
parser.add_argument('-x', action='store', type=int, required=True, help='start-x pixel of first day')
parser.add_argument('-y', action='store', type=int, required=True, help='start-y pixel of first day')
parser.add_argument('-H', action='store', type=int, required=True, help='Height of the day in pixel')
parser.add_argument('-w', action='store', type=int, required=True, help='Width of the day in pixel')
parser.add_argument('--utc-offset', action='store', type=int, help='Offset to UTC, only needed if system does not know it', default=None)
parser.add_argument('--fake-time', action='store', default=False, help='Time to fake, in the format hh:mm')
parser.add_argument('--test', action='store_true', default=False, help='Draws a border around the day for testing the coordinate and size')
args = parser.parse_args()
logLevel = logging.INFO
if args.v:
logLevel = logging.DEBUG
if args.utc_offset:
utcOffset = args.utc_offset
else:
utcOffset = getCurrentUtcOffset()
logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s', datefmt='%d-%b-%y %H:%M:%S', level=logLevel)
markGraphic(inputFile=args.i.name, outputFile=args.o.name, metaFile=args.m, x=args.x, y=args.y, w=args.w, h=args.h, utcOffset=utcOffset, fakeTime=args.fake_time, test=args.test)