Skip to content

Commit

Permalink
minor refactor (#12)
Browse files Browse the repository at this point in the history
* minor-refactor

* minor refactor
  • Loading branch information
fcakyon authored Jan 13, 2023
1 parent ca76274 commit 909c6c9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ultralyticsplus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .hf_utils import push_to_hfhub, download_from_hub
from .ultralytics_utils import YOLO, render_predictions

__version__ = "0.0.6"
__version__ = "0.0.7"
2 changes: 1 addition & 1 deletion ultralyticsplus/hf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import pandas as pd
from PIL import Image

from ultralyticsplus.file_utils import add_text_to_image
from ultralyticsplus.other_utils import add_text_to_image

LOGLEVEL = os.environ.get("LOGLEVEL", "INFO").upper()
logging.basicConfig(level=LOGLEVEL)
Expand Down
24 changes: 13 additions & 11 deletions ultralyticsplus/file_utils.py → ultralyticsplus/other_utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from PIL import ImageDraw, ImageFont, ImageEnhance


def add_text_to_image(
text,
pil_image,
brightness: float = 0.75,
text_font: int = 50,
crop_margin: int = None,
):
from PIL import ImageDraw, ImageFont, ImageEnhance

RESIZE_WIDTH_TO = 1280
MAX_TEXT_WIDTH = 860

# Create an ImageEnhance object
enhancer = ImageEnhance.Brightness(pil_image)
Expand All @@ -16,10 +17,10 @@ def add_text_to_image(
pil_image = enhancer.enhance(brightness)

# Resize the image
pil_image = pil_image.resize((1280, 1280))
pil_image = pil_image.resize((RESIZE_WIDTH_TO, RESIZE_WIDTH_TO))

# crop image
if crop_margin is not None:
# crop image
width, height = pil_image.size
mid_height = int(height / 2)
min_height = max(0, mid_height - crop_margin)
Expand All @@ -38,13 +39,14 @@ def add_text_to_image(
# Get the width and height of the text
text_width = bbox[2] - bbox[0]
text_height = bbox[3] - bbox[1]

# update font size
text_font = int(860 / text_width * text_font)
font = ImageFont.truetype("OpenSans-Bold.ttf", text_font)
bbox = draw.textbbox((0, 0), text, font=font)
text_width = bbox[2] - bbox[0]
text_height = bbox[3] - bbox[1]
if text_width > MAX_TEXT_WIDTH:
text_font = int(MAX_TEXT_WIDTH / text_width * text_font)
font = ImageFont.truetype("OpenSans-Bold.ttf", text_font)
bbox = draw.textbbox((0, 0), text, font=font)
text_width = bbox[2] - bbox[0]
text_height = bbox[3] - bbox[1]

# Define the coordinates of the smaller rounded rectangle
box_margin = 20
Expand Down

0 comments on commit 909c6c9

Please sign in to comment.