You are here

Need help with resizing canvas and saving

I'm trying to automate removing a border on the sides of images.

Here's what I have:

(define (resize-canvas pattern)
  (let* ((filelist (cadr (file-glob pattern 1))))
    (while (not (null? filelist))
      (let* ((filename (car filelist))
              (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
              (drawable (car (gimp-image-get-active-layer image))))
            (gimp-image-resize image 1276 720 -2 0)
            (gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
            (gimp-image-delete image))
    (set! filelist (cdr filelist)))))

The script seems to just save the image without any resize changes. It saves as expected when using image-scale. I'm guessing I'm not using drawable with image-resize correctly, but I'm not sure what the correct way is.

Are you intending to take 2 pixels off the left hand edge? because that is what your code will do. You are also presuming that all your images are 1278 pixels wide?

I'd be tempted to do this sort of thing:

  (let* ( 
          (offx -2)
          (new-width (+ (car (gimp-image-width image)) offx) )
          (image-height (car (gimp-image-height image)))
        (gimp-image-resize image new-width image-height offx 0)     

All of the images are 1280x720. I intend to take off 2 pixels off the left and right sides that's why the new width is set to 1276. My use case for this script will always be 1280x720, but thanks for the suggestion I'll do something similar when I need it.

Any idea why it isn't saving the changes?

When you use 'gimp-file-save' for either PNG or JPEG files, the image canvas size is effectively ignored. What basically happens is a Merge Visible Layers is performed using the EXPAND-AS-NECESSARY option. So, while you've shrunken your canvas using 'gimp-image-resize', you've not changed the size of the layer; and when you go to save it, it gets expanded back to its original size.

Instead of using 'gimp-image-resize', use the following (note the change in sign of the offset):

(gimp-image-crop image 1276 720 2 0)

Thanks! Yes, I was using PNG. Really appreciate the explanation, it's exactly what I needed.

I don't know why I didn't consider using crop sooner.

Subscribe to Comments for "Need help with resizing canvas and saving"