forked from Jxck-S/plane-notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modify_image.py
71 lines (66 loc) · 2.06 KB
/
modify_image.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
def append_airport(filename, airport, text_credit=None):
from PIL import Image, ImageDraw, ImageFont
distance_mi = airport['distance_mi']
icao = airport['icao']
iata = airport['iata_code']
distance_km = distance_mi * 1.609
# create Image object with the input image
image = Image.open(filename)
# initialise the drawing context with
# the image object as background
draw = ImageDraw.Draw(image)
#Setup fonts
fontfile = "./dependencies/Roboto-Regular.ttf"
font = ImageFont.truetype(fontfile, 14)
mini_font = ImageFont.truetype(fontfile, 12)
head_font = ImageFont.truetype(fontfile, 16)
#Setup Colors
black = 'rgb(0, 0, 0)' # Black
white = 'rgb(255, 255, 255)' # White
navish = 'rgb(0, 63, 75)'
whitish = 'rgb(248, 248, 248)'
#Info Box
draw.rectangle(((325, 760), (624, 800)), fill= white, outline=black)
#Header Box
draw.rectangle(((401, 738), (549, 760)), fill= navish)
#Create Text
#ADSBX Credit
if text_credit is not None:
draw.rectangle(((658, 762), (800, 782)), fill= white)
(x, y) = (660, 760)
text = text_credit
draw.text((x, y), text, fill=black, font=head_font)
#Nearest Airport Header
(x, y) = (422, 740)
text = "Nearest Airport"
draw.text((x, y), text, fill=white, font=head_font)
#ICAO | IATA
(x, y) = (330, 765)
if airport['iata_code'] != '' and airport['icao'] != '':
airport_codes = airport['iata_code'] + " / " + airport['icao']
elif airport['icao'] != '':
airport_codes = airport['icao']
else:
airport_codes = airport['ident']
draw.text((x, y), airport_codes, fill=black, font=font)
#Distance
(x, y) = (460, 765)
text = str(round(distance_mi, 2)) + "mi / " + str(round(distance_km, 2)) + "km away"
draw.text((x, y), text, fill=black, font=font)
#Full name
(x, y) = (330, 783)
MAX_WIDTH = 325
if font.getsize(airport['name'])[0] <= MAX_WIDTH:
text = airport['name']
else:
text = ""
for char in airport['name']:
if font.getsize(text)[0] >= (MAX_WIDTH - 10):
text += "..."
break
else:
text += char
draw.text((x, y), text, fill=black, font=mini_font)
image.show()
# save the edited image
image.save(filename)