Home > Blockchain >  Can we add border to a text in OpenCV and make the inside part of the text transparent
Can we add border to a text in OpenCV and make the inside part of the text transparent

Time:11-01

Can we make a watermark using OpenCV
enter image description here

this was made using HTML code but the problem is I want to make the same thing using OpenCV or pillow or any other package that would be really helpful

I have researched a bit and come this far

    def generate_watermark_image(self, banner_details):
        banner_image = requests.get(banner_details.get('banner_url'))
        open('image.jpg', 'wb').write(banner_image.content)
        background_image = Image.open('image.jpg').convert("RGBA")
        actual_width, actual_height = background_image.size
        watermark_text = Image.new('RGBA', background_image.size, (255,255,255,0))
        font_size = '50'
        if actual_width>1000 and actual_height>1000:
            font_size = '100'
        title_font = ImageFont.truetype("arial", int(font_size))
        font_width, font_height = title_font.getsize('PLOTCH')
        padding_left = int((banner_details.get('width')/2)-(font_width/2))
        padding_top = int((banner_details.get('height')/2)-(font_height*2))
        d = ImageDraw.Draw(watermark_text)    
        d.text((padding_left, padding_top), "PLOTCH", fill=(255, 255, 255, 150), font=title_font)
        combined = Image.alpha_composite(background_image, watermark_text)
        background_image = cv2.cvtColor(np.array(combined), cv2.COLOR_RGB2BGR) 
        cv2.imwrite("font/minion.png", background_image)
        return background_image

using my function I was able to do only this this is the response am getting can we somehow make the inside part transparent as we do using -webkit-text-fill-color: transparent; in html

CodePudding user response:

This is one way to create outline text in Python/OpenCV.

Basically, draw white text on a black image, then draw smaller black text over the white image. Then use that as a mask to blend between a color image and the original image.

Input:

enter image description here

import cv2
import numpy as np

# read input image
img = cv2.imread('lena.jpg')
ht, wd = img.shape[:2]

# create black image 
mask = np.zeros_like(img)

# put white text on black image at desired coordinates
#cv2.putText(label_img, str(index), (x,y), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0,0,255), 2)
mask = cv2.putText(mask, "PLOTCH", (50, 128), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255,255,255), 5)

# put smaller black text over white text
mask = cv2.putText(mask, "PLOTCH", (50, 128), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0,0,0), 2)

# create text color image (white, for example)
color_img = np.full_like(img, (255,255,255))

# blend input with color image using mask
result = np.where(mask==(255, 255, 255), color_img, img)

# save result
cv2.imwrite("lena_outline_mask.jpg", mask)
cv2.imwrite("lena_outline_text.jpg", result)

# show images
cv2.imshow("mask", mask)
cv2.imshow("result", result)
cv2.waitKey(0)

Mask Image:

enter image description here

Result:

enter image description here

  • Related