{"id":952,"date":"2017-03-19T15:58:12","date_gmt":"2017-03-19T07:58:12","guid":{"rendered":"https:\/\/www.ceh-photo.de\/blog\/?p=952"},"modified":"2017-03-19T17:00:33","modified_gmt":"2017-03-19T09:00:33","slug":"script-for-embedding-watermarks-into-images-using-imagemagick-for-windows-and-linux","status":"publish","type":"post","link":"https:\/\/www.ceh-photo.de\/blog\/?p=952","title":{"rendered":"Batch processing &#8211; Resizing and embedding watermarks into images using ImageMagick for Windows and Linux"},"content":{"rendered":"<p>As a response to a recent question I came across, I want to share a simple bash script for embedding a watermark image\/logo and resizing multiple images at the same time for online publishing.<\/p>\n<p>In order to use the script below you need to install imagemagick and if you are using Windows you either have to install the cygwin environment or you would have to rewrite this simple script as a bat script.<\/p>\n<p>Cygwin: <a href=\"https:\/\/cygwin.com\/install.html\">https:\/\/cygwin.com\/install.html<\/a><\/p>\n<p>Imagemagick: <a href=\"https:\/\/www.imagemagick.org\/script\/index.php\">https:\/\/www.imagemagick.org\/script\/index.php<\/a><br \/>\nIn most Linux systems you can just install it using your distros package manager, for instance<\/p>\n<div class=\"codecolorer-container bash default\" style=\"overflow:auto;white-space:nowrap;width:70%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"bash codecolorer\"><span class=\"kw2\">sudo<\/span> apt <span class=\"kw2\">install<\/span> imagemagick<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>Your watermark image\/logo should be a PNG file due to the transparency support of PNG.<\/p>\n<p>Save the script below to watermark.sh<\/p>\n<div class=\"codecolorer-container bash default\" style=\"overflow:auto;white-space:nowrap;width:70%;height:400px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/><\/div><\/td><td><div class=\"bash codecolorer\"><span class=\"co0\">#!\/bin\/bash<\/span><br \/>\n<span class=\"co0\"># Author: Christopher Hrabia - www.ceh-photo.de<\/span><br \/>\n<br \/>\n<span class=\"co0\">#adjust your output folder for watermarked images<\/span><br \/>\n<span class=\"re2\">OUTPUT_DIR<\/span>=<span class=\"st_h\">'.\/watermarked\/'<\/span><br \/>\n<br \/>\n<span class=\"co0\">#adjust the target length in px of the larger image side, this preserves the aspect ratio<\/span><br \/>\n<span class=\"re2\">MAX_WIDTH<\/span>=<span class=\"nu0\">1600<\/span><br \/>\n<span class=\"re2\">MAX_HEIGHT<\/span>=<span class=\"nu0\">1600<\/span><br \/>\n<br \/>\n<span class=\"co0\">#Determine the location of this script<\/span><br \/>\n<span class=\"re2\">CURRENT_SCRIPT_DIR<\/span>=$<span class=\"br0\">&#40;<\/span> <span class=\"kw3\">cd<\/span> <span class=\"st0\">&quot;<span class=\"es4\">$( dirname &quot;${BASH_SOURCE[0]}&quot; )<\/span>&quot;<\/span> <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"kw3\">pwd<\/span> <span class=\"br0\">&#41;<\/span><br \/>\n<br \/>\n<span class=\"co0\">#adjust for your logo, path is determined relative to the script location<\/span><br \/>\n<span class=\"re2\">WATERMARK<\/span>=<span class=\"st0\">&quot;<span class=\"es2\">$CURRENT_SCRIPT_DIR<\/span>\/your_logo.png&quot;<\/span><br \/>\n<br \/>\n<span class=\"co0\">#create the output directory if it does not exist<\/span><br \/>\n<span class=\"kw2\">mkdir<\/span> <span class=\"re5\">-p<\/span> <span class=\"re1\">$OUTPUT_DIR<\/span><br \/>\n<br \/>\n<span class=\"co0\">#iterate over all images forwarded as argument to this script<\/span><br \/>\n<span class=\"kw1\">for<\/span> pic <span class=\"kw1\">in<\/span> <span class=\"st0\">&quot;$@&quot;<\/span>; <span class=\"kw1\">do<\/span><br \/>\n&nbsp; &nbsp; <span class=\"kw3\">echo<\/span> <span class=\"st0\">&quot;Processing: <span class=\"es2\">$pic<\/span>&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"co0\">#resize the image first to simplify image positioning<\/span><br \/>\n&nbsp; &nbsp; convert <span class=\"st0\">&quot;<span class=\"es2\">$pic<\/span>&quot;<\/span> <span class=\"re5\">-resize<\/span> <span class=\"re1\">$MAX_WIDTH<\/span><span class=\"st0\">&quot;x&gt;&quot;<\/span> <span class=\"re5\">-resize<\/span> <span class=\"st0\">&quot;x<span class=\"es2\">$MAX_HEIGHT<\/span>&gt;&quot;<\/span> <span class=\"st0\">&quot;<span class=\"es2\">$OUTPUT_DIR<\/span><span class=\"es2\">$pic<\/span>&quot;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"co0\">#adjust parameters here for other watermark positions<\/span><br \/>\n&nbsp; &nbsp; <span class=\"co0\">#current configuration is putting the watermark in the right bottom corner of the image<\/span><br \/>\n&nbsp; &nbsp; composite <span class=\"re5\">-dissolve<\/span> <span class=\"nu0\">30<\/span><span class=\"sy0\">%<\/span> <span class=\"re5\">-gravity<\/span> SouthEast <span class=\"re5\">-geometry<\/span> +<span class=\"nu0\">25<\/span>+<span class=\"nu0\">5<\/span> <span class=\"st0\">&quot;<span class=\"es2\">$WATERMARK<\/span>&quot;<\/span> <span class=\"st0\">&quot;<span class=\"es2\">$OUTPUT_DIR<\/span><span class=\"es2\">$pic<\/span>&quot;<\/span> <span class=\"st0\">&quot;<span class=\"es2\">$OUTPUT_DIR<\/span><span class=\"es2\">$pic<\/span>&quot;<\/span><br \/>\n<span class=\"kw1\">done<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>Script usage:<\/p>\n<div class=\"codecolorer-container bash default\" style=\"overflow:auto;white-space:nowrap;width:70%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/><\/div><\/td><td><div class=\"bash codecolorer\"><span class=\"kw3\">cd<\/span> PATH<span class=\"sy0\">\/<\/span>TO<span class=\"sy0\">\/<\/span>YOUR<span class=\"sy0\">\/<\/span>IMAGE_FOLDER<br \/>\n<span class=\"co0\">#either call the script with an absolute path or add it to your PATH<\/span><br \/>\nwatermark.sh <span class=\"sy0\">*<\/span>.jpg<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>The watermarked and resized pictures will be stored in the subdirectory &#8220;watermarked&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a response to a recent question I came across, I want to share a simple bash script for embedding a watermark image\/logo and resizing multiple images at the same time for online publishing. In order to use the script below you need to install imagemagick and if you are using Windows you either have [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,46,4,45],"tags":[39,40,43],"class_list":["post-952","post","type-post","status-publish","format-standard","hentry","category-it","category-linux-it-2","category-photography","category-windows-it-2","tag-bash","tag-linux","tag-windows"],"_links":{"self":[{"href":"https:\/\/www.ceh-photo.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/952","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ceh-photo.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ceh-photo.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ceh-photo.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ceh-photo.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=952"}],"version-history":[{"count":10,"href":"https:\/\/www.ceh-photo.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/952\/revisions"}],"predecessor-version":[{"id":962,"href":"https:\/\/www.ceh-photo.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/952\/revisions\/962"}],"wp:attachment":[{"href":"https:\/\/www.ceh-photo.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=952"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ceh-photo.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=952"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ceh-photo.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=952"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}