Chez Xavier

Home / The Gimp, scripts fu et automatisation des traitements

Excuse me, I think I'm naked: on a wall of Barcelona.

Excuse me, I think I'm naked: on a wall of Barcelona.

La création de miniatures a toujours été un sport national pour les aficionados de la photographie désireux de publier leurs créations sur Internet. Plusieurs solutions existent :
  • Redimensionner les images une par une, en profitant d'une longue après midi pluvieuse
  • Tout publier sur Flickr, et renoncer à toute présentation personnalisée, ou sinon s'ennuyer avec l'API Flickr (et y parvenir quelques jours plus tard ?)
  • Confier ce travail à un sous-fifre
  • Acheter une license de Adobe Photoshop et créer un traitement automatisé. Oui, ça fait mal au porte-monnaie, mais l'honneteté est à ce prix.
Et là , solution miracle - et gratuite - The Gimp est parmi nous ! Logiciel libre de création et retouche graphique, The Gimp est un outil très puissant, bien que malheureusement un peu austère pour les débutants. Une de ses faces cachées, la possibilité d'employer des scripts d'automatisation des traitements, est un vrai plaisir pour les amateurs de scheme (pour des explications en français, allez voir l'article sur Scheme chez Wikipédia). Le fichier gimp_resize.scm, que je vous propose ici, permet d'insérer un script de redimensionnement automatisé dans The Gimp :
;; FILE   gimp_resize.scm
;; DATE    2005/12/04 17:14:16 Xavier Lacot
;; AUTHOR Xavier Lacot    <xavier@lacot.org>
;;
;; DESCRIPTION
;; Creates thumbnails from a jpeg image with The Gimp
;; To run this from the command prompt:
;; gimp -c -d -i -b '(script-fu-thumbnails "file.jpg" tw th fw fh)' \
;;    '(gimp-quit 0)'
;;
;; LICENSE
;; Copyright (c) 2005 Xavier lacot. All rights reserved.
;;
;; This script 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 2 of the License, or
;; (at your option) any later version.
;;
;; This script 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 script; if not, write to the Free Software
;; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
;;

;; Resizes a high resolution jpeg file + thumbnail creation (filename preceeded
;; with th_"), with constraint proportions
;;
;; @param  string    filename
;; @param int   thumbnail maximal width
;; @param  int   thumbnail maximal height
;; @param int   fullsize image maximal width
;; @param int   fullsize image maximal height
(define (script-fu-thumbnails filename t-width t-height f-width f-height)
(let* (
(img (car (file-jpeg-load 1 filename filename)))
(drw 0)
(fileparts (strbreakup filename "."))
(img-width  (car (gimp-image-width img)))
(img-height (car (gimp-image-height img))))

;; set image resolution to 150dpi
(gimp-image-set-resolution img 150 150)

;; keep ratio
(set! fullsize-height img-height)
(set! fullsize-width img-width)
(if (> img-width f-width)
(begin
(set! fullsize-width f-width)
(set! fullsize-height (* f-width (/ img-height img-width)))
(set! thumbnail-width t-width)
(set! thumbnail-height (* t-width (/ img-height img-width)))
)
)
(if (> fullsize-height f-height)
(begin
(set! fullsize-height f-height)
(set! fullsize-width (* f-height (/ img-width img-height)))
(set! thumbnail-height t-height)
(set! thumbnail-width (* t-height (/ img-width img-height)))
)
)

;; create 'full-size' image ------------------------------------
(gimp-image-scale img fullsize-width fullsize-height)

;; also flatten image to reduce byte storage even further
(set! drw (car (gimp-image-flatten img)))

;; save at quality level of .90
(file-jpeg-save 1 img drw
(string-append (car fileparts) ".jpg")
(string-append (car fileparts) ".jpg")
.90 0 0 0 " " 0 1 0 1)

;; create thumbnail image --------------------------------------
;; set image resolution to 72dpi
(gimp-image-set-resolution img 72 72)

;; save at quality level of .60
(gimp-image-scale img thumbnail-width thumbnail-height)
(file-jpeg-save 1 img drw
(string-append "th_" (car fileparts) ".jpg")
(string-append "th_" (car fileparts) ".jpg")
.60 0 0 0 " " 0 1 0 1)
)
)

(script-fu-register "script-fu-miniatures"
"<Toolbox>/Xtns/Script-Fu/Utils/Image Process..."
"Process Image"
"Xavier lacot"
"Xavier Lacot"
"Dec 2005"
""
SF-VALUE "Image Name" " "
SF-VALUE "Thumbnail Width" "150"
SF-VALUE "Thumbnail Height" "150"
SF-VALUE "Full Width" "1024"
SF-VALUE "Full Height" "1024")

Fort bien !, vous exclamez-vous. Et maintenant, qu'en fais-je ? Il est possible, par exemple, d'employer ce script-fu depuis un script bash, permettant ainsi de redimensionner les photos de tout un dossier d'un seul coup. Les étapes sont les suivantes :

  • enregistrer le script-fu dans ~/.gimp/scripts
  • créer un script bash, contenant par exemple :
    #!/bin/bash
    
    trap "kill $$" SIGINT
    
    echo "This will overwrite all the jpeg files in the current directory. Do you want to proceed ? (Y/N)"
    read action
    if [ $action = "y" -o $action = "Y" ]
    then
    for myfile in *.jpg
    do
    echo "resizing "$myfile
    
    nice -n 19 gimp -c -d -i -b "(script-fu-miniatures \"$myfile\" 150 150 1024 1024)" "(gimp-quit 0)"
    echo "done."
    done
    fi
    echo
    echo "Strike key to continue..."
    read
  • se placer dans le dossier d'images à redimmensionner, et appeler simplement la commande "gimp-resize"

Évidemment, ce script peut paraitre limité par rapport aux besoins d'une publication Web : l'envoi de fichier sur un serveur, par exemple, est cruellement manquant. Mais la commande ftp, entre autres, est là pour vous servir !

  • written on: 2005-12-10