Skip to content

Commit a892391

Browse files
authored
Merge pull request #9 from fact-project/fix_gray_scale_images
Fix gray scale images and random smaller font in status
2 parents db08071 + 5659e2e commit a892391

3 files changed

Lines changed: 167 additions & 102 deletions

File tree

.gitignore

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
env/
12+
build/
13+
develop-eggs/
14+
dist/
15+
downloads/
16+
eggs/
17+
.eggs/
18+
lib/
19+
lib64/
20+
parts/
21+
sdist/
22+
var/
23+
wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.coverage
42+
.coverage.*
43+
.cache
44+
nosetests.xml
45+
coverage.xml
46+
*,cover
47+
.hypothesis/
48+
49+
# Translations
50+
*.mo
51+
*.pot
52+
53+
# Django stuff:
54+
*.log
55+
local_settings.py
56+
57+
# Flask stuff:
58+
instance/
59+
.webassets-cache
60+
61+
# Scrapy stuff:
62+
.scrapy
63+
64+
# Sphinx documentation
65+
docs/_build/
66+
67+
# PyBuilder
68+
target/
69+
70+
# Jupyter Notebook
71+
.ipynb_checkpoints
72+
73+
# pyenv
74+
.python-version
75+
76+
# celery beat schedule file
77+
celerybeat-schedule
78+
79+
# SageMath parsed files
80+
*.sage.py
81+
82+
# dotenv
83+
.env
84+
85+
# virtualenv
86+
.venv
87+
venv/
88+
ENV/
89+
90+
# Spyder project settings
91+
.spyderproject
92+
93+
# Rope project settings
94+
.ropeproject

la_palma_overview/__init__.py

Lines changed: 70 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@
1818
import skimage
1919
import skimage.io
2020
import skimage.transform
21+
import skimage.color
2122
import io
2223
import datetime as dt
2324
import numpy as np
24-
from matplotlib import pyplot as plt
25-
from matplotlib.font_manager import FontProperties
2625
import smart_fact_crawler as sfc
2726
import requests
2827
import logging
29-
import warnings
28+
from PIL import Image, ImageDraw, ImageFont
3029

3130
from multiprocessing.pool import ThreadPool
3231

@@ -45,93 +44,83 @@ def empty_image(rows, cols):
4544

4645
def clock2img(rows, cols):
4746

48-
dpi = 96
49-
fig = plt.figure(figsize=(cols/dpi, rows/dpi), dpi=dpi)
50-
ax = plt.gca()
51-
ax.yaxis.set_visible(False)
52-
53-
ax.text(
54-
0.5, 0.7, dt.datetime.utcnow().strftime('%H:%M:%S'),
55-
horizontalalignment='center',
56-
verticalalignment='center',
57-
fontsize=100, color='red',
58-
transform=ax.transAxes,
59-
)
47+
img = Image.new('RGB', (cols, rows))
6048

61-
ax.text(
62-
0.5, 0.3, dt.datetime.utcnow().strftime('%Y.%m.%d'),
63-
horizontalalignment='center',
64-
verticalalignment='center',
65-
fontsize=80, color='red',
66-
transform=ax.transAxes,
67-
)
49+
font_time = ImageFont.truetype('DejaVuSansMono.ttf', size=120)
50+
font_date = ImageFont.truetype('DejaVuSansMono.ttf', size=100)
51+
font_run = ImageFont.truetype('DejaVuSansMono.ttf', size=120)
6852

69-
try:
70-
run_id = sfc.main_page().run_id
71-
if run_id is not None:
72-
ax.text(
73-
0.5, 0.0, 'Run {0: 3d}'.format(run_id),
74-
horizontalalignment='center',
75-
verticalalignment='center',
76-
fontsize=60, color='red',
77-
transform=ax.transAxes,
78-
)
79-
except:
80-
log.exception("Could't get run_id.")
53+
d = ImageDraw.Draw(img)
54+
55+
now = dt.datetime.utcnow()
8156

82-
ax.spines['top'].set_color('none')
83-
ax.spines['right'].set_color('none')
84-
ax.spines['left'].set_color('none')
85-
ax.xaxis.set_ticks_position('bottom')
86-
ax.patch.set_facecolor('black')
87-
88-
buf = io.BytesIO()
89-
plt.savefig(
90-
buf,
91-
format='png',
92-
dpi=dpi,
93-
transparent=False,
94-
frameon=False,
95-
facecolor='black',
96-
edgecolor='none',
57+
time = now.strftime('%H:%M:%S')
58+
date = now.strftime('%Y-%m-%d')
59+
60+
w, h = d.textsize(time, font=font_time)
61+
d.text(
62+
((cols - w) / 2, 0),
63+
time,
64+
font=font_time,
65+
fill='red'
9766
)
98-
buf.seek(0)
99-
plt.close(fig)
10067

101-
return (skimage.io.imread(buf)[:, :, 0:3]).astype('uint8')
68+
w, h = d.textsize(date, font=font_date)
69+
d.text(
70+
((cols - w) / 2, (rows - h) / 2),
71+
date,
72+
font=font_date,
73+
anchor='center',
74+
align='center',
75+
fill='red'
76+
)
77+
try:
78+
runs = sfc.observations(fallback=True).runs
79+
80+
if runs:
81+
last_run = runs[-1]
82+
if (dt.datetime.utcnow() - last_run.start) <= dt.timedelta(minutes=15):
83+
run_str = 'Run {0: 3d}'.format(last_run.id)
84+
w, h = d.textsize(run_str, font=font_run)
85+
d.text(
86+
((cols - w) / 2, rows - h - 10),
87+
run_str,
88+
font=font_run,
89+
anchor='top',
90+
fill='red'
91+
)
10292

93+
except Exception as e:
94+
log.exception("Could't get run_id.")
10395

104-
def smart_fact2img(rows, cols):
96+
return np.array(img, dtype='uint8')
10597

106-
dpi = 96
107-
fig = plt.figure(figsize=(cols/dpi, rows/dpi), dpi=dpi)
108-
ax = plt.gca()
109-
ax.yaxis.set_visible(False)
11098

99+
def smart_fact2img(rows, cols):
111100
currents = sfc.sipm_currents()
112101
drive_pointing = sfc.drive_pointing()
113102
weather = sfc.weather()
114103
rel_temp = sfc.camera_climate().relative_temperature_mean.value
115104
cam_temp = rel_temp + weather.temperature.value
116105

117-
out = (
106+
status_text = (
118107
'SQM\n'
119-
' Magnitude.... {Magnitude:.1f}\n'
108+
' Magnitude..{Magnitude: > 6.1f}\n'
120109
'SIPM\n'
121-
' power........ {power:.1f} {power_unit:s} \n'
122-
' min med max.. {min_cur:.1f}, {med_cur:.1f}, {max_cur:.1f} {cur_unit:s}\n'
110+
' min........{min_cur: > 6.1f} {cur_unit}\n'
111+
' med........{med_cur: > 6.1f} {cur_unit}\n'
112+
' max........{max_cur: > 6.1f} {cur_unit}\n'
123113
'Temp\n'
124-
' outside...... {out_temp:.1f} C\n'
125-
' container.... {cont_temp:.1f} C\n'
126-
' camera....... {cam_temp:.1f} C\n'
127-
'Source\n'
128-
' name......... {source_name}\n'
129-
' Azimuth...... {source_az:.1f} {source_az_unit:s}\n'
130-
' Zenith....... {source_zd:.1f} {source_zd_unit:s}\n'
114+
' outside....{out_temp: > 6.1f} C\n'
115+
' container..{cont_temp: > 6.1f} C\n'
116+
' camera.....{cam_temp: > 6.1f} C\n'
117+
'Pointing: {source_name}\n'
118+
' Azimuth....{source_az: > 6.1f} {source_az_unit}\n'
119+
' Zenith.....{source_zd: > 6.1f} {source_zd_unit}\n'
131120
).format(
132121
Magnitude=sfc.sqm().magnitude.value,
133-
power=currents.power.value,
134-
power_unit=currents.power.unit,
122+
power=currents.power_camera.value,
123+
power_unit=currents.power_camera.unit,
135124
min_cur=currents.min_per_sipm.value,
136125
med_cur=currents.median_per_sipm.value,
137126
max_cur=currents.max_per_sipm.value,
@@ -146,37 +135,14 @@ def smart_fact2img(rows, cols):
146135
source_zd_unit=drive_pointing.zenith_distance.unit,
147136
)
148137

149-
font = FontProperties()
150-
font.set_family('monospace')
151-
ax.text(
152-
0, 0.5, out,
153-
verticalalignment='center',
154-
fontsize=20, color='red',
155-
horizontalalignment='left',
156-
fontproperties=font,
157-
transform=ax.transAxes,
158-
)
138+
img = Image.new('RGB', (cols, rows))
139+
font = ImageFont.truetype('DejaVuSansMono.ttf', size=32)
159140

160-
ax.spines['top'].set_color('none')
161-
ax.spines['right'].set_color('none')
162-
ax.spines['left'].set_color('none')
163-
ax.xaxis.set_ticks_position('bottom')
164-
ax.patch.set_facecolor('black')
165-
166-
buf = io.BytesIO()
167-
plt.savefig(
168-
buf,
169-
format='png',
170-
dpi=dpi,
171-
transparent=False,
172-
frameon=False,
173-
facecolor='black',
174-
edgecolor='none',
175-
)
176-
buf.seek(0)
177-
plt.close(fig)
141+
d = ImageDraw.Draw(img)
142+
143+
d.text((10, 10), status_text, font=font, anchor='top', fill='red')
178144

179-
return (skimage.io.imread(buf)[:, :, 0:3]).astype('uint8')
145+
return np.array(img, dtype='uint8')
180146

181147

182148
def stack_image_list_into_rows_and_cols(imgs, big_rows, big_cols):
@@ -214,7 +180,11 @@ def download_and_resize_image(url, rows, cols, fmt='jpg', fallback=True):
214180
req = requests.get(url, verify=False, timeout=15)
215181

216182
img = skimage.io.imread(io.BytesIO(req.content), format=fmt)
217-
img = skimage.transform.resize(img, (rows, cols, 3))
183+
184+
if img.ndim == 2:
185+
img = skimage.color.gray2rgb(img)
186+
187+
img = skimage.transform.resize(img, (rows, cols))
218188
img = (img * 255).astype('uint8')
219189

220190
log.debug('Downloaded image from url {}'.format(url))

setup.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
setup(
88
name='la_palma_overview',
9-
version='0.1.1',
9+
version='0.1.2',
1010
description=description,
1111
url='https://github.com/fact-project/la_palma_overview.git',
1212
author='Sebastian Mueller,Maximilian Noethe',
@@ -19,8 +19,9 @@
1919
'docopt',
2020
'scikit-image',
2121
'requests',
22-
'smart_fact_crawler==0.3.1',
22+
'smart_fact_crawler==0.4.1',
2323
'send2trash',
24+
'pillow',
2425
],
2526
entry_points={'console_scripts': [
2627
'la_palma_overview = la_palma_overview.__init__:main',

0 commit comments

Comments
 (0)