Post Reply 

Resize image batch with a minimum of 10px transparency border

Sep 27, 2011, 22:31
Post: #1
Resize image batch with a minimum of 10px transparency border
I'm writing a script to take all of the images in a folder and resize them so that the longest side is 290 pixels. Then it places this image onto a new transparent image that is 300x300 and saves it as a png.

At least that's what it's supposed to do. So far, all it does is the first part of resizing the image so that its longest length is 290 px. I don't get any errors when I run the script so I can't figure out why it's not working. Can anyone see what I've done wrong here?



(define (batch-resize pattern)
(let*
(
;Create a 300x300 image with a transparent background

(filelist (cadr (file-glob pattern 1)))
(finalImageWidth 300)
(finalImageHeight 300)
(finalImage)
(finalImage
(car
(gimp-image-new
finalImageWidth
finalImageHeight
RGBA
)
)
)
(layer
(car (gimp-layer-new theImage finalImageWidth finalImageHeight RGBA "layer 1" 0 NORMAL))
)


)

(while (not (null? filelist))

;Resize all of the image files in the folder and add them as a layer to the 300x300 tranparent image
;with 10 pixels of padding on the longest side to show the transparency underneath

(let*
(
(filename (car filelist))
(image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
(drawable1 (car (gimp-image-get-active-layer image)))
(theWidth (car (gimp-drawable-width drawable1)))
(theHeight (car (gimp-drawable-height drawable1)))
)

(if (> theHeight theWidth)
(gimp-image-scale-full image (+ theWidth (- 290 theHeight)) 290 INTERPOLATION-CUBIC)
(gimp-image-scale-full image 290 (+ theHeight (- 290 theWidth)) INTERPOLATION-CUBIC)

)
(let*
(
(new-layer (car (gimp-image-get-layers image)))
(gimp-image-add-layer theImage new-layer -1)
(drawable2 (car (gimp-image-flatten theImage)))
)


(file-png-save2 RUN-NONINTERACTIVE theImage drawable2 new.png new.png 0 0 0 0 0 0 0 0 1)
(gimp-image-delete image)
)
)
(set! filelist (cdr filelist))
)
)
)
Find all posts by this user
Quote this message in a reply
Sep 27, 2011, 23:10
Post: #2
RE: Resize image batch with a minimum of 10px transparency border
Why don't you look at ImageMagick for that. You would of course have to figure out a handful of parameters, but that would still be a lot simpler (and likely execute much faster) than using Gimp.

Now answering your questions on
https://Gimp-Forum.net
Visit this user's website Find all posts by this user
Quote this message in a reply
Sep 28, 2011, 06:27 (This post was last modified: Sep 28, 2011 06:28 by paynekj.)
Post: #3
RE: Resize image batch with a minimum of 10px transparency border
I think your problem is that you're not creating a new transparent layer:

I think that the following line is where you think you should be creating the new layer
Code:
(new-layer (car (gimp-image-get-layers image)))
But I think you need a call to gimp-layer-new instead:
Code:
(new-layer (car (gimp-layer-new image finalImageWidth finalImageHeight RGBA-IMAGE "New Layer" 0 NORMAL-MODE)))
Find all posts by this user
Quote this message in a reply
Sep 28, 2011, 17:59
Post: #4
RE: Resize image batch with a minimum of 10px transparency border
(Sep 28, 2011 06:27)paynekj Wrote:  I think your problem is that you're not creating a new transparent layer:

I think that the following line is where you think you should be creating the new layer
Code:
(new-layer (car (gimp-image-get-layers image)))
But I think you need a call to gimp-layer-new instead:
Code:
(new-layer (car (gimp-layer-new image finalImageWidth finalImageHeight RGBA-IMAGE "New Layer" 0 NORMAL-MODE)))

I switched out that line as you suggested but it didn't change anything. I noticed that where I was saving the file (file-png-save2), I was saving the drawable from the wrong image. But even after fixing THAT my result was the same (just resizing the original image to 290 px. I came to the conclusion that my script would have to be picked apart piece by piece to figure out what is wrong so I went with the previous suggestion to use ImageMagick. But thank you for your help anyway.
Find all posts by this user
Quote this message in a reply
Sep 28, 2011, 18:02 (This post was last modified: Sep 28, 2011 18:03 by Bad Pie.)
Post: #5
RE: Resize image batch with a minimum of 10px transparency border
Replied to wrong comment. Guess I can't delete...
Find all posts by this user
Quote this message in a reply
Sep 28, 2011, 18:03
Post: #6
RE: Resize image batch with a minimum of 10px transparency border
(Sep 27, 2011 23:10)ofnuts Wrote:  Why don't you look at ImageMagick for that. You would of course have to figure out a handful of parameters, but that would still be a lot simpler (and likely execute much faster) than using Gimp.

60 lines of code that didn't work to this using imagemagick:
mogrify -path formatted -format png images/*.jpg -resize 290x290 -background transparent -gravity center -extent 300x300 images/*.jpg

Works like a charm. Thanks for the suggestion.
Find all posts by this user
Quote this message in a reply
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Scale image to specific size MattRothschild 1 117 Sep 15, 2017 11:13
Last Post: ythgilb
  Dividing and printing a large image RayArdia 1 103 Aug 9, 2017 07:22
Last Post: ythgilb
  Late binding / COM for opening an image into GIMP? i73 2 129 Aug 4, 2017 17:39
Last Post: i73
  Batch create images from array edmilner 1 204 Jul 17, 2017 16:41
Last Post: ythgilb
  How to close gimp with batch command? flixe 1 216 Jun 19, 2017 12:29
Last Post: ythgilb

Forum Jump:


GIMP ForumPortalArchiveContactTermsRSS