Spaces:
Sleeping
Sleeping
Ankan Ghosh
commited on
Upload 6 files
Browse files- app.py +96 -0
- images/hp-2.jpg +0 -0
- images/hp.jpg +0 -0
- images/opencv-logo-rz.png +0 -0
- images/opencv-university-rz.png +0 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def apply_watermark(img, logo, mode):
|
| 8 |
+
|
| 9 |
+
# change the mode to BGR for processing
|
| 10 |
+
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
|
| 11 |
+
logo = cv2.cvtColor(logo, cv2.COLOR_RGBA2BGRA)
|
| 12 |
+
|
| 13 |
+
# Get dimensions of the main image and watermark
|
| 14 |
+
main_height, main_width = img.shape[:2]
|
| 15 |
+
watermark_height, watermark_width = logo.shape[:2]
|
| 16 |
+
|
| 17 |
+
# Calculate the top-left corner of the watermark (bottom-right corner placement)
|
| 18 |
+
x = main_width - watermark_width
|
| 19 |
+
y = main_height - watermark_height
|
| 20 |
+
|
| 21 |
+
# If you want the watermark to not exactly stick to the corner, you can add some padding
|
| 22 |
+
padding = 10 # 10 pixels padding for example
|
| 23 |
+
x -= padding
|
| 24 |
+
y -= padding
|
| 25 |
+
|
| 26 |
+
# Place the watermark
|
| 27 |
+
roi = img[y : y + watermark_height, x : x + watermark_width]
|
| 28 |
+
|
| 29 |
+
# Separate the color and alpha channels.
|
| 30 |
+
# print(logo.shape)
|
| 31 |
+
logo_bgr = logo[:, :, 0:3]
|
| 32 |
+
logo_alpha = logo[:, :, 3]
|
| 33 |
+
|
| 34 |
+
# Make the dimensions of the mask same as the input image.
|
| 35 |
+
# Since the background image is a 3-channel image, we create a 3 channel image for the mask.
|
| 36 |
+
logo_mask = cv2.merge([logo_alpha, logo_alpha, logo_alpha])
|
| 37 |
+
logo_mask_inv = cv2.bitwise_not(logo_mask)
|
| 38 |
+
|
| 39 |
+
# Use the mask to create the masked ROI region.
|
| 40 |
+
masked_roi = cv2.bitwise_and(roi, logo_mask_inv)
|
| 41 |
+
|
| 42 |
+
# Use the mask to create the masked logo region.
|
| 43 |
+
masked_logo = cv2.bitwise_and(logo_bgr, logo_mask)
|
| 44 |
+
|
| 45 |
+
if mode == "Opaque":
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
# Combine the masked ROI with the masked logo to get the combined ROI image.
|
| 49 |
+
roi_final = cv2.bitwise_or(masked_roi, masked_logo)
|
| 50 |
+
|
| 51 |
+
# make a image copy
|
| 52 |
+
img_1 = img.copy()
|
| 53 |
+
|
| 54 |
+
# Insert the ROI patch in the image.
|
| 55 |
+
img_1[y : y + watermark_height, x : x + watermark_width] = roi_final
|
| 56 |
+
# cv2.imshow('output',img_1)
|
| 57 |
+
# cv2.waitKey(0)
|
| 58 |
+
return cv2.cvtColor(img_1, cv2.COLOR_BGR2RGB)
|
| 59 |
+
else:
|
| 60 |
+
# Make a copy of the ROI.
|
| 61 |
+
roi_2 = roi.copy()
|
| 62 |
+
|
| 63 |
+
# Blend ROI and the logo.
|
| 64 |
+
watermarked = cv2.addWeighted(roi_2, 1, masked_logo, 0.6, 0)
|
| 65 |
+
|
| 66 |
+
# Make a copy of the image.
|
| 67 |
+
img_2 = img.copy()
|
| 68 |
+
|
| 69 |
+
# Insert the watermarked ROI patch in the image.
|
| 70 |
+
img_2[y : y + watermark_height, x : x + watermark_width] = watermarked
|
| 71 |
+
# cv2.imshow('output',img_2)
|
| 72 |
+
# cv2.waitKey(0)
|
| 73 |
+
return cv2.cvtColor(img_2, cv2.COLOR_BGR2RGB)
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def main(img, logo, mode):
|
| 77 |
+
img_final = apply_watermark(img, logo, mode)
|
| 78 |
+
return img_final
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
image_input = gr.Image(type="numpy", label="Input Image")
|
| 82 |
+
logo_input = gr.Image(type="numpy", label="Logo Image", image_mode="RGBA")
|
| 83 |
+
mode = gr.Radio(["Opaque", "Transparent"], label="Watermark Mode", info="Choose the MODE")
|
| 84 |
+
final_output = gr.Image(type="numpy", label="Output Image")
|
| 85 |
+
|
| 86 |
+
interface = gr.Interface(
|
| 87 |
+
fn=main,
|
| 88 |
+
inputs=[image_input, logo_input, mode],
|
| 89 |
+
outputs=[final_output],
|
| 90 |
+
title="OpenCV Watermark",
|
| 91 |
+
description="Upload your Input Image and choose a logo to create your watermark image!",
|
| 92 |
+
examples=[["./images/hp-2.jpg", "./images/opencv-logo-rz.png"], ["./images/hp.jpg", "./images/opencv-university-rz.png"]],
|
| 93 |
+
cache_examples=False,
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
interface.launch()
|
images/hp-2.jpg
ADDED
|
images/hp.jpg
ADDED
|
images/opencv-logo-rz.png
ADDED
|
images/opencv-university-rz.png
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.42.0
|
| 2 |
+
opencv-python==4.10.0.84
|