GIMP/Scripts/ScalePage

From DPWiki
< GIMP‎ | Scripts
Jump to navigation Jump to search
  • Script names:
    • dp-scale-all-fixed
    • dp-scale-all-percent
    • dp-scale-image-fixed
    • dp-scale-image-percent
  • Author: Katt83
  • Summary: For all open images or just the current one:
    1. Scale the longest dimension:
      • to size specified (in pixels)
      • to specified percentage (50 means 50%, ...)
    2. Set new resolution (if smaller)
    3. Run unsharp-mask (with specified radius, amount, and threshold)
    4. MANUAL STEP: Lessen unsharp-mask using Edit/Fade Unsharp Mask. I usually decrease to 20 or 30%. (Do immediately after running script)
      • ( Anyone knows how to script this, please let me know! )
(define (dp-scale-all-fixed  max_size
                             new_res
                             radius sharp_amount threshold)
	(let* (
		(image-num  (car (gimp-image-list)) )
		(image-ids  (cadr (gimp-image-list)) )
		(counter 0)
	      )
		(while (< counter image-num)
			(let* (
          ; Reference into image-ids array
				(image (aref image-ids counter))
        (drawable (car (gimp-image-get-active-drawable image)))
			      )
        (dp-scale-image-fixed image drawable
                             max_size
                             new_res
                             radius sharp_amount threshold)
				(set! counter (+ counter 1))
        (print (string-append "All=" (number->string counter) " out of " (number->string image-num)))
			)
		)
	)
)

(define (dp-scale-all-percent percent
                             new_res
                             radius sharp_amount threshold)
	(let* (
		(image-num  (car (gimp-image-list)) )
		(image-ids  (cadr (gimp-image-list)) )
		(counter 0)
	      )
		(while (< counter image-num)
			(let* (
          ; Reference into image-ids array
				(image (aref image-ids counter))
        (drawable (car (gimp-image-get-active-drawable image)))
			      )
        (dp-scale-image-percent image drawable
                             percent
                             new_res
                             radius sharp_amount threshold)
				(set! counter (+ counter 1))
        (print (string-append "All=" (number->string counter) " out of " (number->string image-num)))
			)
		)
	)
)

; Scale image to a fixed size
(define (dp-scale-image-fixed image
                             drawable
                             max_size
                             new_res
                             radius sharp_amount threshold)
    (let* ( (old_width (car (gimp-image-width image)))
            (old_height (car (gimp-image-height image)))
            (new_size (dp-calc-new-size old_width old_height max_size))
            (new_width (car new_size))
            (new_height (cadr new_size))
        )

        (dp-scale-image image drawable new_width new_height
                        new_res radius sharp_amount threshold)
    )
)

; Scale image to a percentage of the current size
(define (dp-scale-image-percent image
                             drawable
                             percent
                             new_res
                             radius sharp_amount threshold)
    (let* ( (old_width (car (gimp-image-width image)))
            (old_height (car (gimp-image-height image)))
            (new_size (dp-calc-new-size-percent old_width old_height percent))
            (new_width (car new_size))
            (new_height (cadr new_size))
        )

        (dp-scale-image image drawable new_width new_height
                        new_res radius sharp_amount threshold)
    )
)

;;;; Does all the work!
(define (dp-scale-image image drawable
                             new_width new_height
                             new_res
                             radius sharp_amount threshold)
    (let* ( (old_res (car (gimp-image-get-resolution image)))
            (old_width (car (gimp-image-width image)))
            (old_height (car (gimp-image-height image)))
            (new_size (list old_width old_height))
            (active_layer (gimp-image-get-active-layer image))
            (new_layer 0)
        )

        (gimp-selection-none image)
        (if (> (+ new_width new_height) 0)
            (gimp-image-scale-full image new_width new_height INTERPOLATION-CUBIC)
        )
        (if (< new_res old_res)
            (gimp-image-set-resolution image new_res new_res)
        )
        (plug-in-unsharp-mask RUN-NONINTERACTIVE image drawable
                               radius
                               sharp_amount
                               threshold)
    )
;    (gimp-message "Don't forget to Fade Unsharp Mask!!")
    (gimp-displays-flush)
)

; Returns new dimensions as list based on ratio of old sizes
(define (dp-calc-new-size-percent old_x old_y percent)
  (if (> old_x old_y)
    (dp-calc-ratio old_x old_y (round (* old_x (/ percent 100))) 0)
    (dp-calc-ratio old_x old_y 0 (round (* old_y (/ percent 100))))
  )
)

; Returns new dimensions as list based on ratio of old sizes
(define (dp-calc-new-size old_x old_y new_dim)
  (if (> old_x old_y)
    (dp-calc-ratio old_x old_y new_dim 0)
    (dp-calc-ratio old_x old_y 0 new_dim)
  )
)

; Returns missing new size based on ratio of old sizes
(define (dp-calc-ratio old_x old_y new_x new_y)
    (if (= new_x 0)
        ; newx/newy = oldx/oldy
        ; therefore, newx = (oldx/oldy) * newy
        ; result is (new_x, new_y)
        (list (round (* new_y (/ old_x old_y))) new_y)

        ; else
        (reverse (dp-calc-ratio old_y old_x new_y new_x))
    )
)

 (script-fu-register
    "dp-scale-all-fixed"
    "Scale All Images (_Fixed)..."
    "Scales and unsharp-masks all open images. Edit/Fade Unsharp Mask on each image immediately afterwards!"
    "Katt"
    "Katt83"
    "08/01/2010"
    ""                ;accepts any image type
    SF-VALUE       "Max Size:"        "500"
    SF-VALUE       "Max Resolution:"   "144"
    SF-VALUE       "Radius:"       "1.5"
    SF-VALUE       "Sharp Amt:"    "0.75"
    SF-VALUE       "Threshold:"    "8"
)

(script-fu-menu-register "dp-scale-all-fixed"
                         "<Image>/DP")

 (script-fu-register
    "dp-scale-all-percent"
    "Scale All Images _(Percent)..."
    "Scales and unsharp-masks all open images. Edit/Fade Unsharp Mask on each image immediately afterwards!"
    "Katt"
    "Katt83"
    "08/01/2010"
    ""                ;accepts any image type
    SF-ADJUSTMENT  "Percentage Scaling:"   '(30 0 100 1 10 0 0)
    SF-VALUE       "Max Resolution:"   "144"
    SF-VALUE       "Radius:"       "1.5"
    SF-VALUE       "Sharp Amt:"    "0.75"
    SF-VALUE       "Threshold:"    "8"
)

(script-fu-menu-register "dp-scale-all-percent"
                         "<Image>/DP")

 (script-fu-register
    "dp-scale-image"
    "_Scale Image..."
    "Scales and unsharp-masks the image. Edit/Fade Unsharp Mask immediately afterwards!"
    "Katt"
    "Katt83"
    "03/10/2009"
    ""                ;accepts any image type
    SF-IMAGE "Image" 0
    SF-DRAWABLE "Drawable" 0
    SF-VALUE       "Width:"        "0"
    SF-VALUE       "Height:"       "300"
    SF-VALUE       "Max Resolution:"   "144"
    SF-VALUE       "Radius:"       "1.5"
    SF-VALUE       "Sharp Amt:"    "0.75"
    SF-VALUE       "Threshold:"    "8"
)

 (script-fu-register
    "dp-scale-image-fixed"
    "_Scale Image (Fixed)"
    "Scales and unsharp-masks the image. Edit/Fade Unsharp Mask immediately afterwards!"
    "Katt"
    "Katt83"
    "03/10/2009"
    ""                ;accepts any image type
    SF-IMAGE "Image" 0
    SF-DRAWABLE "Drawable" 0
    SF-VALUE       "Max Size:"     "500"
    SF-VALUE       "Max Resolution:"   "144"
    SF-VALUE       "Radius:"       "1.5"
    SF-VALUE       "Sharp Amt:"    "0.75"
    SF-VALUE       "Threshold:"    "8"
)

(script-fu-menu-register "dp-scale-image-fixed"
                         "<Image>/DP")

(script-fu-register
    "dp-scale-image-percent"
    "_Scale Image (Percent)..."
    "Scales and unsharp-masks the image. Edit/Fade Unsharp Mask immediately afterwards!"
    "Katt"
    "Katt83"
    "03/10/2009"
    ""                ;accepts any image type
    SF-IMAGE "Image" 0
    SF-DRAWABLE "Drawable" 0
    SF-ADJUSTMENT  "Percentage:"   '(30 0 100 1 10 0 0)
    SF-VALUE       "Max Resolution:"   "144"
    SF-VALUE       "Radius:"       "1.5"
    SF-VALUE       "Sharp Amt:"    "0.75"
    SF-VALUE       "Threshold:"    "8"
)

(script-fu-menu-register "dp-scale-image-percent"
                         "<Image>/DP")