lunedì 28 giugno 2010

GIMP Scripting - Fade and Shadow

Siccome qualcuno potrebbe pensare che questo blog sia stato abbandonato, provvedo a pubblicare qualcosa di nuovo che non è molto impegnativo, ma può fare molto comodo ed essere interessante.
Non pensate che siano stati abbandonati gli argomenti da sistemista, ma ho accumulato solamente un deciso ritardo perchè le tante bozze presenti sono da revisionare e correggere, alcune anche da terminare.
Visto che non mi piace farmi mancare "oggetti" scaturiti dalla mia mente e non perdo mai l'abitudine a cercare di arrivare dove altri si sentono a casa loro, mi sono dedicato ad un po' di sano scripting per GIMP, ovvero gli script-foo; questa tipologia di script serve per automatizzare operazioni ricorsive sulle immagini semplificando di molto la vita, ed aumentando moltissimo la velocità, se si devono applicare sempre gli stessi effetti.
Il primo e più semplice script che ho creato serve per effettuare un effetto fading e shadowing ad un'immagine, ovvero sfumare l'immagine ai bordi, come se fosse un rettangolo arrotondato, ed applicargli l'effetto ombra.

Ecco il codice:
; Script to make fading and shadowing of an image
;
; The script is located in "<Image> / Script-Fu / SebaX75 / Fade & Shadow..."
;
; Last changed: 25 Aprile 2010
;
; Copyright (C) 2010 SebaX75 <sebax75 AT yahoo DOT it>
;
; --------------------------------------------------------------------
; 
; Changelog:
;  Version 0.1
;    - First release
;
; --------------------------------------------------------------------
;
; 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.
;

(define (script-fu-SebaX75-fade-shadow      myImage
     layer0
     offset_x
     offset_y
     arrotondamento
     riduzione
     sfumatura
     coloreOmbra
 )
;       Start an undo group so the process can be undone with one undo
 (gimp-image-undo-group-start myImage)

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

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

 ; Inizializzo le variabili da utilizzare
 (let* (
  (image-width (gimp-drawable-width layer0))
  (image-height (gimp-drawable-width layer0))
  (layer1())
  )

  ;Impostare il colore del primo piano a #444444
  (gimp-context-set-foreground coloreOmbra)
  ;Livello -> Trasparenza -> Aggiungi canale alfa
  (gimp-layer-add-alpha layer0)
  ; Seleziona -> Rettangolo arrotondato (20)
  (gimp-selection-all myImage)
  (script-fu-selection-rounded-rectangle myImage layer0 arrotondamento FALSE)
  ; Seleziona -> Riduci (10)
  (gimp-selection-shrink myImage riduzione)
  ; Seleziona -> Inverti
  (gimp-selection-invert myImage)
  ; Seleziona -> Sfumata (20)
  (gimp-selection-feather myImage sfumatura)
  ;Pressione Canc
  (gimp-edit-clear layer0)
;# Seleziona -> Niente
  ;Livello -> Nuovo livello con colore del primo piano ed in secondo piano
  (set! layer1 (car (gimp-layer-new-from-drawable layer0 myImage)))
  (gimp-image-add-layer myImage layer1 1)
  (gimp-drawable-fill layer1 FOREGROUND-FILL)
  ;Seleziona -> Rettangolo arrotondato (20)
  (gimp-selection-all myImage)
  (script-fu-selection-rounded-rectangle myImage layer1 arrotondamento FALSE)
  ;Seleziona -> Riduci (15)
  (gimp-selection-shrink myImage (+ riduzione 5))
  ;Seleziona -> Inverti
  (gimp-selection-invert myImage)
  ;#Seleziona -> Sfumata (20)
  (gimp-selection-feather myImage sfumatura)
  ;Pressione Canc
  (gimp-edit-clear layer1)
  ;Spostare di posizione il nuovo layer (10,10 pixel)
  (gimp-layer-set-offsets layer1 offset_x offset_y)
  ; Rimuove qualunque selezione
  (gimp-selection-none myImage)
  ; Unisco tutti i layer in uno unico
  (set! layer0 (car (gimp-image-flatten myImage)))
  (gimp-drawable-set-name layer0 "Ombra+Fade")
 )

 ;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-fade-shadow"
 _"<Image>/Script-Fu/SebaX75/Fade & Shadow..."
 "Applica gli effetti per ottenere l'ombra ed il fading dell'immagine"
 "SebaX75"
 "SebaX75"
 "April 21 2010"
 "*"
 SF-IMAGE "Image"     0
 SF-DRAWABLE     "Drawable"  0
 SF-ADJUSTMENT   _"Spostamento X" '(10 -4096 4096 1 10 0 1)
 SF-ADJUSTMENT   _"Spostamento Y" '(10 -4096 4096 1 10 0 1)
 SF-ADJUSTMENT   _"Arrotondamento %"     '(20 -4096 4096 1 10 0 1)
 SF-ADJUSTMENT   _"Riduzione"    '(10 0 1024 1 10 0 1)
 SF-ADJUSTMENT   _"Sfumatura"    '(20 0 1024 1 10 0 1)
 SF-COLOR _"Colore ombra" "#444444"
)

Per oggi direi che è tutto... buon divertimento con GIMP.