-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring: remove unnecessary code (#2)
- Loading branch information
1 parent
554127e
commit 3a03a3e
Showing
3 changed files
with
35 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.