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 911033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:08:09+00:00 2026-05-15T17:08:09+00:00

I want to adjust the colour levels of an image in python. I can

  • 0

I want to adjust the colour levels of an image in python. I can use any python library that can easily be installed on my Ubuntu desktop. I want to do the same as ImageMagick’s -level ( http://www.imagemagick.org/www/command-line-options.html#level ). PIL (Python Image Library) doesn’t seem to have it. I have been calling convert on the image and then reading in the file back again, but that seems wasteful. Is there a better / faster way?

  • 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-15T17:08:10+00:00Added an answer on May 15, 2026 at 5:08 pm

    If I understood correctly the -level option of ImageMagick, then the level_image function I provide should do what you want.

    Two things to note:

    • the speed definitely can be improved
    • it currently only works with RGB images
    • the algorithm goes through the HSV colorspace, and affects only the V (brightness) component

    The code:

    import colorsys
    
    class Level(object):
    
        def __init__(self, minv, maxv, gamma):
            self.minv= minv/255.0
            self.maxv= maxv/255.0
            self._interval= self.maxv - self.minv
            self._invgamma= 1.0/gamma
    
        def new_level(self, value):
            if value <= self.minv: return 0.0
            if value >= self.maxv: return 1.0
            return ((value - self.minv)/self._interval)**self._invgamma
    
        def convert_and_level(self, band_values):
            h, s, v= colorsys.rgb_to_hsv(*(i/255.0 for i in band_values))
            new_v= self.new_level(v)
            return tuple(int(255*i)
                    for i
                    in colorsys.hsv_to_rgb(h, s, new_v))
    
    def level_image(image, minv=0, maxv=255, gamma=1.0):
        """Level the brightness of image (a PIL.Image instance)
        All values ≤ minv will become 0
        All values ≥ maxv will become 255
        gamma controls the curve for all values between minv and maxv"""
    
        if image.mode != "RGB":
            raise ValueError("this works with RGB images only")
    
        new_image= image.copy()
    
        leveller= Level(minv, maxv, gamma)
        levelled_data= [
            leveller.convert_and_level(data)
            for data in image.getdata()]
        new_image.putdata(levelled_data)
        return new_image
    

    If there is some way to do the RGB→HSV conversion (and vice versa) using PIL, then one can split into the H, S, V bands, use the .point method of the V band and convert back to RGB, speeding up the process by a lot; however, I haven’t found such a way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to adjust zsh so that I can tab complete: myprog <tab> using
I want to adjust each cell height according to the dynamic data that comes
I want to adjust exposure of an image using Java. I know in Java
when my form is shown, i want to adjust the height of certain control,
want to have a Hyperlink-Button in a gridView in which I can display a
i want to adjust the volume programatically like Get/SetMasterVolume in vista and xp? using
I have two group boxes, the top one is collapsible. I want to adjust
What I want to do is use the JS functions for additional input fields
I want to adjust the output from my TeamCity build configuration of my class
hi every 1 i have a problem in my jquery i want this adjust

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.