Gimptalk - Premier Gimp Community: Automatically crop and scale image - Gimptalk - Premier Gimp Community

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Automatically crop and scale image Script to crop and scale to small sized images with the high quality

#1 User is offline   Jasper 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 26-October 11

Posted 26 October 2011 - 11:51 AM

I found a script on the internet by Igor Birman, this is a script from 2005 so not working in Gimp2.6
I tried to change the script but i am a newbie at scripting. I managed to run the script without errors but also without result.. Please help

; Resize Stairstep

; Author:    Igor Birman
; Date:      08/03/2005
; Version:   1.0

; This script will resize an image in several steps to give the sharpest 
; possible resized image.  An image can be scaled up or down using this 
; technique.  8-12 steps is considered to be the optimum amount.

; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version.
; 
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
; 
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

(define (resize-stairstep img 
                       drawable 
                  new-width
                  number-of-steps)

  ; Start an undo group. Everything between the start and the end will 
  ; be carried out if an undo command is issued. 
  (gimp-image-undo-group-start img) 

  ; Select the entire image
  (gimp-selection-all img)

  (let* ( (selection-bounds 0) (x1 0) (y1 0) (x2 0) (y2 0) (w 0) (h 0) (step 0) (width 0) (height 0) (ratio 0) (new-width 0) )
  
  
  
  
  
  
  ; Retrieve the current dimensions of the image
  (set! selection-bounds (gimp-selection-bounds img))
  (set! x1 (cadr selection-bounds))
  (set! y1 (caddr selection-bounds))
  (set! x2 (- (cadr (cddr selection-bounds)) x1))
  (set! y2 (- (caddr (cddr selection-bounds)) y1))

  ; De-select the selection
  (gimp-selection-none img)

  ; Start a let group  
  (let* 
    (
      ; Calculate current and new width, height, ratio, and step size
      (width  (- x2 x1))
      (height (- y2 y1))
      (ratio  (/ height width))
      (step   (/ (- width new-width) number-of-steps))
      (w      width)
      (h      0)
    )
)
    ; Loop until the image is the desired size
    (while (not (= w new-width))
      (set! w (- w step))

      ; Allow for rounding, do not make it smaller than needed.
      ; If decreasing the size of the image..
      (if (< new-width width)
        (if (< w new-width)
          (set! w new-width)
        )
      )
      ; If increasing the size of the image..
      (if (> new-width width)
        (if (> w new-width)
          (set! w new-width)
        )
      )
      ; Set new height based on width times ratio 
      (set! h (* w ratio))

      ; Increase the canvas size to the new size if increasing image 
      (if (> new-width width)
        (gimp-image-resize img w h 0 0) 
      )

      ; Scale the image to the new width and height
      (gimp-drawable-transform-scale drawable 0 0 w h 0 2 0 3 0)

      ; Crop the image down to the new scaled size
      (if (< new-width width)
        (gimp-image-crop img w h 0 0) 
      )

      ; Re-display the cropped image 
      (gimp-displays-flush) 
    )

    ; Remove any existing selections 
    (gimp-selection-none img) 

    ; Stop the undo group.
    (gimp-image-undo-group-end img) 

    ; Flush the display to show changes 
    (gimp-displays-flush) 
  )
)

(script-fu-register "resize-stairstep"
            _"<Image>/Script-Fu/Misc/Resize by Steps"
            "Resize an image by steps"
            "Igor Birman"
            "Igor Birman"
            "August 3, 2005"
            ""
            SF-IMAGE    "Image"         0
            SF-DRAWABLE "Drawable"      0
            SF-VALUE    "Target Width"  "1024"
            SF-ADJUSTMENT  "Number of Steps"  '(3 2 12 1 1 1 1)
            )
			

0

#2 User is offline   ofnuts 

  • Moderator GT
  • Group: Moderators
  • Posts: 1,302
  • Joined: 17-October 10
  • LocationLooking over your shoulder :)

Posted 26 October 2011 - 02:33 PM

View PostJasper, on 26 October 2011 - 11:51 AM, said:

I found a script on the internet by Igor Birman, this is a script from 2005 so not working in Gimp2.6
I tried to change the script but i am a newbie at scripting. I managed to run the script without errors but also without result.. Please help

Before you invest too much time in this, what makes you think you'll get a significantly better result than with plain scaling using Lanczos/Sinc? Have you tried to emulate it manually on a picture and compare results? This script is 6 years old, and, incidentally, doesn't even bother to check the scaling algorithm used...
010011110110011001101110011101010111010001110011
0

#3 User is offline   Jasper 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 26-October 11

Posted 26 October 2011 - 02:47 PM

View Postofnuts, on 26 October 2011 - 02:33 PM, said:

View PostJasper, on 26 October 2011 - 11:51 AM, said:

I found a script on the internet by Igor Birman, this is a script from 2005 so not working in Gimp2.6
I tried to change the script but i am a newbie at scripting. I managed to run the script without errors but also without result.. Please help

Before you invest too much time in this, what makes you think you'll get a significantly better result than with plain scaling using Lanczos/Sinc? Have you tried to emulate it manually on a picture and compare results? This script is 6 years old, and, incidentally, doesn't even bother to check the scaling algorithm used...


First of, thanks for your input!

Perhaps my question is wrong, i'm a scripting noob, so i can't see what's good and what's not.
I'm using pictures ( a lot) for a PDA game, so i need to make my pics fit the several screens in the game.
The script i found was the one that came closest to my goal (need to resize about 200 pics)


Where i want to create a script for is the following.
- Crop image by sight
- Scale 'image (predefined width 231px)'
- Crop 'image (predefined width 231px height 152px)'
- Save 'name file should become "original file name"+"GPS"
- Crop 'image (predefined width 224px height 150px)'
- Save 'name file should become "original file name"+"MSG"
- Crop 'image (predefined width 224px height 120px'
- Save 'name file should become "original file name"+"MC"

Because the pics become so small i want to contain as much as quality as possible, pics can't be bigger because i don't want the pda to auto-scale my pics.

Hope anyone can help
0

#4 User is offline   ofnuts 

  • Moderator GT
  • Group: Moderators
  • Posts: 1,302
  • Joined: 17-October 10
  • LocationLooking over your shoulder :)

Posted 26 October 2011 - 03:18 PM

View PostJasper, on 26 October 2011 - 02:47 PM, said:

View Postofnuts, on 26 October 2011 - 02:33 PM, said:

View PostJasper, on 26 October 2011 - 11:51 AM, said:

I found a script on the internet by Igor Birman, this is a script from 2005 so not working in Gimp2.6
I tried to change the script but i am a newbie at scripting. I managed to run the script without errors but also without result.. Please help

Before you invest too much time in this, what makes you think you'll get a significantly better result than with plain scaling using Lanczos/Sinc? Have you tried to emulate it manually on a picture and compare results? This script is 6 years old, and, incidentally, doesn't even bother to check the scaling algorithm used...


First of, thanks for your input!

Perhaps my question is wrong, i'm a scripting noob, so i can't see what's good and what's not.
I'm using pictures ( a lot) for a PDA game, so i need to make my pics fit the several screens in the game.
The script i found was the one that came closest to my goal (need to resize about 200 pics)


Where i want to create a script for is the following.
- Crop image by sight
- Scale 'image (predefined width 231px)'
- Crop 'image (predefined width 231px height 152px)'
- Save 'name file should become "original file name"+"GPS"
- Crop 'image (predefined width 224px height 150px)'
- Save 'name file should become "original file name"+"MSG"
- Crop 'image (predefined width 224px height 120px'
- Save 'name file should become "original file name"+"MC"

Because the pics become so small i want to contain as much as quality as possible, pics can't be bigger because i don't want the pda to auto-scale my pics.

Hope anyone can help
Have a look at the ImageMagick tools set (www.imagemagick.org) and use a shell script around it. IIRC it can even apply a bit of Gaussian blur before and a some sharpening after the scaling down(*).

(*) Scaling down blindly has its pitfalls, you can be hit by "spatial frequency folding" and get moire patterns or jagged/aliased lines. This isn't a bug in the software, even if some algorithms are a bit more immune than others. Proper scaling down requires to low-pass filter the picture (which is applying a Gaussian filter) before the scaling down, by an equivalent amount (for instance, 3 pixels blur for a 3x scale down).
010011110110011001101110011101010111010001110011
0

#5 User is offline   Jasper 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 26-October 11

Posted 26 October 2011 - 03:28 PM

shell script? Help, i don't understand. could u give an example? Also, how can i rip code from the imagemagick tools set?

Thanks
0

#6 User is offline   rich2005 

  • Member
  • PipPip
  • Group: Members
  • Posts: 327
  • Joined: 01-December 10

Posted 26 October 2011 - 03:31 PM

This is a re-worked up-to-date version of the old script you found.

http://www.gimphelp....tair-resize.scm

In certain circumstamces it can give a better scaled image than a plain resize (but not many).

Does not of course crop the image.

Looking at your list of requirements, maybe possible with a imagemagick batch file
or
If you "cropped by sight" and scaled all 200 images, then Davids Batch processor would do a final crop + rename +save in one go for all 200. So you would need to run 3 times with the different settings/name.
http://members.ozema...odsond/dbp.html

If you wanted to do one image at a time, I did a video demo for someone who wanted a more interactive rescale for a mobile phone. Would not take too much to adapt to your requirements, some extra guides maybe and crop then save-as 3 times
http://www.youtube.c...h?v=E0qJZjGKzdE about 3 1/2 mins
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic