|
| 1 | +################################# |
| 2 | +# |
| 3 | +# Imports from useful Python libraries |
| 4 | +# |
| 5 | +################################# |
| 6 | + |
| 7 | +import matplotlib.pyplot as plt |
| 8 | +from matplotlib import patheffects |
| 9 | +import numpy as np |
| 10 | + |
| 11 | +################################# |
| 12 | +# |
| 13 | +# Imports from CellProfiler |
| 14 | +# |
| 15 | +################################## |
| 16 | + |
| 17 | +import cellprofiler_core.module |
| 18 | +import cellprofiler_core.setting.text |
| 19 | + |
| 20 | +__doc__ = """\ |
| 21 | +HelloWorld |
| 22 | +============ |
| 23 | +
|
| 24 | +**HelloWorld** takes an image, and overlays "Hello World!" on top of it, by default. |
| 25 | +
|
| 26 | +
|
| 27 | +I am a module |
| 28 | +look at me, |
| 29 | +about as simple |
| 30 | +as could be. |
| 31 | +
|
| 32 | +| |
| 33 | +
|
| 34 | +============ ============ =============== |
| 35 | +Supports 2D? Supports 3D? Respects masks? |
| 36 | +============ ============ =============== |
| 37 | +YES NO YES |
| 38 | +============ ============ =============== |
| 39 | +
|
| 40 | +""" |
| 41 | + |
| 42 | +class HelloWorld(cellprofiler_core.module.ImageProcessing): |
| 43 | + module_name = "HelloWorld" |
| 44 | + category = "Info" |
| 45 | + |
| 46 | + variable_revision_number = 1 |
| 47 | + |
| 48 | + def create_settings(self): |
| 49 | + super().create_settings() |
| 50 | + self.y_name.set_value("OverlayImage") |
| 51 | + self.overlay_text = cellprofiler_core.setting.text.Text("Overlay Text", "Hello World!", doc="The text you would like to be overlayed on top of the image.") |
| 52 | + |
| 53 | + def settings(self): |
| 54 | + return super().settings() + [self.overlay_text] |
| 55 | + |
| 56 | + def visible_settings(self): |
| 57 | + return super().visible_settings() + [self.overlay_text] |
| 58 | + |
| 59 | + def run(self, workspace): |
| 60 | + self.function = self.place_text_on_image |
| 61 | + super().run(workspace) |
| 62 | + |
| 63 | + def place_text_on_image(self, |
| 64 | + img, |
| 65 | + text, |
| 66 | + x_pos = 0, |
| 67 | + y_pos = 0.99, |
| 68 | + color = "white", |
| 69 | + weight = "bold", |
| 70 | + ha = "left", |
| 71 | + va = "top", |
| 72 | + outline_color = "black", |
| 73 | + outline_width = 3): |
| 74 | + fig = plt.figure() |
| 75 | + fig.figimage(img, resize=True) |
| 76 | + |
| 77 | + fontsize = 34/400*img.shape[0] |
| 78 | + |
| 79 | + # Main text |
| 80 | + txt = fig.text(x_pos, y_pos, text, fontsize=fontsize, color=color, weight=weight, |
| 81 | + horizontalalignment=ha, verticalalignment=va) |
| 82 | + |
| 83 | + # Apply white outline using path_effects |
| 84 | + outline_effect = patheffects.withStroke(linewidth=outline_width, foreground=outline_color) |
| 85 | + txt.set_path_effects([outline_effect]) |
| 86 | + |
| 87 | + fig.canvas.draw() |
| 88 | + annotated_img = np.asarray(fig.canvas.renderer.buffer_rgba()) |
| 89 | + plt.close(fig) |
| 90 | + return annotated_img |
| 91 | + |
0 commit comments