File size: 2,188 Bytes
f4a41d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from PIL import Image, ImageDraw, ImageFont, ImageColor
from lib.custom_text_overlay.align import align, Position, getTextAlignFromPosition
from lib.custom_text_overlay.logger import logger
from lib.custom_text_overlay.util import percentToByte

def drawText(img: Image.Image, text: str, fontSize: int, textColor: str, position: Position, backgroundColor: str, backgroundOpacity: int, margin: int = 0, padding: int = 8, outline: int = 0, outlineColor: str = '#000000', outlineOpacity: int = 100) -> Image.Image:
  draw = ImageDraw.Draw(img)
  font = ImageFont.truetype("D:/ai/automatic1111/modules/Roboto-Regular.ttf", fontSize)
  multilineAlign = getTextAlignFromPosition(position)
  drawTextArgs = {
    'text': text,
    'font': font,
    'spacing': 0,
    'align': multilineAlign,
    'stroke_width': outline,
  }

  textBounds = draw.multiline_textbbox((0, 0), **drawTextArgs)
  textWidth = textBounds[2] - textBounds[0]
  textHeight = textBounds[3] - textBounds[1]
  boxWidth = textWidth + padding * 2
  boxHeight = textHeight + padding * 2

  boxPosition = align(img.size, (boxWidth, boxHeight), position, margin)
  textPosition = (boxPosition[0] + padding, boxPosition[1] + padding)

  logger.debug(f'Text bounds: [x: {textPosition[0]}, y: {textPosition[1]}, width: {textWidth}, height: {textHeight}]')
  logger.debug(f'Background bounds: [x: {boxPosition[0]}, y: {boxPosition[1]}, width: {boxWidth}, height: {boxHeight}]')

  img = img.convert('RGBA')
  overlay = Image.new('RGBA', img.size, (0, 0, 0, 0))
  overlayDraw = ImageDraw.Draw(overlay)
  backgroundInkSolid = ImageColor.getrgb(backgroundColor)
  backgroundInk = (*backgroundInkSolid, percentToByte(backgroundOpacity))
  overlayDraw.rectangle((boxPosition[0], boxPosition[1], boxPosition[0] + boxWidth, boxPosition[1] + boxHeight), fill=backgroundInk) # type: ignore
  img = Image.alpha_composite(img, overlay)
  img = img.convert('RGB')
  imgDraw = ImageDraw.Draw(img)

  outlineColorSolid = ImageColor.getrgb(outlineColor)
  outlineColor = (*outlineColorSolid, percentToByte(outlineOpacity)) # type: ignore
  imgDraw.multiline_text(textPosition, fill=textColor, **drawTextArgs, stroke_fill=outlineColor)

  return img