Monday, June 25, 2012

Watermark using Composite, Python and Identify

A few days ago I wanted to watermark few of my images on cmd line and I wrote this blog http://bullet-bala.blogspot.in/2012/06/adding-watermark.html and the trouble which I had was that the Watermark was either very small or very big and got out of the final image. The result of which I created a small python script to extract the Dimension of an image and resize my Watermarker so my water marker always looks okay on the final image.

Initial step create an watermark image and put in inside /usr/local/src/watermark1.jpeg

Next call my python script on command line like so




$cat  watermark.py


#!/usr/bin/python
#Author: Balasubramaniam Natarajan
#Create WaterMark
import subprocess
import sys
import os

#Here I want to get the File to WaterMark
if len(sys.argv) < 2:
print"You need to specify the src image file"
print"For example"
print"watermark.py "
sys.exit()

src = sys.argv[1]
print "Your Input Image File is: ", src

#This command extracts the Dimentions of my Image
CreateImgSize = "identify -format '%wx%h\n' "+src
Size = subprocess.Popen(CreateImgSize, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
out, error = Size.communicate()
#This is to trim the additional new line from the above command.
out = out.rstrip('\n')
print "The Dimension of the Image is: ",out, " Pixels"

#This is going to be the name of the output file.
WaterMarkedImage = src+"_output.png"
WaterMarkCmd = "composite -dissolve 10% -gravity south -resize ", out, " /usr/local/src/watermark1.jpeg  ",src, WaterMarkedImage
# The above WaterMarkCmd become tuple which os.system can't accept.
#print type(WaterMarkCmd)
# FinalCmd is a string got from the tuple
FinalCmd = " ".join(WaterMarkCmd)
print "The final command is: ", FinalCmd
os.system(FinalCmd)

#END


1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete