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

  • SEARCH
  • Home
  • 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 6991797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:33:03+00:00 2026-05-27T19:33:03+00:00

What would be the approach to trim an image that’s been input using a

  • 0

What would be the approach to trim an image that’s been input using a scanner and therefore has a large white/black area?

  • 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-27T19:33:03+00:00Added an answer on May 27, 2026 at 7:33 pm

    the entropy solution seems problematic and overly intensive computationally. Why not edge detect?

    I just wrote this python code to solve this same problem for myself. My background was dirty white-ish, so the criteria that I used was darkness and color. I simplified this criteria by just taking the smallest of the R, B or B value for each pixel, so that black or saturated red both stood out the same. I also used the average of the however many darkest pixels for each row or column. Then I started at each edge and worked my way in till I crossed a threshold.

    Here is my code:

    #these values set how sensitive the bounding box detection is
    threshold = 200     #the average of the darkest values must be _below_ this to count (0 is darkest, 255 is lightest)
    obviousness = 50    #how many of the darkest pixels to include (1 would mean a single dark pixel triggers it)
    
    from PIL import Image
    
    def find_line(vals):
        #implement edge detection once, use many times 
        for i,tmp in enumerate(vals):
            tmp.sort()
            average = float(sum(tmp[:obviousness]))/len(tmp[:obviousness])
            if average <= threshold:
                return i
        return i    #i is left over from failed threshold finding, it is the bounds
    
    def getbox(img):
        #get the bounding box of the interesting part of a PIL image object
        #this is done by getting the darekest of the R, G or B value of each pixel
        #and finding were the edge gest dark/colored enough
        #returns a tuple of (left,upper,right,lower)
    
        width, height = img.size    #for making a 2d array
        retval = [0,0,width,height] #values will be disposed of, but this is a black image's box 
    
        pixels = list(img.getdata())
        vals = []                   #store the value of the darkest color
        for pixel in pixels:
            vals.append(min(pixel)) #the darkest of the R,G or B values
    
        #make 2d array
        vals = np.array([vals[i * width:(i + 1) * width] for i in xrange(height)])
    
        #start with upper bounds
        forupper = vals.copy()
        retval[1] = find_line(forupper)
    
        #next, do lower bounds
        forlower = vals.copy()
        forlower = np.flipud(forlower)
        retval[3] = height - find_line(forlower)
    
        #left edge, same as before but roatate the data so left edge is top edge
        forleft = vals.copy()
        forleft = np.swapaxes(forleft,0,1)
        retval[0] = find_line(forleft)
    
        #and right edge is bottom edge of rotated array
        forright = vals.copy()
        forright = np.swapaxes(forright,0,1)
        forright = np.flipud(forright)
        retval[2] = width - find_line(forright)
    
        if retval[0] >= retval[2] or retval[1] >= retval[3]:
            print "error, bounding box is not legit"
            return None
        return tuple(retval)
    
    if __name__ == '__main__':
        image = Image.open('cat.jpg')
        box = getbox(image)
        print "result is: ",box
        result = image.crop(box)
        result.show()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am wondering how you would approach this problem I have two Taxrates that
What would be a better approach to compare two floating point values - using
I would like to trim down the routes on my application so that: http://myapplication.com/users/peter/questions/how-do-i-create-urls
I would like to code something that could take some sort of input and
I want to ask a question about how you would approach a simple object-oriented
What logging library or approach would you recommend for this case: We want to
I'm working on a request timeout mechanism. My initial approach would be to create
What would be the best approach to install a keyboard hook on Linux (X-windows)
Would you recommend me the best approach to edit SQL query with Visual Studio
I would like some advice on the best approach to use in the following

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.