lunedì 5 luglio 2010

GIMP Scripting - HDR

Dato che ormai ho preso un po' di mano con lo scripting in GIMP sto cercando di realizzare degli effetti che mi piacciono da applicare indistintamente alle foto senza dover ripetere continuamente tutte le operazioni.
L'HDR (High Dynamic Range) è l'effetto che ho scelto; se vogliamo essere pignoli questo effetto necessiterebbe di almeno 3 immagini, scattate con tempi di esposizione diversi, in maniera da avere un'immagine sovraesposta, una normale ed una sottoesposta... unendo queste tre con particolari procedure si ottiene un HDR reale.
Siccome non sempre è possibile procurarsi facilmente 3 immagini con esposizioni diverse, un po' di tempo fa ho trovato un tutorial che spiegava come ottenere un'immagine simil HDR partendo da un singolo scatto applicandogli vari filtri per simulare la sovra/sotto esposizione ottnendo un buon risultato; trovando la procedura valida ho scriptato il processo spiegato ottenendo l'effetto voluto tramite il seguente  script-fu:

; Script to make HDR Image from one layer file
;
; The script is located in "<Image> / Script-Fu / SebaX75 / HDR..."
;
; Last changed: 29 Aprile 2010
;
; Copyright (C) 2010 SebaX75 <sebax75 AT yahoo DOT it>
;
; --------------------------------------------------------------------
; 
; Changelog:
;  Version 0.1
;    - Initial version, with manual dodge-burn 
;  Version 0.2
;    - Added dodge-burn code and automated
;
; --------------------------------------------------------------------
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 3 of the License, or
; (at your option) any later version.  
; 
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
; 
; You should have received a copy of the GNU General Public License
; along with this program; if not, you can view the GNU General Public
; License version 3 at the web site http://www.gnu.org/licenses/gpl-3.0.html
; Alternatively you can write to the Free Software Foundation, Inc., 675 Mass
; Ave, Cambridge, MA 02139, USA.
;

; This part code was got from dodge-burn.scm script-fu (http://registry.gimp.org/files/dodge-burn.scm) by Harry Phillips <script-fu@tux.com.au>
(define (my-layer-dodge-burn    myImage
   myLayer
   modeOp
   thinNum
   thickNum
 )
 ;Initiate some variables
 (let* (
  (firstTemp (car (gimp-layer-copy myLayer 1)))
  (thinTemp)
  (thickTemp)
  (merged)
  )
  ;Rename the layer
  (gimp-drawable-set-name firstTemp "First temp")
  ;Add the first layer to the image
  (gimp-image-add-layer myImage firstTemp 0)
  ;Desaturate the layer
  (gimp-desaturate firstTemp)
  ;Copy and add the dodge me layer as the thin layer
  (set! thinTemp (car (gimp-layer-copy firstTemp 1)))
  (gimp-image-add-layer myImage thinTemp 0)
  ;Blur the thin layer
  (plug-in-gauss 1 myImage thinTemp thinNum thinNum 0)
  (if (= modeOp 1)
   ;Change the mode of the thin layer to lighten
   (gimp-layer-set-mode thinTemp 10)
   ;Change the mode of the thin layer to darken
   (gimp-layer-set-mode thinTemp 9)
  )
  ;Blur the dodge me layer
  (plug-in-gauss 1 myImage firstTemp thickNum thickNum 0)
  ;Copy the dodge me layer as a new layer
  (set! thickTemp (car (gimp-layer-copy firstTemp 1)))
  ;Add the new layer to the image
  (gimp-image-add-layer myImage thickTemp 1)
  ;Merge the top layer down and keep track of the newly merged layer
  (set! merged (car (gimp-image-merge-down myImage thinTemp 0)))
  ;Change the mode of the dodge copy layer to difference mode
  (gimp-layer-set-mode merged 6)
  ;Merge the top layer down and keep track of the newly merged layer
  (set! merged (car (gimp-image-merge-down myImage merged 0)))
  (if (= modeOp 1)
   (begin
    ;Rename the layer
    (gimp-drawable-set-name merged "Dodge channel")
    ;Change the mode of the dodge copy layer to dodge mode
    (gimp-layer-set-mode merged 16)
   )
   (begin
    ;Rename the layer
    (gimp-drawable-set-name merged "Burn channel")
    ;Change the mode of the dodge copy layer to dodge mode
    (gimp-layer-set-mode merged 17)
    ;Invert layer
    (gimp-invert merged)
   )
  )
  ;Return
 )
)

(define (script-fu-SebaX75-HDR      myImage
    layer0
    thinAmount
    thickAmount
 )
;       Start an undo group so the process can be undone with one undo
 (gimp-image-undo-group-start myImage)

 ; Rimuove qualunque selezione
 (gimp-selection-none myImage)

 ; Esegue la normalizzazione del layer principale
 (plug-in-normalize 1 myImage layer0)

 ; Inizializzo le variabili da utilizzare
 (let* ( (layer1()) (layer2()) (layer3()) (layer4()) )

  ; Creo una copia del layer principale, lo aggiungo e lo rinomino
  (set! layer1 (car (gimp-layer-copy layer0 1)))
  (gimp-image-add-layer myImage layer1 0)
  (gimp-drawable-set-name layer1 "Op. 50%")

  ; Desaturo, inverto ed applico l'effetto chiaritore sfumato al nuovo layer
  (gimp-desaturate layer1)
  (gimp-invert layer1)
  (plug-in-softglow 1 myImage layer1 10 0.75 0.85)

  ; Imposto il merging del layer a "Soft light" con opacita' al 50%
  (gimp-layer-set-mode layer1 SOFTLIGHT-MODE)
  (gimp-layer-set-opacity layer1 50)

  ; Creo una copia del layer modificato, imposto l'opacita' al 75% e lo rinomino
  (set! layer2 (car (gimp-layer-copy layer1 1)))
  (gimp-image-add-layer myImage layer2 -1)
  (gimp-layer-set-opacity layer2 75)
  (gimp-drawable-set-name layer2 "Op. 75%")

  ; Creo il layer di testa, modifico l'istogramma dei colori, eseguo il merging ed imposto la trasparenza al 40% (valori consigliati 30%-50%)
  (set! layer3 (car (gimp-layer-copy layer0 1)))
  (gimp-image-add-layer myImage layer3 -1)
  (gimp-levels layer3 HISTOGRAM-VALUE 100 255 1.00 0 255)
  (gimp-layer-set-mode layer3 DARKEN-ONLY-MODE)
  (gimp-layer-set-opacity layer3 40)
  (gimp-drawable-set-name layer3 "Top Layer")

  ; Unisco tutti i layer visibili
  (set! layer4 (car (gimp-image-flatten myImage)))
  (gimp-drawable-set-name layer4 "Last Layer")

  ;Do the dodge layer first
  (my-layer-dodge-burn myImage layer4 1 thinAmount thickAmount)

  ;Do the burn layer
  (my-layer-dodge-burn myImage layer4 0 thinAmount thickAmount)

  ; Modifico la saturazione/luminanza
  (gimp-hue-saturation layer4 ALL-HUES 0 0 50)

 )

 ;Finish the undo group for the process
 (gimp-image-undo-group-end myImage)

 ; Assicuro l'aggiornamento e la nuova visualizzazione
 (gimp-displays-flush)
)

; Registrazione dello script
(script-fu-register "script-fu-SebaX75-HDR"
 _"<Image>/Script-Fu/SebaX75/HDR..."
 "Applica gli effetti per ottenere un HDR"
 "SebaX75"
 "SebaX75"
 "April 29 2010"
 "*"
 SF-IMAGE "Image"     0
 SF-DRAWABLE     "Drawable"  0
 SF-ADJUSTMENT   _"Thin amount"  '(10 0 1000 1 1 0 1)
 SF-ADJUSTMENT   _"Thick amount" '(25 0 10000 1 1 0 1)
)

Buon HDR a tutti ed alla prossima.

Nessun commento:

Posta un commento