Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2 as ocv
- class ImageStamping:
- def _watermarking(self):
- img_name = input("Enter file name:")
- watermark_name = input("Input watermark name:")
- img = ocv.imread(img_name)
- watermark = ocv.imread(watermark_name)
- # Preserve ratio of image
- image_scaling = 20
- img_width = int(img.shape[1] * image_scaling/100)
- img_height = int(img.shape[0] * image_scaling/100)
- img_dim = (img_width, img_height)
- img_resized = ocv.resize(img, img_dim, interpolation=ocv.INTER_AREA)
- filename = 'new_image_size.jpg'
- wm_scale = 40
- wm_width = int(watermark.shape[1] * wm_scale/100)
- wm_height = int(watermark.shape[0] * wm_scale/100)
- wm_dim = (wm_width, wm_height)
- resized_wm = ocv.resize(watermark, wm_dim, interpolation=ocv.INTER_AREA)
- h_img, w_img, _ = img_resized.shape
- center_y = int(h_img/2)
- center_x = int(w_img/2)
- h_wm, w_wm, _ = resized_wm.shape
- top_y = center_y - int(h_wm/2)
- left_x = center_x - int(w_wm/2)
- bottom_y = top_y + h_wm
- right_x = left_x + w_wm
- roi = img_resized[top_y:bottom_y, left_x:right_x]
- result = ocv.addWeighted(roi, 1, resized_wm, 0.3, 0)
- img_resized[top_y:bottom_y, left_x:right_x] = result
- # Apply watermark
- ocv.imwrite(filename, img_resized)
- #ocv.imshow(watermark_name, img)
- ocv.imshow("Resized Input Image", img_resized)
- ocv.waitKey(0)
- ocv.destroyAllWindows()
- obj = ImageStamping()
- obj._watermarking()
Advertisement
Add Comment
Please, Sign In to add comment