Spaces:
Runtime error
Runtime error
| import cv2 | |
| from PIL import ImageFont, ImageDraw, Image | |
| def process(img,font_text,font_fac,font_x,font_y,font_col,font_op): | |
| img.save('tmp.png') | |
| img = Image.open('tmp.png').convert("RGBA") | |
| im=img | |
| #im=Image.open(img) | |
| txt = Image.new('RGBA', im.size, (255,255,255,0)) | |
| w, h = im.size | |
| print (f'FONT COLOR: {font_col}') | |
| #t_fill = ("#"+"E6"+f"{font_col.strip('#')}") | |
| #t_fill = (font_col) | |
| h1 = font_col.strip("#") | |
| rgb_tup = tuple(int(h1[i:i+2], 16) for i in (0, 2, 4)) | |
| print (rgb_tup) | |
| a,b,c = rgb_tup | |
| t_fill = (a,b,c,font_op) | |
| print (f'FONT COLOR: {t_fill}') | |
| #x = int(w-font_x) | |
| #y = int(h-font_y) | |
| x = int(font_x) | |
| y = int(font_y) | |
| draw = ImageDraw.Draw(txt) | |
| text = f'{font_text}' | |
| font_size=font_fac | |
| font = ImageFont.truetype("./fonts/SansitaOne.ttf", int(font_size)) | |
| size = font.getsize(text) | |
| draw.text((x-size[0]/2, y),text, font = font, fill=t_fill) | |
| #txt.putalpha(128) | |
| combined = Image.alpha_composite(im, txt) | |
| return combined | |
| def textover(im,txt1="",txt2=""): | |
| #im.save('tmp.png') | |
| im = Image.open(im) | |
| inp=1 | |
| hh=0 | |
| hhh=25 | |
| #cnt = len(inp) | |
| cnt = inp | |
| font_a = 30 | |
| font_b = 10 | |
| if cnt >0: | |
| font_a = font_a + (cnt * 2) | |
| font_b = font_b + (cnt * 2) | |
| #hh = hh-int(cnt/2) | |
| hhh = hhh+int(cnt/2) | |
| w,h = im.size | |
| print (w) | |
| print (h) | |
| font_x = (w/2) | |
| font_y = h-hhh | |
| out = process(im,txt1,font_fac=font_a,font_x=font_x,font_y=hh,font_col="#000000",font_op=255) | |
| out = process(out,txt2,font_fac=font_b,font_x=font_x,font_y=font_y,font_col="#000000",font_op=255) | |
| #out.save("out.png") | |
| return out | |
| def background_image(): | |
| img_b = np.zeros([512,512,3],dtype=np.uint8) | |
| img_b.fill(255) | |
| img_b.save("background.png") | |
| def custom_overlay(txt=None): | |
| background_image() | |
| background = Image.open("background.png") | |
| x,y = background.size | |
| if x > y: | |
| aa=y | |
| if y > x: | |
| aa=x | |
| if x==y: | |
| aa=x | |
| out = f'{txt}' | |
| print (f'txt {out}') | |
| qrm = qr.QRCode(box_size=10,error_correction=qr.constants.ERROR_CORRECT_H) | |
| qrm.add_data(out) | |
| qrm.make(fit=True) | |
| img1 = qrm.make_image(fill_color=fill, back_color="white") | |
| img1 = img1.resize((aa,aa)) | |
| img1.save("im2.png") | |
| image_bgr = cv2.imread('im2.png') | |
| h, w, c = image_bgr.shape | |
| image_bgra = np.concatenate([image_bgr, np.full((h, w, 1), 255, dtype=np.uint8)], axis=-1) | |
| white = np.all(image_bgr == [255, 255, 255], axis=-1) | |
| image_bgra[white, -1] = 0 | |
| cv2.imwrite('result.png', image_bgra) | |
| over_im = Image.open("result.png") | |
| background.paste(over_im, (0, 0), over_im) | |
| background.save("out_im.png") | |
| return "out_im.png" |