the_baahubali

Untitled

Mar 4th, 2022
1,038
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.65 KB | None | 0 0
  1. import cv2 as ocv
  2.  
  3.  
  4. class ImageStamping:
  5.  
  6.     def _watermarking(self):
  7.  
  8.         img_name = input("Enter file name:")
  9.         watermark_name = input("Input watermark name:")
  10.         img = ocv.imread(img_name)
  11.         watermark = ocv.imread(watermark_name)
  12.  
  13.         # Preserve ratio of image
  14.         image_scaling = 20
  15.         img_width = int(img.shape[1] * image_scaling/100)
  16.         img_height = int(img.shape[0] * image_scaling/100)
  17.         img_dim = (img_width, img_height)
  18.         img_resized = ocv.resize(img, img_dim, interpolation=ocv.INTER_AREA)
  19.         filename = 'new_image_size.jpg'
  20.        
  21.         wm_scale = 40
  22.         wm_width = int(watermark.shape[1] * wm_scale/100)
  23.         wm_height = int(watermark.shape[0] * wm_scale/100)
  24.         wm_dim = (wm_width, wm_height)
  25.         resized_wm = ocv.resize(watermark, wm_dim, interpolation=ocv.INTER_AREA)
  26.        
  27.         h_img, w_img, _ = img_resized.shape
  28.         center_y = int(h_img/2)
  29.         center_x = int(w_img/2)
  30.         h_wm, w_wm, _ = resized_wm.shape
  31.         top_y = center_y - int(h_wm/2)
  32.         left_x = center_x - int(w_wm/2)
  33.         bottom_y = top_y + h_wm
  34.         right_x = left_x + w_wm
  35.        
  36.         roi = img_resized[top_y:bottom_y, left_x:right_x]
  37.        
  38.         result = ocv.addWeighted(roi, 1, resized_wm, 0.3, 0)
  39.         img_resized[top_y:bottom_y, left_x:right_x] = result
  40.  
  41.         # Apply watermark
  42.         ocv.imwrite(filename, img_resized)
  43.         #ocv.imshow(watermark_name, img)
  44.         ocv.imshow("Resized Input Image", img_resized)
  45.  
  46.         ocv.waitKey(0)
  47.         ocv.destroyAllWindows()
  48.        
  49.        
  50.  
  51. obj = ImageStamping()
  52. obj._watermarking()
Advertisement
Add Comment
Please, Sign In to add comment