Skip to content

Commit

Permalink
use getbbox to determine size
Browse files Browse the repository at this point in the history
  • Loading branch information
jburel committed Jan 11, 2024
1 parent cd7f4a3 commit f9a6048
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 24 deletions.
9 changes: 6 additions & 3 deletions omero/figure_scripts/Movie_Figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ def createmovie_figure(conn, pixel_ids, t_indexes, z_start, z_end, width,
col_count = min(max_col_count, len(rendered_images))
row_count = int(math.ceil(len(rendered_images) / col_count))
font = image_utils.get_font(width // 12)
font_height = font.getbbox("Textq")[3]
box = font.getbbox("Textq")
font_height = box[3] - box[1]
canvas_width = ((width + spacer) * col_count) + spacer
canvas_height = row_count * (spacer // 2 + font_height +
spacer + height)
Expand All @@ -211,7 +212,8 @@ def createmovie_figure(conn, pixel_ids, t_indexes, z_start, z_end, width,
if t_index >= size_t:
continue
time = time_labels[t]
text_w = font.getbbox(time)[2]
box = font.getbbox(time)
text_w = box[2] - box[0]
inset = (width - text_w) // 2
textdraw = ImageDraw.Draw(canvas)
textdraw.text((text_x+inset, text_y), time, font=font,
Expand Down Expand Up @@ -295,7 +297,8 @@ def add_left_labels(panel_canvas, image_labels, row_index, width, spacer):
mode = "RGB"
white = (255, 255, 255)
font = image_utils.get_font(width/12)
text_height = font.getbbox("Sampleq")[3]
box = font.getbbox("Sampleq")
text_height = box[3] - box[1]
text_gap = spacer / 2

# find max number of labels
Expand Down
9 changes: 6 additions & 3 deletions omero/figure_scripts/Movie_ROI_Figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ def get_roi_movie_view(re, query_service, pixels, time_shape_map,
row_count += 1
col_count = max_columns
font = image_utils.get_font(font_size)
text_height = font.getbbox("Textq")[3]
box = font.getbbox("Textq")
text_height = box[3] - box[1]
# no spaces around panels
canvas_width = ((panel_width + spacer) * col_count) - spacer
row_height = rendered_images[0].size[1] + spacer + text_height
Expand All @@ -221,7 +222,8 @@ def get_roi_movie_view(re, query_service, pixels, time_shape_map,
col = 0
for i, img in enumerate(rendered_images):
label = time_labels[i]
indent = (panel_width - (font.getbbox(label)[2])) // 2
box = font.getbbox(label)
indent = (panel_width - (box[2] - box[0])) // 2
draw.text((px+indent, text_y), label, font=font, fill=(0, 0, 0))
image_utils.paste_image(img, canvas, px, panel_y)
if col == (col_count - 1):
Expand Down Expand Up @@ -381,7 +383,8 @@ def get_split_view(conn, image_ids, pixel_ids, merged_indexes, merged_colours,
elif width > 200:
font_size = 16
font = image_utils.get_font(font_size)
text_height = font.getbbox("Textq")[3]
box = font.getbbox("Textq")
text_height = box[3] - box[1]
max_count = 0
for row in image_labels:
max_count = max(max_count, len(row))
Expand Down
15 changes: 10 additions & 5 deletions omero/figure_scripts/ROI_Split_Figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,8 @@ def get_roi_split_view(re, pixels, z_start, z_end, split_indexes,

# now assemble the roi split-view canvas
font = image_utils.get_font(fontsize)
text_height = font.getbbox("Textq")[3]
box = font.getbbox("Textq")
text_height = box[3] - box[1]
top_spacer = 0
if show_top_labels:
if merged_names:
Expand All @@ -247,7 +248,8 @@ def get_roi_split_view(re, pixels, z_start, z_end, split_indexes,
draw = ImageDraw.Draw(canvas)
for i, index in enumerate(split_indexes):
label = channel_names.get(index, index)
indent = (panel_width - (font.getbbox(label)[2])) // 2
box = font.getbbox(label)
indent = (panel_width - (box[2] - box[0])) // 2
# text is coloured if channel is not coloured AND in the merged image
rgb = (0, 0, 0)
if index in merged_colours:
Expand Down Expand Up @@ -275,12 +277,14 @@ def get_roi_split_view(re, pixels, z_start, z_end, split_indexes,
name = channel_names[index]
else:
name = str(index)
comb_text_width = font.getbbox(name)[2]
box = font.getbbox(name)
comb_text_width = box[2] - box[0]
inset = int((panel_width - comb_text_width) / 2)
draw.text((px + inset, text_y), name, font=font, fill=rgb)
text_y = text_y - text_height
else:
comb_text_width = font.getbbox("Merged")[2]
box = font.getbbox("Merged")
comb_text_width = box[2] - box[0]
inset = int((panel_width - comb_text_width) / 2)
draw.text((px + inset, text_y), "Merged", font=font,
fill=(0, 0, 0))
Expand Down Expand Up @@ -431,7 +435,8 @@ def get_split_view(conn, image_ids, pixel_ids, split_indexes, channel_names,
elif width > 200:
fontsize = 16
font = image_utils.get_font(fontsize)
text_height = font.getbbox("Textq")[3]
box = font.getbbox("Textq")
text_height = box[3] - box[1]
max_count = 0
for row in image_labels:
max_count = max(max_count, len(row))
Expand Down
12 changes: 8 additions & 4 deletions omero/figure_scripts/Split_View_Figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ def make_split_view_figure(conn, pixel_ids, z_start, z_end, split_indexes,
font = image_utils.get_font(fontsize)
mode = "RGB"
white = (255, 255, 255)
text_height = font.getbbox("Textq")[3]
box = font.getbbox("Textq")
text_height = box[3] - box[1]

# if adding text to the left, write the text on horizontal canvas, then
# rotate to vertical (below)
Expand Down Expand Up @@ -442,7 +443,8 @@ def make_split_view_figure(conn, pixel_ids, z_start, z_end, split_indexes,
py = top_text_height + spacer - (text_height + text_gap)
for index in split_indexes:
# calculate the position of the text, centered above the image
w = font.getbbox(channel_names[index])[2]
box = font.getbbox(channel_names[index])
w = box[2] - box[0]
inset = int((width - w) // 2)
# text is coloured if channel is grey AND in the merged image
rgba = (0, 0, 0, 255)
Expand All @@ -465,12 +467,14 @@ def make_split_view_figure(conn, pixel_ids, z_start, z_end, split_indexes,
if rgba == (255, 255, 255, 255): # if white (unreadable)
rgba = (0, 0, 0, 255) # needs to be black!
name = channel_names[index]
comb_text_width = font.getbbox(name)[2]
box = font.getbbox(name)
comb_text_width = box[2] - box[0]
inset = int((width - comb_text_width) // 2)
draw.text((px + inset, py), name, font=font, fill=rgba)
py = py - text_height
else:
comb_text_width = font.getbbox("Merged")[2]
box = font.getbbox("Merged")
comb_text_width = box[2] - box[0]
inset = int((width - comb_text_width) // 2)
px = px + inset
draw.text((px, py), "Merged", font=font, fill=(0, 0, 0))
Expand Down
27 changes: 18 additions & 9 deletions omero/figure_scripts/Thumbnail_Figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,14 @@ def paint_thumbnail_grid(thumbnail_store, length, spacing, pixel_ids,
fontsize = (length // 10) + 5
font = get_font(fontsize)
if left_label:
text_width, text_height = font.getbbox(left_label)[2:]
box = font.getbbox(left_label)
text_width = box[2] - box[0]
text_height = box[3] - box[1]
left_space = spacing + text_height + spacing
if top_label:
text_width, text_height = font.getbbox(top_label)[2:]
box = font.getbbox(top_label)
text_width = box[2] - box[0]
text_height = box[3] - box[1]
top_space = spacing + text_height + spacing
min_width = left_space + text_width + spacing

Expand All @@ -168,7 +172,8 @@ def paint_thumbnail_grid(thumbnail_store, length, spacing, pixel_ids,
label_size = (label_canvas_width, label_canvas_height)
text_canvas = Image.new(mode, label_size, bg)
draw = ImageDraw.Draw(text_canvas)
text_width = font.getbbox(left_label)[2]
box = font.getbbox(left_label)
text_width = box[2] - box[0]
text_x = (label_canvas_width - text_width) // 2
draw.text((text_x, spacing), left_label, font=font, fill=text_color)
vertical_canvas = text_canvas.rotate(90)
Expand Down Expand Up @@ -297,7 +302,8 @@ def paint_dataset_canvas(conn, images, title, tag_ids=None,
# set-up fonts
fontsize = length/7 + 5
font = get_font(fontsize)
text_height = font.getbbox("Textq")[3]
box = font.getbbox("Textq")
text_height = box[3] - box[1]
top_spacer = spacing + text_height
left_spacer = spacing + text_height

Expand Down Expand Up @@ -372,11 +378,12 @@ def paint_dataset_canvas(conn, images, title, tag_ids=None,
'showSubsetLabels': show_subset_labels})

# Find the indent we need
max_tag_name_width = max([font.getbbox(ts['tagText'])[2]
for ts in toptag_sets])
for ts in toptag_sets:
box = font.getbbox(ts['tagText'])
max_tag_name_width = max(box[2] - box[0])
if show_untagged:
max_tag_name_width = max(max_tag_name_width,
font.getbbox("Not Tagged")[2])
box = font.getbbox("Not Tagged")
max_tag_name_width = max(max_tag_name_width, (box[2] - box[0]))

tag_sub_panes = []

Expand Down Expand Up @@ -435,7 +442,9 @@ def make_tagset_canvas(tag_string, tagset_pix_ids, show_subset_labels):
p_y += pane.size[1]
if tag_text is not None:
draw = ImageDraw.Draw(tag_canvas)
tt_w, tt_h = font.getbbox(tag_text)[2:]
box = font.getbbox(tag_text)
tt_w = box[2] - box[0]
tt_h = box[3] - box[1]
h_offset = (total_height - tt_h)/2
draw.text((spacing, h_offset), tag_text, font=font,
fill=(50, 50, 50))
Expand Down

0 comments on commit f9a6048

Please sign in to comment.