You are here

gimp-file-save problem

I'm trying to write to script to automate setting up my library of background images for my new dual-monitor setup.
I've got it mostly done, but I can't seem to get gimp-file-save & gimp-image-flatten to play. I can save if I don't flatten the image, but if I flatten the image, it complains that I'm not handing it the right type of object for the layer.
Here's the code:

; Background-resize
; This will scale & properly center an image so that it presents a good background with the monitor/monitor or monitor/TV setup.
; Side by side monitors are assumed.

(define (scale-resize-background sourceImg
background
leftWidth
leftHeight
rightWidth
rightHeight
justification
)
(let*
(
;Declare some values used along the way
(widthOffset 0)
(heightOffset 0)
(scaleFactor 1)
(leftJustificationOffset 0)
(rightJustificationOffset 0)
(justificationOffset 0)
(layer)

;Calculate total width & height
(totalWidth
(+ leftWidth rightWidth)
)
(totalHeight
(if (> leftHeight rightHeight)
leftHeight
rightHeight
)
)

;Create a new Image
(theImage (car
(gimp-image-new
totalWidth
totalHeight
RGB
)
)
)

(theLayer (car
(gimp-layer-new
theImage
totalWidth
totalHeight
RGB-IMAGE
"theLayer"
100
NORMAL
)
)
)

;Load the source image
(sourceImageLayer (car
(gimp-file-load-layer
0
theImage
sourceImg
)
)
)

;Copy the source image layer, so we can manipulate it independently for each monitor

(sourceImageLayer2 (car
(gimp-layer-copy
sourceImageLayer
FALSE
)
)
)

;Read the size of the source image
(sourceWidth (car
(gimp-drawable-width
sourceImageLayer
)
)
)

(sourceHeight (car
(gimp-drawable-height
sourceImageLayer
)
)
)
;At this point we're done initializing things
)

;Calculate an additional height offset necessary due to
;justification.
(if (= 1 justification)
(set! justificationOffset
(/
(- leftHeight rightHeight)
2)
)
)
(if (= 2 justification)
(set! justificationOffset
(- leftHeight rightHeight)
)
)
(if (= 0 justification)
(set! justificationOffset 0)
)

;Figure out which of the monitors needs to be justified
(if (> leftHeight rightHeight)
(set! rightJustificationOffset justificationOffset)
(set! leftJustificationOffset justificationOffset)
)

;Create a background layer, and fill it with the selected color.
(gimp-image-add-layer theImage theLayer 0)

(gimp-context-set-background background)

(gimp-drawable-fill theLayer BACKGROUND-FILL)

;Insert the source image for the first time
(gimp-image-add-layer
theImage
sourceImageLayer
0
)

;Figure necessary resizing & offsetting for left Monitor
;Find the desired scale factor (source will be multiplied by this)
;The image will be scaled down to fit, but never up, the image is scaled to fit in both height in width.
(if (> sourceHeight leftHeight)
(set! scaleFactor (/ leftHeight sourceHeight)
)
)

(if (> sourceWidth leftWidth)
(if (> scaleFactor
(/ leftWidth sourceWidth)
)
(set! scalefactor (/ leftWidth sourceWidth)
)
)
)

;scale the image, if scaling is needed
(if (> 1 scaleFactor)
(gimp-layer-scale-full
sourceImageLayer
(* sourceWidth scaleFactor)
(* sourceHeight scaleFactor)
TRUE
2
)
)

;figure the needed offset to center it on the monitor
(set! widthOffset (/
(- leftWidth
(* sourceWidth scaleFactor)
)
2)
)

(set! heightOffset (-
(/
(- leftHeight
(* sourceHeight scaleFactor)
)
2)
leftJustificationOffset)
)

;offset the image
(gimp-layer-set-offsets
sourceImageLayer
widthOffset
heightOffset
)

;do it again, for the other monitor

;Insert the source image for the second time
(gimp-image-add-layer
theImage
sourceImageLayer2
0
)

;Figure necessary resizing & offsetting for left Monitor
;Find the desired scale factor (source will be multiplied by this)
;The image will be scaled down to fit, but never up, the image is scaled to fit in both height in width.
(if (> sourceHeight rightHeight)
(set! scaleFactor (/ rightHeight sourceHeight)
)
)

(if (> sourceWidth rightWidth)
(if (> scaleFactor
(/ rightWidth sourceWidth)
)
(set! scalefactor (/ rightWidth sourceWidth)
)
)
)

;scale the image, if scaling is needed
(if (> 1 scaleFactor)
(gimp-layer-scale-full
sourceImageLayer2
(* sourceWidth scaleFactor)
(* sourceHeight scaleFactor)
TRUE
2
)
)

;figure the needed offset to center it on the monitor
(set! widthOffset (+
(/
(- rightWidth
(* sourceWidth scaleFactor)
)
2)
leftWidth)
)

(set! heightOffset (-
(/
(- rightHeight
(* sourceHeight scaleFactor)
)
2)
rightJustificationOffset)
)

;offset the image
(gimp-layer-set-offsets
sourceImageLayer2
widthOffset
heightOffset
)

;merge the layers
(gimp-image-flatten
theImage
)

;get the ID of the new layer
(set! layer car(
(gimp-image-get-active-drawable
theImage
)
)
)

;save the file
(gimp-file-save
1
theImage
layer
"background.jpg"
"background.jpg"
)

;display current
(gimp-display-new theImage)

)
)

(script-fu-register "scale-resize-background"
"Background-fix"
"Size an image so it shows correctly in 2 side by side monitors\
This procedure is intended for monitors stitched with\
twinview or xinerama"
"D3snoopy"
"D3snoopy"
"2009"
"RGB"
SF-FILENAME "Source Image" "File"
SF-COLOR "Background Color" '(0 0 0)
SF-VALUE "Left Screen Width" "1680"
SF-VALUE "Left Screen Height" "1050"
SF-VALUE "Right Screen Width" "1920"
SF-VALUE "Right Screen Height" "1080"
SF-OPTION "Justification (of monitors)" '("Top" "Center" "Bottom")
)

(script-fu-menu-register "scale-resize-background"
"/Filters/Background")

I can't figure out what I'm doing wrong (BTW, I tried catching the return value of gimp-image-flatten, and that didn't work either)

Can anyone help me? Or, should I rewrite it in python?

I got it to work by using gimp-image-get-active-layer.

I never did manage to figure out why the other operations that are supposed to return layers weren't doing so.

Also, I never figured out why gimp-image-get-active-drawable wasn't returning a layer, since I wasn't doing anything with channels along the way.

I succeeded with the second phase of my endeavor, as well: to write a shell script that would randomly pick an image file from a directory to send to the gimp operation.

Subscribe to Comments for "gimp-file-save problem"