(images, names, colors, output_path)
| 191 | |
| 192 | |
| 193 | def concat_visualizations(images, names, colors, output_path): |
| 194 | def draw_text(draw, font, text, width, bar_height, offset, color): |
| 195 | left, top, right, bottom = font.getbbox(text) |
| 196 | text_width, text_height = right - left, bottom - top |
| 197 | draw.rectangle([(offset, 0), (offset + width, bar_height)], fill=color) |
| 198 | draw.text((offset + (width - text_width) / 2, text_height - text_height / 2), text, fill="black", font=font) |
| 199 | |
| 200 | bar_height = 18 |
| 201 | width = 0 |
| 202 | height = 0 |
| 203 | for im in images: |
| 204 | width += im.width |
| 205 | height = max(height, im.height) |
| 206 | |
| 207 | concat = Image.new("RGB", (width, height + bar_height)) |
| 208 | draw = ImageDraw.Draw(concat) |
| 209 | font = ImageFont.load_default() |
| 210 | |
| 211 | offset = 0 |
| 212 | for i, im in enumerate(images): |
| 213 | concat.paste(im, (offset, bar_height)) |
| 214 | draw_text(draw, font, names[i], im.width, bar_height, offset, colors[i]) |
| 215 | offset += im.width |
| 216 | |
| 217 | if output_path is None: |
| 218 | return concat |
| 219 | concat.save(output_path) |
no test coverage detected