; Script-fu (Scheme) script to add a watermark signature to an image ; Some constant names for debugging ; ORIENTATION-HORIZONTAL=0; ORIENTATION-VERTICAL=1 ; RUN-INTERACTIVE=0; RUN-NONINTERACTIVE=1 ; UTF-8 encoding=0; filename encoding=1 ; ; Scale an image relative to another image ; Main application is for insertion of watermark ; I like copyright text to be 2% to 3% height of image ; Arguments: ; imgToScale The image to be scaled ; img The image relative to which imgToScale is to be scaled ; scaleFactor per cent of img height of the final scaled image ; Returns: the scaled image ; Scale factor is expressed as a percent of the "parent" image's size (define (scale-relative imgToScale img scaleFactor) ; (display "scale-relative:\n") ; (display (string-append " scaleFactor:" (number->string scaleFactor) "\n")) (let* ( (scaledLayer 0) ; the layer to be scaled (layer 0) ; the layer to manipulate ; attributes and drawables from the original image ; (imgLayer (car (gimp-image-get-active-layer img))) ; layer from the original image (imgHt (car (gimp-image-height img))) ; primary image height (imgWid (car (gimp-image-width img))) ; primary image width (imgToScaleWid (car (gimp-image-width imgToScale))) ; width of image to be scaled (imgToScaleHt (car (gimp-image-height imgToScale))) ; height of image to be scaled ; variables needed for scaling (scaledHt 0) ; scale image height (scaledWid 0) ; scale image width (resultScaleFactor 0) ; scale factor for layer to scale ) ; Compute scaled size ; Always scale relative to height ; scaledHeight = imageHeight * scaleFactor / 100 ; scaleFactor = scaledHeight / imageToScaleHeight (set! scaledHt (/ (* imgHt scaleFactor) 100)) ; (display (string-append " imgToScaleHt:" (number->string imgToScaleHt) "\n")) ; (display (string-append " scaledHt:" (number->string scaledHt) "\n")) (set! resultScaleFactor (/ scaledHt imgToScaleHt)) ; (display (string-append " resultScaleFactor:" (number->string resultScaleFactor) "\n")) (set! scaledWid (* imgToScaleWid resultScaleFactor)) ; (display (string-append " imgToScaleWid:" (number->string imgToScaleWid) "\n")) ; (display (string-append " scaledWid:" (number->string scaledWid) "\n")) ; set to the best interpolation we can do and scale the image (gimp-context-set-interpolation 3) ; set the interpolation method to use (gimp-image-scale imgToScale scaledWid scaledHt) ; scale the image (display " scaledImage:") (print imgToScale) imgToScale ; force the return value ) ) ; Add watermark to an image ; Arguments: ; Name of file to process (xxx.tif, etc) ; Image to which watermark will be added ; Drawable, unused ; Name of file containing watermark (xxx.png, etc) ; Relative height (per cent) of watermark to original image ; Opacity of watermark (define (add-watermark orgImg drw watermarkFileName relHt opacity) ; (display "\nadd-watermark\n") ; (display (string-append " fileName=\"" fileName "\"\n")) ; (display (string-append " watermarkFileName=\"" watermarkFileName "\"\n")) ; (display (string-append " relHt=\"" (number->string relHt) "\"\n")) ; (display (string-append " opacity=\"" (number->string opacity) "\"\n")) (let* ( ; (orgImg 0) ; original image (watermarkImg 0) ; watermark image (layer 0) ; the layer to manipulate ; (multiplier (/ opacity 100)) ; opacity as a decimal multiplier (xOff (car (gimp-image-width orgImg))) ; x offset for added watermark (yOff (car (gimp-image-height orgImg))) ; y offset for added watermark ) ; open the image and the watermark ; (set! orgImg (car (file-tiff-load RUN-NONINTERACTIVE fileName fileName))) ; load the original image (set! watermarkImg (car (file-png-load RUN-NONINTERACTIVE watermarkFileName watermarkFileName))) ; load the watermark image ; establish relative height for added image (if (equal? relHt "") (set! relHt "2") ) (gimp-context-push) (gimp-image-undo-group-start orgImg) ; scale the watermark image ; (display "watermarkImg:") (print watermarkImg) ; (display (string-append " ht:" (number->string (car (gimp-image-height watermarkImg))) "\n")) (scale-relative watermarkImg orgImg relHt) ; (display (string-append " ht:" (number->string (car (gimp-image-height watermarkImg))) "\n")) ; set the opacity of the scaled image ; Note! 0 <= opacity <= 100, NOT 0 <= opacity <= 1 as noted in the function definition (set! layer (car (gimp-image-get-active-layer watermarkImg))) ; get the layer (drawable) to modify ; (display "layers:") (print layer) ; (display (string-append " multiplier:" (number->string multiplier) "\n")) (gimp-layer-set-opacity layer opacity) ; add the scaled image to the main image (set! layer (car (gimp-layer-new-from-drawable layer orgImg))) ; (display "layer:") (print layer) (gimp-image-insert-layer orgImg layer 0 0) (gimp-layer-set-name layer "Copyright") ; position the scaled image in the lower right corner of the main image ; (display (string-append "xOff:" (number->string xOff))) (set! xOff (- xOff (car (gimp-drawable-width layer)))) ; (display (string-append " xOff:" (number->string xOff))) (set! yOff (- yOff (car (gimp-drawable-height layer)))) ; (display (string-append " yOff:" (number->string yOff))) (gimp-layer-set-offsets layer xOff yOff); (gimp-image-delete watermarkImg) ; get rid of the original watermark image (gimp-image-undo-group-end orgImg) (gimp-displays-flush) (gimp-context-pop) ; Need to save the modified image ; (gimp-image-delete orgImg) ; get rid of the original image ) ) ; Register add-watermark ; NOTE!!! The script-fu browser will show an additional argument, SF-MODE, which it automatically ; adds and removes in some crazy way. ; It appears to only be of significance when calling a non-script-fu procedure from a script-fu, ; or vice-versa, and its purpose is to indicate whether or not the argument dialog box should be displayed. ; So basically, for my purposes, it is of no significance whatsoever. ; SF-MODE "Mode" "RUN-INTERACTIVE" ; First arg is run mode ; NOTE!!! It also appears there is no way to use this procedure both interactively and non-interactively, ; because of the need to specify the name of the input file for non-interactive use. ; So note the additional procedure, batch-add-watermark (script-fu-register "add-watermark" ; function name "Add Watermark..." ; menu label "Adds a scaled, partially transparent image to an image" "Gary Aitken" ; author "Copyright 2013, Gary Aitken" ; copyright notice "2013-08-28 v 0.1" ; date created "" ; image type the script works on SF-IMAGE "Image" 0 ; image to which watermark will be added SF-DRAWABLE "Drawable" 0 ; unused SF-STRING "Added image file" "/home/garya/Photos/Copyright.png" ; file containing image to add ; SF-VALUE "Relative (percent) height of added image" "5" ; height of scaled image as a percent of main image height SF-ADJUSTMENT "Relative (percent) height of added image" '(3 1 100 1 10 1 0) ; height of scaled image as a percent of main image height SF-ADJUSTMENT "Opacity of added image" '(50 1 100 1 10 1 0) ; Opacity of scaled image ) ; "/garya" puts a top level menu item of "garya" on the menu bar for the image window (script-fu-menu-register "add-watermark" "/garya") ; (add-watermark "/home/garya/Photos/Wildlife/Butterflies/Butterfly_PearlCrescent_0004_20130710.tif" "/home/garya/Photos/Copyright.png" 2 70) ; add-watermark script for use from batch mode (define (batch-add-watermark inFileName watermarkFileName outFileName relHt opacity) (let* ( (orgImg (car (gimp-file-load RUN-NONINTERACTIVE inFileName inFileName))) (drw (car (gimp-image-get-active-layer orgImg))) ) (add-watermark orgImg drw watermarkFileName relHt opacity) (gimp-file-save RUN-NONINTERACTIVE orgImg drw outFileName outFileName) (gimp-image-delete orgImg) ) )