diff --git a/README.md b/README.md index 04e78e6..c7cfe00 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ from bounding_box import bounding_box as bb Then, just add the bounding box on an image. ```python -bb.add(image, left, top, right, bottom, label, color) +bb.add(image, left, top, right, bottom, label, color, size) ``` This method takes 5 mandatory parameters: @@ -70,6 +70,7 @@ If `label` is specified and `color` is not, then a color depending on the `label` is randomly chosen. If neither `color` and `label` is specified then the bounding box color is defaulted to `green`. +- `size`: A integer representing the label's font size. Defaults to 15px. ## Examples The script to plot exemples of this **README** is available diff --git a/bounding_box/bounding_box.py b/bounding_box/bounding_box.py index d5f8850..aeefa05 100644 --- a/bounding_box/bounding_box.py +++ b/bounding_box/bounding_box.py @@ -37,7 +37,6 @@ _FONT_PATH = _os.path.join(_LOC, "Ubuntu-B.ttf") _FONT_HEIGHT = 15 -_FONT = ImageFont.truetype(_FONT_PATH, _FONT_HEIGHT) def _rgb_to_bgr(color): return list(reversed(color)) @@ -45,7 +44,9 @@ def _rgb_to_bgr(color): def _color_image(image, font_color, background_color): return background_color + (font_color - background_color) * image / 255 -def _get_label_image(text, font_color_tuple_bgr, background_color_tuple_bgr): +def _get_label_image(text, font_color_tuple_bgr, background_color_tuple_bgr, size=_FONT_HEIGHT): + _FONT = ImageFont.truetype(_FONT_PATH, size) + text_image = _FONT.getmask(text) shape = list(reversed(text_image.size)) bw_image = np.array(text_image).reshape(shape) @@ -58,7 +59,7 @@ def _get_label_image(text, font_color_tuple_bgr, background_color_tuple_bgr): return np.concatenate(image).transpose(1, 2, 0) -def add(image, left, top, right, bottom, label=None, color=None): +def add(image, left, top, right, bottom, label=None, color=None, size=_FONT_HEIGHT): if type(image) is not _np.ndarray: raise TypeError("'image' parameter must be a numpy.ndarray") try: @@ -92,7 +93,7 @@ def add(image, left, top, right, bottom, label=None, color=None): if label: _, image_width, _ = image.shape - label_image = _get_label_image(label, color_text, color) + label_image = _get_label_image(label, color_text, color, size) label_height, label_width, _ = label_image.shape rectangle_height, rectangle_width = 1 + label_height, 1 + label_width diff --git a/docs/examples.py b/docs/examples.py index 56b8417..a1dbcd9 100755 --- a/docs/examples.py +++ b/docs/examples.py @@ -93,5 +93,12 @@ def main(): bb.add(image, 864, 51, 959, 266, "Male", "blue") show_and_save("POBB", image, out_path) + in_path = os.path.join("docs", "images", "globe.jpg") + out_path = os.path.join("docs", "images", "globe_bb.png") + image = cv2.imread(in_path, cv2.IMREAD_COLOR) + bb.add(image, 721, 312, 721 + 92, 312 + 32, "Big Text", "olive", 30) + bb.add(image, 824, 893, 824 + 125, 893 + 89, "Small Text", "red", 15) + show_and_save("Globe and a Magnifying Glass", image, out_path) + if __name__ == "__main__": main() diff --git a/docs/images/globe.jpg b/docs/images/globe.jpg new file mode 100644 index 0000000..5954258 Binary files /dev/null and b/docs/images/globe.jpg differ diff --git a/docs/images/globe_bb.png b/docs/images/globe_bb.png new file mode 100644 index 0000000..f97c44d Binary files /dev/null and b/docs/images/globe_bb.png differ