Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6941841
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:56:14+00:00 2026-05-27T12:56:14+00:00

In the GIMP UI, there is an Apply Threshold functionality (GIMP 2.6) which has

  • 0

In the GIMP UI, there is an Apply Threshold functionality (GIMP 2.6) which has an option called Auto. This automatically calculates an appropriate lower threshold value for the image. Is this function/option available to use in a plugin? The gimp-threshold and gimp-histogram functions don’t seem to have this option.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:56:15+00:00Added an answer on May 27, 2026 at 12:56 pm

    Here’s the solution I finally used. Works only for grayscale images. Its the same algorithm as in the gimp_histogram_get_threshold function in gimphistogram.c

    http://git.gnome.org/browse/gimp/tree/app/base/gimphistogram.c

    (define (auto-threshold imagePath)
        (let*
            (
                (theImage (car (gimp-file-load
                                        RUN-NONINTERACTIVE
                                        imagePath
                                        imagePath
                                )
                        )
                )
    
                (theDrawable (car  (gimp-image-get-active-drawable theImage)))
                (hist (get-hist theDrawable 0))
            )
            (get-auto-threshold hist)
        )
    )
    
    ;returns the threshold 
    (define (get-auto-threshold hist)
        (let*
            (
                (hist_max (vector-ref hist 0))
                (chist (make-vector 256))
                (cmom (make-vector 256))
                (maxval 255) ;end - start
                (i 1)
                (tmp )
                (chist_max)
                (cmom_max)
                (bvar_max 0)
                (threshold 127)
            )
    
            (vector-set! chist 0 (vector-ref hist 0))
            (vector-set! cmom 0 0)
    
            (set! i 1)
            (while (<= i maxval)
                (if (> (vector-ref hist i) hist_max)
                    (set! hist_max (vector-ref hist i))
                )
                (vector-set! chist i (+ (vector-ref chist (- i 1)) (vector-ref hist i)) )
                (vector-set! cmom i (+ (vector-ref cmom (- i 1)) (* i (vector-ref hist i))) )
                (set! i (+ i 1))
            )
    
            (set! chist_max (vector-ref chist maxval))
            (set! cmom_max (vector-ref cmom maxval))
    
            (set! i 0)      
            (while (< i maxval)
            (if (and (> (vector-ref chist i) 0) (< (vector-ref chist i) chist_max) ) 
                (let* 
                   ((bvar (/ (vector-ref cmom i) (vector-ref chist i))))
    
                   (set! bvar (- bvar (/ (- cmom_max (vector-ref cmom i)) (- chist_max (vector-ref chist i)) ) ))
                   (set! bvar (* bvar bvar))
                   (set! bvar (* bvar (vector-ref chist i)) )
                   (set! bvar (* bvar (- chist_max (vector-ref chist i)) ))
    
                   (if (> bvar bvar_max) 
                      (begin
                        (set! threshold i)
                        (set! bvar_max bvar)
                      )
                    )
    
                )   
            )
            (set! i (+ i 1))
        )
    
        threshold
    )   
    
    
    )
    
    ;returns the raw histogram  with values 0-1 as an array
    (define (get-hist drawable chan)
    (let* (
    (i 0)
    (hist (make-vector 256))
    )
    (set! i 0)
    (while (< i 256)
    (vector-set! hist i (car (cddddr (gimp-histogram drawable chan i i))))
    (set! i (+ i 1))
    )
    hist
    )
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a screen shot from GIMP: There's a regular window header on the
This is a question that's been bugging me for some time now: In photoshop/GIMP,
is there any way to connect GIMP with python or PHP and use its
This may be waaay to specific for SO, but there seems to be a
this is my first script-fu for gimp and it gives me illegal function when
Hello as maybe you have heard about GIMP or something like that which uses
I would like to do a presentation like this : (done with Gimp )
I'm sure this is an easy one for someone out there. I have a
I'm looking for something like paint.net or Gimp, but for audio files, and runs
I am frustrated with string-to-number and number-to-string conversion in GIMP scripting. I am runnning

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.