Post Reply 

Autocrop after fuzzy in a script ?

Jan 30, 2012, 17:19
Post: #1
Autocrop after fuzzy in a script ?
Hello everybody!!

I'm just trying to write a script in order to crop a png file which has a white border.
I tried this but nothing change... :/
Can somebody help me, please ?

Code:
(define (script-crop filename
            )
    (let*
        (
            (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
            (drawable (car (gimp-image-get-active-layer image)))
        )

        (gimp-fuzzy-select image 1 1 0 CHANNEL-OP-REPLACE TRUE FALSE 0 TRUE)
        
        ; Inverse la sélection
        (gimp-selection-invert image)

        ; crop the image
        (plug-in-autocrop RUN-NONINTERACTIVE image drawable)

        ; clean up the image
        (gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
        (gimp-image-delete image)
    )
)

Thanks for your help ;-)
Find all posts by this user
Quote this message in a reply
Jan 30, 2012, 17:59
Post: #2
RE: Autocrop after fuzzy in a script ?
Autocrop removes identical pixels on the perimeter of the image. So either your border is perfectly uniform, and autocrop will remove it directly (no need for select/delete) or you make the border uniform (select+delete or paint in specific color). You seem to be using the second solution, but I don't see where your code deletes/paints the border pixels. You also call fuzzy-select on the image instead of the drawable so it won't work...

But if you use a threshold of 1 you won't select much and an autocrop up front may be enough.

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
Jan 31, 2012, 07:27
Post: #3
RE: Autocrop after fuzzy in a script ?
Hi,
If I don't use a script, I use the fuzzy select in a pixel at (1;1) which is white(all my border is white), then I invert the selection and I crop it. Then, hopefully, I save my new image!

Example:

My image at start :
   

My image after modification
   

Thanks for your help ofnuts!

NB : It's my first script so I don't really know the syntax of Scheme :/
Find all posts by this user
Quote this message in a reply
Jan 31, 2012, 07:48
Post: #4
RE: Autocrop after fuzzy in a script ?
- Given the image, no need for all this, you can do an "autocrop" layer directly.

- I write all my Gimp scripts in Python. Much more readable and suitable for the job IMHO (plus you benefit from all the python libs). Its only downside is that it's not part of the standard setup on Windows/OSX, but it's not that hard to have (one of the gimp-win installers has it built-in). IIRC. To give you an idea of the difference, the Python code to autocrop all layers with the "link" flag:

Code:
def autocropLinkedLayers(image,drawable):
    for layer in image.layers:
        if layer.linked:
            print 'Autocropping layer %s' % layer.name
            pdb.gimp_image_set_active_layer(image, layer)
            pdb.plug_in_autocrop_layer(image, layer)

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
Jan 31, 2012, 07:52
Post: #5
RE: Autocrop after fuzzy in a script ?
Thanks ofnuts, I try now your python solution..
Find all posts by this user
Quote this message in a reply
Jan 31, 2012, 08:50
Post: #6
RE: Autocrop after fuzzy in a script ?
After the installation of python(not very easy to find 2.6), I write that :

Code:
#!/usr/bin/env python

# This tells Python to load the Gimp module
from gimpfu import *

# This is the function that will perform actual actions
def autocropLinkedLayers(image,drawable):
    for layer in image.layers:
        if layer.linked:
            print 'Autocropping layer %s' % layer.name
            pdb.gimp_image_set_active_layer(image, layer)
            pdb.plug_in_autocrop_layer(image, layer)

# This is the plugin registration function
register(
    "autocrop",    
    "autocrop",  
    "Ce script reduit l'image au plus utile.",
    "Kim24",
    "Kim24",
    "Janvier 2012",
    "<Image>/MyScripts/autocrop",
    "*",
    [],
    [],
    autocropLinkedLayers,
    )

I reboot Gimp and I found my script but when I run it, nothing happened..
My purpose is to run it in batch mode..(when it will work ^^)
Can you help more please ?
Find all posts by this user
Quote this message in a reply
Jan 31, 2012, 09:06 (This post was last modified: Jan 31, 2012 09:07 by ofnuts.)
Post: #7
RE: Autocrop after fuzzy in a script ?
As indicated this one only crops the layers that have the "link" checkbox (chainlink icon, next to the eye one) ticked in the Layers list ("if layer.linked...").

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
Jan 31, 2012, 09:16
Post: #8
RE: Autocrop after fuzzy in a script ?
Arf, and now, I don't have anymore my script registred :/
I'm lost!
Find all posts by this user
Quote this message in a reply
Jan 31, 2012, 10:50
Post: #9
RE: Autocrop after fuzzy in a script ?
Likely you introduced a syntax error, so the registration wasn't run... Python is very picky about indentation, space and tabs cannot be mixed, and some editors are a bit brutal with this (Kate that I use on Linux displays the tabs very nicely). My usual technique when I do heavy editing on python script is to launch them outside of Gimp: "python thescript.py". Most of the syntax errors get caught before it tries to import gimpfu, so if it gets as far as issuing a message on gimpfu import, the syntax is ok. There are also specific editors for Python (IDLE?) that catch most errors, and you can also paste your code in the python console to check it to some extent.

Under Windows, starting gimp with gimp-console may also let you catch some error messages (and the output of the "print" instruction used for debug).

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
Post Reply 


Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Create new images button & Save as override from Script-fu beniman 12 524 Sep 10, 2014 10:57
Last Post: paynekj
  fuzzy select bounding box pwinward 1 281 Sep 3, 2014 04:35
Last Post: pwinward
  fuzzy select modes katydove 2 346 Mar 26, 2014 11:56
Last Post: katydove
Question Odd Fuzzy Select Tool selection javierdl 5 478 Mar 6, 2014 17:54
Last Post: rich2005
  Line Script 2Old 8 645 Feb 6, 2014 21:51
Last Post: ofnuts

Forum Jump:


GIMP ForumPortalArchiveContactTermsRSS