Skip to content

Commit

Permalink
refactoring: remove unnecessary code (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
dessanhemrayev authored May 7, 2024
1 parent 554127e commit 3a03a3e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 217 deletions.
201 changes: 0 additions & 201 deletions LICENSE

This file was deleted.

51 changes: 35 additions & 16 deletions extend_pillow.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,63 @@
from PIL import Image, ImageDraw, ImageFont

def text_highlighting_colors(image_src,font_src,size_font,position_text,text_origin,text_align,text_color,highlighting_color):


def text_highlighting_colors(
image_src,
font_src,
size_font,
position_text,
text_origin,
text_align,
text_color,
highlighting_color,
):
"""Highlight text in image.
Args:
image_src (str): Path to image.
font_src (str): Path to font.
size_font (int): Size of font.
position_text (tuple): Position of text.
text_origin (str): Text to highlight.
text_align (str): Text alignment.
text_color (tuple): Color of text.
highlighting_color (tuple): Color of highlighting.
"""
image = Image.open(image_src)
draw = ImageDraw.Draw(image)
font = ImageFont.truetype(font_src, size_font)
position = position_text
align = text_align
y = 0
text = text_origin
bbox = draw.textbbox(position, text, font=font,align=text_align)
bbox = draw.textbbox(position_text, text_origin, font=font,align=text_align)
xx,yy,x1 = bbox[0:3]
text2 = text.split('\n')
text2 = text_origin.split('\n')
for item in text2:
font = ImageFont.truetype("Lobster-Regular.ttf", size_font)
font_width, font_height = font.getsize(item)
font_width, _ = font.getsize(item)

if align == 'right':
if text_align == 'right':
left, top, right, bottom = draw.textbbox((x1-font_width,yy+y), item, font=font)
draw.rectangle((left-5, top-5, right+5, bottom+5), fill=highlighting_color)
draw.text((x1-font_width,yy+y), item, font=font, fill=text_color)
elif align == 'center':
elif text_align == 'center':
left, top, right, bottom = draw.textbbox((xx/2-font_width/2+x1/2,yy+y), item, font=font)
draw.rectangle((left-5, top-5, right+5, bottom+5), fill=highlighting_color)
draw.text((xx/2-font_width/2+x1/2,yy+y), item, font=font, fill=text_color)
else:
left, top, right, bottom = draw.textbbox((xx,yy+y), item, font=font)
print(left, top, right, bottom)
draw.rectangle((left-5, top-5, right+5, bottom+5), fill=highlighting_color)
draw.text((xx,yy+y), item, font=font, fill=text_color)
y += size_font
image.save("image_highlighting_colors.png")

image.show()

size = 100
image = 'tnc_48980557.jpg'
image = "tnc_48980557.jpg"
font = "Lobster-Regular.ttf"
position = (1000, 1000)
text = "Hello\nworld\nPillow Python"
align = 'center'
align = "center"
text_color = "#FF1827"
highlighting_color = "#18D8FF"
text_highlighting_colors(image,font,size,position,text,align,text_color,highlighting_color)
text_highlighting_colors(
image, font, size, position, text, align, text_color, highlighting_color
)
Binary file added image_highlighting_colors.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 3a03a3e

Please sign in to comment.