Spaces:
Sleeping
Sleeping
kwabs22
commited on
Commit
•
cf6fafe
1
Parent(s):
5f93b50
Change the Caption function with proper version
Browse files
app.py
CHANGED
@@ -537,23 +537,71 @@ def removeTonalMarks(string):
|
|
537 |
return noTonalMarksStr
|
538 |
|
539 |
|
540 |
-
def add_text_to_image(input_image, text, output_image_path="output.png", border_size=2):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
541 |
text = removeTonalMarks(text)
|
542 |
-
imagearr = np.asarray(input_image)
|
543 |
-
|
|
|
544 |
img = Image.fromarray(imagearr)
|
|
|
545 |
draw = ImageDraw.Draw(img)
|
546 |
-
font = ImageFont.truetype(
|
547 |
-
|
548 |
-
|
549 |
x = (width - text_width) / 2
|
550 |
y = (height - text_height) / 2
|
551 |
-
|
|
|
552 |
for dx, dy in [(0, 0), (border_size, border_size), (-border_size, -border_size), (border_size, -border_size), (-border_size, border_size)]:
|
553 |
draw.text((x + dx, y + dy), text, font=font, fill=(255, 255, 255))
|
554 |
draw.text((x, y), text, font=font, fill=(0, 0, 0))
|
555 |
img.save(output_image_path, "PNG")
|
556 |
-
return
|
557 |
|
558 |
def UnknownTrackTexttoApp(text): #Copy of def OptimisedTtAppForUNWFWO(text):
|
559 |
#Buttons and labels autocreation
|
|
|
537 |
return noTonalMarksStr
|
538 |
|
539 |
|
540 |
+
# def add_text_to_image(input_image, text, output_image_path="output.png", border_size=2):
|
541 |
+
# text = removeTonalMarks(text)
|
542 |
+
# imagearr = np.asarray(input_image) #Image.open(input_image_path)
|
543 |
+
# width, height = imagearr.shape[:2] #width, height = image.size
|
544 |
+
# img = Image.fromarray(imagearr)
|
545 |
+
# draw = ImageDraw.Draw(img)
|
546 |
+
# font = ImageFont.truetype("ShortBaby.ttf", 36) #ShortBaby-Mg2w.ttf
|
547 |
+
# text_width, text_height = draw.textbbox((0, 0), text, font=font)[2:] #draw.textsize(text, font)
|
548 |
+
# # calculate the x, y coordinates of the text box
|
549 |
+
# x = (width - text_width) / 2
|
550 |
+
# y = (height - text_height) / 2
|
551 |
+
# # put the text on the image with a border
|
552 |
+
# for dx, dy in [(0, 0), (border_size, border_size), (-border_size, -border_size), (border_size, -border_size), (-border_size, border_size)]:
|
553 |
+
# draw.text((x + dx, y + dy), text, font=font, fill=(255, 255, 255))
|
554 |
+
# draw.text((x, y), text, font=font, fill=(0, 0, 0))
|
555 |
+
# img.save(output_image_path, "PNG")
|
556 |
+
# return "output.png"
|
557 |
+
|
558 |
+
|
559 |
+
def calculate_max_chars(image_width, font_size, font_path="ShortBaby.ttf", margin=20):
|
560 |
+
# Create a temporary image to calculate character width
|
561 |
+
img_temp = Image.new('RGB', (100, 100))
|
562 |
+
draw_temp = ImageDraw.Draw(img_temp)
|
563 |
+
font = ImageFont.truetype(font_path, font_size)
|
564 |
+
# Use a common character for width estimation and multiply by an adjustment factor
|
565 |
+
avg_char_width = draw_temp.textlength("W", font=font) #textsize("W", font=font)[0]
|
566 |
+
max_chars = int(image_width / avg_char_width) * 2
|
567 |
+
return max_chars
|
568 |
+
|
569 |
+
def fitimagetexttowidth(text, image_width, font_size, font_path="ShortBaby.ttf"):
|
570 |
+
max_chars = calculate_max_chars(image_width, font_size, font_path)
|
571 |
+
words = text.split()
|
572 |
+
adjusted_text = ""
|
573 |
+
line = ""
|
574 |
+
|
575 |
+
for word in words:
|
576 |
+
if len(line + word) <= max_chars:
|
577 |
+
line += word + " "
|
578 |
+
else:
|
579 |
+
adjusted_text += line.strip() + "\n"
|
580 |
+
line = word + " "
|
581 |
+
adjusted_text += line.strip()
|
582 |
+
|
583 |
+
return adjusted_text
|
584 |
+
|
585 |
+
def add_text_to_image(input_image, text, text_size=36, output_image_path="output.png", border_size=2, font_path="ShortBaby.ttf"):
|
586 |
text = removeTonalMarks(text)
|
587 |
+
imagearr = np.asarray(input_image)
|
588 |
+
height, width = imagearr.shape[:2]
|
589 |
+
text = fitimagetexttowidth(text, width, text_size, font_path)
|
590 |
img = Image.fromarray(imagearr)
|
591 |
+
|
592 |
draw = ImageDraw.Draw(img)
|
593 |
+
font = ImageFont.truetype(font_path, text_size)
|
594 |
+
# Recalculate position based on the entire block of text
|
595 |
+
text_width, text_height = draw.textbbox((0, 0), text, font=font)[2:]
|
596 |
x = (width - text_width) / 2
|
597 |
y = (height - text_height) / 2
|
598 |
+
|
599 |
+
# Add text with border
|
600 |
for dx, dy in [(0, 0), (border_size, border_size), (-border_size, -border_size), (border_size, -border_size), (-border_size, border_size)]:
|
601 |
draw.text((x + dx, y + dy), text, font=font, fill=(255, 255, 255))
|
602 |
draw.text((x, y), text, font=font, fill=(0, 0, 0))
|
603 |
img.save(output_image_path, "PNG")
|
604 |
+
return output_image_path
|
605 |
|
606 |
def UnknownTrackTexttoApp(text): #Copy of def OptimisedTtAppForUNWFWO(text):
|
607 |
#Buttons and labels autocreation
|