Skip to content

Commit e322dde

Browse files
authored
Update heatmap.py
Fix 2 calls deprecated in Py-Pillow 10. (copied from keenerd#27)
1 parent f33c63a commit e322dde

1 file changed

Lines changed: 25 additions & 7 deletions

File tree

heatmap/heatmap.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ def prepare_args():
180180
sys.argv[i] = ' ' + arg
181181
parser = build_parser()
182182
args = parser.parse_args()
183+
183184
reparse(args, 'low_freq', freq_parse)
184185
reparse(args, 'high_freq', freq_parse)
185186
reparse(args, 'offset_freq', freq_parse)
@@ -194,9 +195,11 @@ def prepare_args():
194195
reparse(args, 'head_time', lambda s: datetime.timedelta(seconds=s))
195196
reparse(args, 'tail_time', lambda s: datetime.timedelta(seconds=s))
196197
args.compress = float(args.compress)
198+
197199
if args.db_limit:
198200
a,b = args.db_limit
199201
args.db_limit = (float(a), float(b))
202+
200203
if args.begin_time and args.tail_time:
201204
print("Can't combine --begin and --tail")
202205
sys.exit(2)
@@ -232,17 +235,19 @@ def summarize_pass(args):
232235
min_z = 0
233236
max_z = -100
234237
start, stop = None, None
238+
235239
for line in raw_data():
236240
line = [s.strip() for s in line.strip().split(',')]
237241
#line = [line[0], line[1]] + [float(s) for s in line[2:] if s]
238242
line = [s for s in line if s]
239243

240-
low = int(float(line[2])) + args.offset_freq
241-
high = int(float(line[3])) + args.offset_freq
244+
low = int(line[2]) + args.offset_freq
245+
high = int(line[3]) + args.offset_freq
242246
step = float(line[4])
243247
t = line[0] + ' ' + line[1]
244248
if '-' not in line[0]:
245249
t = line[0]
250+
246251
if args.low_freq is not None and high < args.low_freq:
247252
continue
248253
if args.high_freq is not None and args.high_freq < low:
@@ -264,29 +269,36 @@ def summarize_pass(args):
264269
#freqs.add(f_key[1]) # high
265270
#labels.add(f_key[0]) # low
266271
f_cache.add(f_key)
272+
267273
if not args.db_limit:
268274
zs = floatify(zs)
269275
min_z = min(min_z, min(zs))
270276
max_z = max(max_z, max(zs))
277+
271278
if start is None:
272279
start = date_parse(t)
273280
stop = date_parse(t)
274281
if args.head_time is not None and args.end_time is None:
275282
args.end_time = start + args.head_time
283+
276284
if not args.db_limit:
277285
args.db_limit = (min_z, max_z)
286+
278287
if args.tail_time is not None:
279288
times = [t for t in times if date_parse(t) >= (stop - args.tail_time)]
280289
start = date_parse(min(times))
290+
281291
freqs = list(sorted(list(freqs)))
282292
times = list(sorted(list(times)))
283293
labels = list(sorted(list(labels)))
294+
284295
if len(labels) == 1:
285296
delta = (max(freqs) - min(freqs)) / (len(freqs) / 500.0)
286297
delta = round(delta / 10**int(math.log10(delta))) * 10**int(math.log10(delta))
287298
delta = int(delta)
288299
lower = int(math.ceil(min(freqs) / delta) * delta)
289300
labels = list(range(lower, int(max(freqs)), delta))
301+
290302
height = len(times)
291303
pix_height = height
292304
if args.compress:
@@ -298,6 +310,7 @@ def summarize_pass(args):
298310
if args.compress:
299311
args.compress = -1 / args.compress
300312
pix_height = time_compression(height, args.compress)
313+
301314
print("x: %i, y: %i, z: (%f, %f)" % (len(freqs), pix_height, args.db_limit[0], args.db_limit[1]))
302315
args.freqs = freqs
303316
args.times = times
@@ -363,8 +376,8 @@ def collate_row(x_size):
363376
continue # happens with live files and time cropping
364377
if old_t is None:
365378
old_t = t
366-
low = int(float(line[2])) + args.offset_freq
367-
high = int(float(line[3])) + args.offset_freq
379+
low = int(line[2]) + args.offset_freq
380+
high = int(line[3]) + args.offset_freq
368381
step = float(line[4])
369382
columns = list(frange(low, high, step))
370383
start_col, stop_col = slice_columns(columns, args.low_freq, args.high_freq)
@@ -454,12 +467,12 @@ def closest_index(n, m_list, interpolate=False):
454467

455468
def word_aa(label, pt, fg_color, bg_color):
456469
f = ImageFont.truetype(vera_path, pt*3)
457-
s = f.getsize(label)
458-
s = (s[0], pt*3 + 3) # getsize lies, manually compute
470+
left, top, right, bottom = f.getbbox(label)
471+
s = (right - left, pt*3 + 3) # getsize lies, manually compute
459472
w_img = Image.new("RGB", s, bg_color)
460473
w_draw = ImageDraw.Draw(w_img)
461474
w_draw.text((0, 0), label, font=f, fill=fg_color)
462-
return w_img.resize((s[0]//3, s[1]//3), Image.ANTIALIAS)
475+
return w_img.resize((s[0]//3, s[1]//3), Image.Resampling.LANCZOS)
463476

464477
def blend(percent, c1, c2):
465478
"c1 and c2 are RGB tuples"
@@ -530,13 +543,15 @@ def create_labels(args, img):
530543
draw = ImageDraw.Draw(img)
531544
font = ImageFont.load_default()
532545
pixel_bandwidth = args.pixel_bandwidth
546+
533547
draw.rectangle([0,0,img.size[0],tape_height], fill='yellow')
534548
min_freq = min(args.freqs)
535549
max_freq = max(args.freqs)
536550
delta = max_freq - min_freq
537551
width = len(args.freqs)
538552
height = len(args.times)
539553
label_base = 9
554+
540555
for i in range(label_base, 0, -1):
541556
interval = int(10**i)
542557
low_f = (min_freq // interval) * interval
@@ -546,19 +561,22 @@ def create_labels(args, img):
546561
label_base = i
547562
break
548563
label_base = 10**label_base
564+
549565
for scale,y in [(1,10), (5,15), (10,19), (50,22), (100,24), (500, 25)]:
550566
hits = tape_lines(draw, args.freqs, label_base/scale, y, tape_height)
551567
pixels_per_hit = width / hits
552568
if pixels_per_hit > 50:
553569
tape_text(img, args.freqs, label_base/scale, y-tape_pt)
554570
if pixels_per_hit < 10:
555571
break
572+
556573
start, stop = args.start_stop
557574
duration = stop - start
558575
duration = duration.days * 24*60*60 + duration.seconds + 30
559576
pixel_height = duration / len(args.times)
560577
hours = int(duration / 3600)
561578
minutes = int((duration - 3600*hours) / 60)
579+
562580
if args.time_tick:
563581
label_format = "%H:%M:%S"
564582
if args.time_tick % (60*60*24) == 0:

0 commit comments

Comments
 (0)