Spaces:
Runtime error
Runtime error
Update overlay.py
Browse files- overlay.py +40 -3
overlay.py
CHANGED
|
@@ -1,6 +1,43 @@
|
|
| 1 |
import cv2
|
| 2 |
-
from PIL import Image
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def textover(im,txt):
|
| 6 |
inp=2
|
|
@@ -20,8 +57,8 @@ def textover(im,txt):
|
|
| 20 |
print (h)
|
| 21 |
font_x = (w/2)
|
| 22 |
font_y = h-hhh
|
| 23 |
-
out =
|
| 24 |
-
out =
|
| 25 |
|
| 26 |
return out
|
| 27 |
|
|
|
|
| 1 |
import cv2
|
| 2 |
+
from PIL import ImageFont, ImageDraw, Image
|
| 3 |
|
| 4 |
+
def process(img,font_text,font_fac,font_x,font_y,font_col,font_op):
|
| 5 |
+
img.save('tmp.png')
|
| 6 |
+
|
| 7 |
+
img = Image.open('tmp.png').convert("RGBA")
|
| 8 |
+
im=img
|
| 9 |
+
#im=Image.open(img)
|
| 10 |
+
txt = Image.new('RGBA', im.size, (255,255,255,0))
|
| 11 |
+
|
| 12 |
+
w, h = im.size
|
| 13 |
+
print (f'FONT COLOR: {font_col}')
|
| 14 |
+
#t_fill = ("#"+"E6"+f"{font_col.strip('#')}")
|
| 15 |
+
|
| 16 |
+
#t_fill = (font_col)
|
| 17 |
+
h1 = font_col.strip("#")
|
| 18 |
+
rgb_tup = tuple(int(h1[i:i+2], 16) for i in (0, 2, 4))
|
| 19 |
+
print (rgb_tup)
|
| 20 |
+
a,b,c = rgb_tup
|
| 21 |
+
t_fill = (a,b,c,font_op)
|
| 22 |
+
|
| 23 |
+
print (f'FONT COLOR: {t_fill}')
|
| 24 |
+
|
| 25 |
+
#x = int(w-font_x)
|
| 26 |
+
#y = int(h-font_y)
|
| 27 |
+
x = int(font_x)
|
| 28 |
+
y = int(font_y)
|
| 29 |
+
draw = ImageDraw.Draw(txt)
|
| 30 |
+
text = f'{font_text}'
|
| 31 |
+
font_size=font_fac
|
| 32 |
+
font = ImageFont.truetype("./fonts/SansitaOne.ttf", int(font_size))
|
| 33 |
+
size = font.getsize(text)
|
| 34 |
+
draw.text((x-size[0]/2, y),text, font = font, fill=t_fill)
|
| 35 |
+
|
| 36 |
+
#txt.putalpha(128)
|
| 37 |
+
|
| 38 |
+
combined = Image.alpha_composite(im, txt)
|
| 39 |
+
|
| 40 |
+
return combined
|
| 41 |
|
| 42 |
def textover(im,txt):
|
| 43 |
inp=2
|
|
|
|
| 57 |
print (h)
|
| 58 |
font_x = (w/2)
|
| 59 |
font_y = h-hhh
|
| 60 |
+
out = process(im,txt,font_fac=font_a,font_x=font_x,font_y=hh,font_col="#000000",font_op=255)
|
| 61 |
+
out = process(out,"Contact Card",font_fac=font_b,font_x=font_x,font_y=font_y,font_col="#000000",font_op=255)
|
| 62 |
|
| 63 |
return out
|
| 64 |
|