Search code examples
pythonpython-imaging-librarystreamlit

How to add a text in the captured image


I have created a app which capture the image and convert into pencil sketch.

I need to add a watermark inside the capture image I find for the documentation I didn't get the exact one let me know how to add a water mark inside the image or any idea would be appreciated.

import base64
import streamlit as st 
import numpy as np
from PIL import Image  
import cv2 

def dodgeV2(x, y):
    return cv2.divide(x, 255 - y, scale=256)

def pencilsketch(inp_img):
img_gray = cv2.cvtColor(inp_img, cv2.COLOR_BGR2GRAY)
img_invert = cv2.bitwise_not(img_gray)
img_smoothing = cv2.GaussianBlur(img_invert, (21, 21),sigmaX=0, sigmaY=0)
final_img = dodgeV2(img_gray, img_smoothing)
logo_img = cv2.imread("Watertext.jpg")
logo_gray = cv2.cvtColor(logo_img, cv2.COLOR_BGR2GRAY)
logo_height, logo_width = logo_gray.shape[:2]
#y_offset = x_offset = 0 # paste to the top left of image
x_offset = final_img.shape[1] - logo_width
y_offset = 0
final_img[x_offset:x_offset+logo_height, y_offset:y_offset+logo_width] = logo_gray
return(final_img)

Water Mark


Solution

  • Modify your pencilsketch function like

    def pencilsketch(inp_img):
        img_gray = cv2.cvtColor(inp_img, cv2.COLOR_BGR2GRAY)
        img_invert = cv2.bitwise_not(img_gray)
        img_smoothing = cv2.GaussianBlur(img_invert, (21, 21),sigmaX=0, sigmaY=0)
        final_img = dodgeV2(img_gray, img_smoothing)
        logo_img = cv2.imread("path/to/logo/image.jpg")
        logo_gray = cv2.cvtColor(logo_img, cv2.COLOR_BGR2GRAY)
        logo_height, logo_width = logo_gray.shape[:2]
        x_offset = y_offset = 0 # paste to the top left of image
        final_img[y_offset:y_offset+logo_height, x_offset:x_offset+logo_width] = logo_gray
        return(final_img)
    

    If you want to paste in top right, change line

        x_offset = y_offset = 0 # paste to the top left of image
    

    to

        x_offset = final_img.shape[1] - logo_width
        y_offset = 0
    

    If you want to paste in bottom right, change line

        x_offset = y_offset = 0 # paste to the top left of image
    

    to

        x_offset = final_img.shape[1] - logo_width
        y_offset = final_img.shape[0] - logo_height