Skip to content


Gimp Crop Script

Look at this photo.

Fat Girl?

Fat Girl?

It seems a little fat? In my previouse post, I had introduced the resize script. The method resize script used was a direct resize. If the ratio of the original file is not same as the target file, the file will get into distortion.  I write a script can automatically crop the image to the target ratio. The new result is here.

Croped Image

Cropped Image

This script crop the top and bottom part of the image, so the result image can fit my favorite ratio, compare the images below.

Not Cropped

Not Cropped

Not Cropped

Cropped

The girl become slim again.

Now let’s see the gimp script rs-center-crop.scm:

(define (script-fu-rs-center-crop filename outfilename width height)
 
  (let* ((image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
          (drawable (car (gimp-image-get-active-layer image))))
          (let* ((original-width (car (gimp-image-width image)))
               (original-height (car (gimp-image-height image)))
               (new-width original-width)
               (new-height original-height)
               (offset-x 0)
               (offset-y 0))
 
               (if (<= (/ original-width original-height) (/ width height))
                   (gimp-image-crop image original-width (* original-width (/ height width)) 0 (/ (- original-height (* original-width (/ height width))) 2) )
                   (gimp-image-crop image (* original-height (/ width height)) original-height (/ (- original-width (* original-height (/ width height))) 2) 0)
               )
           )
 
  (set! drawable (car (gimp-image-get-active-layer image)))
 
(gimp-file-save RUN-NONINTERACTIVE image drawable outfilename outfilename)
     (gimp-image-delete image)))

Copy the rs-center-crop.scm to “D:\Program Files\GIMP-2.0\share\gimp\2.0\scripts”(If your GIMP installed at D:\Program Files\GIMP-2.0 ). Now we can write a bat rs-center-crop.bat  to run this script. 

set str=%1
set str=%str:\=/%
set str2=%2
set str2=%str2:\=/%
"D:\Program Files\GIMP-2.0\bin\gimp-2.6.exe" -i --verbose -b "(script-fu-rs-center-crop \"%str%\" \"%str2%\" 200 120)" -b "(gimp-quit 0)"

In the last line, “200″ and “100″ is the target ratio. Because my file will resize to 200*120, so I set the ratio as 200 and 120. At last, we add it into the context menu, we write the reg file rs-center-crop.reg.

Windows Registry Editor Version 5.00
 
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\*\shell\RSGimpCrop]
@="Crop as 200*120"
 
[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\*\shell\RSGimpCrop\command]
@="F:\\rocksun\\gimp-script\\rs-center-crop\\rs-center-crop.bat \"%1\" \"%1\""

All files you can download or checkout with subversion from http://gimp-script.googlecode.com/svn/trunk/rs-center-crop/ . I create this project to collect and produce gimp script, you can join it.

Related posts:

  1. Watermark: With GIMP Script
  2. Resize Image In Context Menu
  3. RockSun的script-fu教程
  4. FTP image with Conext Menu
  5. 自己动手简化图片处理

Posted in blog, recommend. Tagged with , , , , .

6 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. I’m a Gimp beginner with little to no programming experience–before I dig in too far, I’d like to know if it is feasible

    What I would like to do is crop to the selection, resize to 800 pixels wide while maintaining aspect ratio, save as a .jpeg with default settings and close the image, with as few clicks or commands as possible. Does this sound feasible without the equivalent of learning C?

  2. @Sevesteen

    The GIMP Script is not C, but scheme – a more simple language.

    I think it is easy. You can change my script to accomplice your requirements.

  3. David said

    hi rocksum like the crop here a fue mods:-
    rem rs-center-crop.bat
    @echo off
    set str=%1
    set str=%str:”=%
    set str=%str:\=/%
    set str2=%str%
    set x=%2
    if “%x%”==”" set x=7
    set y=%3
    if “%y%”==”" set y=5
    echo Input file %str% Outputfile %str2% X %x% Y %y%
    rem pause
    @echo on
    “c:\Program Files\GIMP-2.0\bin\gimp-console-2.6.exe” –verbose -b

    “(script-fu-rs-center-crop \”%str%\” \”%str2%\” %x% %y%)” -b “(gimp-quit 0)

  4. David said

    And more:-
    Windows Registry Editor Version 5.00

    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\*\shell\RSGimpCrop]
    @=”Crop as 7*5″

    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\*\shell\RSGimpCrop\command]
    @=”c:\\dr\\gimp\\script\\rs-center-crop.bat \”%1\” 7 5″

    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\*\shell\RSGimpCrop64]
    @=”Crop as 6*4″

    [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\*\shell\RSGimpCrop64\command]
    @=”c:\\dr\\gimp\\script\\rs-center-crop.bat \”%1\” 6 4″

  5. David said

    And a new bat file to do a dir:-
    rem rs-center-crop-dir.bat
    del /q c:\tempxx.bat
    for /r C:\dr\temp %%V in (*.jpg) do (echo call C:\dr\gimp\script\rs-center-crop.bat

    “%%V”>>c:\tempxx.bat)
    type c:\tempxx.bat
    pause
    c:\tempxx.bat

  6. Eleazar said

    Hey, just wanted to say thanks for a great resource!
    Helped me out a lot.

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.