Bienvenido a On Disseny Web ... x
2 octubre 2015
02 Oct 2015

Script GIMP compresión y optimización de las imágenes de una carpeta

El siguiente script sirve para poder comprimir y optimizar todas las imágenes de una carpeta usando el lenguaje de scripting que incorpora Gimp (script-fu)

~$ cat .gimp-2.8/scripts/gimp_batch_script_fu_file.scm 
(define (gimp_batch_script_fu_file filename
				fileext		; extensió 'jpeg', 'jpg', 'png'
				max-width 	; image max-width
				max-height 	; image max-height
				quality		; Jpeg quality / compression 0 .. 1  (png : compressio 0..9)
				smooth		; Smoothness 0 .. 1 		(png : color fons 0..1)
				opt		; optimized 0 - false | 1 - true 	(png : desar gamma 0..1)
				prog		; pregressive 0 - false | 1 - true	 (png : desar desplaçament 0..1)
				ss		; subsampling modes, discard color information 0 - lower quality | ... |  3 - higher quality (no subsampling)	(png : desar resolució 0..1)
				bl		; Force baseline, jpeg readable with all decoders 0 - no, higher quality | 1 - yes, lower quality  	(png : desar data 0..1)
				rm		; Restart markers, useful when data corruption can occur  0 - no restart  .... 				(png : desar comentari 0..1)
				dct		; DCT algorithm	0 - Fast Integer (not recommended) | 1 -  Integer (standard)  | 2 - Floating (slightly better) (png : desar pix. trans 0..1)
				comment)
;
; copy script gimp folder  .gimp-2.8/scripts/
; run script  batch
; $ gimp -i -b '(gimp_batch_script_fu "*.jpg" "jpg" 1100 800 0.80 0 1 1 0 1 0 2 "exportacio batch imatges qualitat web corrales pianos")' -b '(gimp-quit 0)'
; $ gimp -i -b '(gimp_batch_script_fu "*.png" "png" 1100 800 9 0 0 0 1 0 0 1 "exportacio batch imatges qualitat web corrales pianos")' -b '(gimp-quit 0)'  

; #!/bin/bash
; # script folder
; # $/optimize_images_to_web.sh /home/alex/wordpress/espaisandco.dev/wp-content/uploads/ "missatge qualsevol"

; for f in $(find $1 -type f -name '*.jpg') ;
; do
;   echo "Processing $f file..."
; 	gimp -i -b "(gimp_batch_script_fu_file \"$f\" "jpg" 1100 800 0.80 0 1 1 0 1 0 2 \"exportacio batch imatges qualitat web corrales pianos\")" -b "(gimp-quit 0)" 	
; done

	        (let* (
	                (image (car (gimp-file-load RUN-NONINTERACTIVE
                                              filename filename)))
			(w (car (gimp-image-width image)))	
			(h (car (gimp-image-height image)))

	                (drawable (car (gimp-image-get-active-layer image))))  ; end let*
			(gimp-message (string-append "Processing image: " filename ))
			(gimp-message (string-append "Width is: " (number->string w)))
			(gimp-message (string-append "Height is: " (number->string h)))

			; scale images greater than max-height
			(if (< max-height h)
				(begin
					;? = w * max-height / h			
					(set! w (* w (/ max-height h)))
					(set! h max-height)
					(gimp-message (string-append "New width is: " (number->string w)))
					(gimp-message (string-append "height is: " (number->string h)))
					; input image + new-width + new-height
					(gimp-image-scale image w max-height)
				)
				(gimp-message (string-append "Else nothing --> image height < " (number->string max-height)))
			)

			; scale images greater than max-width
			(if (< max-width w)
				(begin
					;? = h * max-width / w			
					(set! h (* h (/ max-width w)))
					(set! w max-width)
					(gimp-message (string-append "New height is: " (number->string h)))
					(gimp-message (string-append "width is: " (number->string w)))
					; input image + new-width + new-height
					(gimp-image-scale image max-width h)
				)
				(gimp-message (string-append "Else nothing --> image width < " (number->string max-width)))
			)
				;(gimp-message (string-append "params"  filename filename  ))
				;smooth quality opt prog comment ss bl rm dct
			(if (equal?  fileext "png")
; mode + input image + objecte per desar + nom + nom raw? +  interlace + compression + bkgd (desar color fons) + (desar) gama  + offs + phys + time  + comment + svtrans
				(begin
				(file-png-save2 RUN-NONINTERACTIVE image drawable filename filename 0 quality smooth opt prog 1 ss bl rm) 
				)
			)
			;(else)
			(if (equal?  fileext "jpg")
; mode + input image + objecte per desar + nom + nom raw? +  quality + suavitzat + optimizat + progressiu + comentari + subsampling + baseline + restart markers + dct algorithm 
				(begin
				(file-jpeg-save RUN-NONINTERACTIVE image drawable filename filename quality smooth opt prog comment ss bl rm dct) 
				)
			)
			(gimp-image-delete image)
		)
		
)

El script se ubica el la carpeta de usuario de Gimp dentro del directorio “scripts”, por ejemplo /home/alex/.gimp-2.8/scripts/

Se puede usar un senzillo shell-script para pasarle los ficheros del directorio

$ cat  optimize_images_to_web.sh 
#!/bin/bash
# script folder
# $/optimize_images_to_web.sh /folder/  "descripcio exportacio imatges"

for f in $(find $1 -type f -name '*.jpg') ;
do
  echo "Processing $f file..."
	gimp -i -b "(gimp_batch_script_fu_file \"$f\" \"jpg\" 1980 1480 0.80 0 1 1 0 1 0 2 \"$2\")" -b "(gimp-quit 0)" 	
  # take action on each file. $f store current file name
  #cat $f
done

Fácilmente se puede modificar para adaptarlo a imágenes ‘png’ o modificar el tamaño máximo de las imágenes 1100×800

Se puede ejecutar de la siguiente manera

$ ./optimize_images_to_web.sh /../folder/  "imagenes web"
Etiquetas:, ,
0 respuestas

Deje una Respuesta

Quiere unirse a la conversación?
Siéntese libre para participar!

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *