Skip to content

Instantly share code, notes, and snippets.

@Scoppio
Created November 13, 2016 21:02
Show Gist options
  • Save Scoppio/ea1e33c2ad40160bb661359d9aa9355b to your computer and use it in GitHub Desktop.
Save Scoppio/ea1e33c2ad40160bb661359d9aa9355b to your computer and use it in GitHub Desktop.
Watermark pictures inside a folder
# Coding: utf-8
# Python 3.5
# OpenCV 3.1.0
#
# Developed by Lucas Coppio
#
__author__ = "Lucas Scoppio"
__version__ = "0.1"
__date__ = "26 july 2016"
__copyright__ = """MIT License, look it up"""
from imutils import paths
import numpy as np
import cv2
import argparse
import os
def resize(frame,
scale=None,
size=None,
fx=0,
fy=0,
interpolation=cv2.INTER_CUBIC):
'''
Resize image
size = Resize image with fixed dimension
scale = If set, ignores the value of `size` and resize dividing the image dimensions
fx = scale factor along the horizontal axis
fy = scale factor along the vertical axis
interpolation = default is bicubic interpolation over 4x4 pixel neighborhood
'''
sz = None if not size else size
if scale:
sz = (int(frame.shape[1] // scale), int(frame.shape[0] // scale))
scaled_image = cv2.resize(frame, sz, fx=fx, fy=fy, interpolation=interpolation)
return scaled_image
ap = argparse.ArgumentParser()
ap.add_argument("-w", "--watermark", required=True,
help="input watermark")
ap.add_argument("-i", "--input", required=True,
help="input directory of images")
ap.add_argument("-o", "--output", required=True,
help="output directory of watermarked images")
ap.add_argument("-a", "--alpha", type = float, default = 0.25,
help="output directory of watermarked images")
ap.add_argument("-c", "--correct", type=int, default=1,
help="flag used to handle if bug is displayed or not")
ap.add_argument("-p", "--preview", action="store_true",
help="shows real-time preview of the video")
args = vars(ap.parse_args())
watermark = cv2.imread(args["watermark"], cv2.IMREAD_UNCHANGED)
(wH, wW) = watermark.shape[:2]
if args["correct"] > 0:
(B, G, R, A) = cv2.split(watermark)
B = cv2.bitwise_and(B, B, mask=A)
G = cv2.bitwise_and(G, G, mask=A)
R = cv2.bitwise_and(R, R, mask=A)
watermark = cv2.merge([B, G, R, A])
# loop over the input images
imgNumber = 0
for imagePath in paths.list_images(args["input"]):
imgNumber += 1
print("marking image {0} :: {1}".format(imgNumber,imagePath))
(wH, wW) = watermark.shape[:2]
# load the input image, then add an extra dimension to the
# image (i.e., the alpha transparency)
image = cv2.imread(imagePath)
(h, w) = image.shape[:2]
factor = h*0.3 / wH
#print(h*0.3, wH, factor, imagePath)
watermarkR = resize (watermark, fx=factor, fy=factor)
(wH, wW) = watermarkR.shape[:2]
image = np.dstack([image, np.ones((h, w), dtype="uint8") * 255])
# construct an overlay that is the same size as the input
# image, (using an extra dimension for the alpha transparency),
# then add the watermark to the overlay in the bottom-right
# corner
overlay = np.zeros((h, w, 4), dtype="uint8")
overlay[h - wH - 20:h - 20, w - wW - 20:w - 20] = watermarkR
# blend the two images together using transparent overlays
output = image.copy()
cv2.addWeighted(overlay, args["alpha"], output, 1.0, 0, output)
# write the output image to disk
filename = imagePath[imagePath.rfind(os.path.sep) + 1:]
p = os.path.sep.join((args["output"], filename))
cv2.imwrite(p, output)
if args["preview"]:
cv2.imshow('frame',output)
k = cv2.waitKey(30) & 0xff
if k == 27:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment