Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

minor refactor #12

Merged
merged 2 commits into from
Jan 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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