Auto level with PIL?

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

Auto level with PIL?

Chris Barker
Hi all,

I need to do something like Photoshop's Auto Level to a bunch of images.
Honestly, I have only a vague idea of what it does, but I saw that
someone posted a script to do it here about 6 years ago, but I can't
seem to get that code from the list archives. It's also possible that
someone has improved upon it in all those years. Has anyone got any code
for this?

A pointer to a good description of what auto level is would be nice too.

-thanks,

-Chris



--
Christopher Barker, Ph.D.
Oceanographer
                                     
NOAA/OR&R/HAZMAT         (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Auto level with PIL?

Joao S. O. Bueno Calligaris
Hi,

If you can't find an autolevel  to use with PIL, maybe yyou can use
the GIMP instead.
It is scriptable in python as well.

        JS
        -><-

On Wednesday 07 September 2005 14:00, Chris Barker wrote:

> Hi all,
>
> I need to do something like Photoshop's Auto Level to a bunch of
> images. Honestly, I have only a vague idea of what it does, but I
> saw that someone posted a script to do it here about 6 years ago,
> but I can't seem to get that code from the list archives. It's also
> possible that someone has improved upon it in all those years. Has
> anyone got any code for this?
>
> A pointer to a good description of what auto level is would be nice
> too.
>
> -thanks,
>
> -Chris
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Auto level with PIL?

Fredrik Lundh
In reply to this post by Chris Barker
Chris Barker wrote:

> I need to do something like Photoshop's Auto Level to a bunch of images.
> Honestly, I have only a vague idea of what it does, but I saw that
> someone posted a script to do it here about 6 years ago, but I can't
> seem to get that code from the list archives.

this one?

http://aspn.activestate.com/ASPN/Mail/Message/image-sig/582729

here's the code from that post:

# adjust levels to fill histogram
# Kevin Cazabon, 1999.  [hidden email]  [hidden email]

def levels(data, all_same = 0, clip = 0):

    if data.mode not in ['RGB', 'CMYK']:
        return data

    lut = makelut(data, all_same, clip)

    data = data.point(lut)

    return data

def find_hi_lo(lut, clip):
    min = None
    max = None

    for i in range(len(lut)):
        if lut[i] > clip:
            min = i
            break

    lut.reverse()

    for i in range(len(lut)):
        if lut[i] > clip:
            max = 255 - i
            break

    return min, max

def scale(channels, min, max):
    lut = []
    for i in range (channels):
        for i in range(256):
            value = int((i - min)*(255.0/float(max-min)))
            if value < 0:
                value = 0
            if value > 255:
                value = 255
            lut.append(value)

    return lut


def makelut(data, all_same, clip):
    import Image

    histogram = data.histogram()

    lut = []
    r, g, b, k = [], [], [], []

    channels = len(histogram)/256

    for i in range(256):
        r.append(histogram[i])
        g.append(histogram[256+i])
        b.append(histogram[512+i])
    if channels == 4:
        for i in range(256):
            k.append(histogram[768+i])


    rmin, rmax = find_hi_lo(r, clip)
    gmin, gmax = find_hi_lo(g, clip)
    bmin, bmax = find_hi_lo(b, clip)
    if channels == 4:
        kmin, kmax = find_hi_lo(k)
    else:
        kmin, kmax = 128, 128

    if all_same == 1:

        min_max = [rmin, gmin, bmin, kmin, rmax, gmax, bmax, kmax]
        min_max.sort()
        lut = scale(channels, min_max[0], min_max[-1])

    else:

        r_lut = scale(1, rmin, rmax)
        g_lut = scale(1, gmin, gmax)
        b_lut = scale(1, bmin, bmax)
        if channels == 4:
            k_lut = scale(1, kmin, kmax)

        lut = []

        for i in range (256):
            lut.append(r_lut[i])
        for i in range (256):
            lut.append(g_lut[i])
        for i in range (256):
            lut.append(b_lut[i])
        if channels == 4:
            for i in range (256):
                lut.append(k_lut[i])

    return lut



_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Auto level with PIL?

Stefano Masini
In reply to this post by Chris Barker
Autolevel basically works by analyzing the histogram of the image.
The histogram of an 8-bit image is basically a set of 256 counters
(32767 if working in 16-bit mode) for each channel. Each one counts
how many bits are the in the image for the specified intensity value.
A pixel with color (100, 200, 150) increments by one the 100th red
counter, the 200th green one and the 150th blue one.
After building the histogram, you look at the lowest value that counts
at least one pixel, and the highest. Say that in your image no pixel
has a colour darker than (10, 15, 7) and no one brighter than (250,
247, 200). It means that the range of your image is 7-250.
Last step, you scale the values of each pixels proportionally so that
7 becomes 0, 250 becomes 255 and whatever in between is proportionally
scaled.

it's a rough explanation... but I hope it helps.
-stefano

On 9/7/05, Chris Barker <[hidden email]> wrote:

> Hi all,
>
> I need to do something like Photoshop's Auto Level to a bunch of images.
> Honestly, I have only a vague idea of what it does, but I saw that
> someone posted a script to do it here about 6 years ago, but I can't
> seem to get that code from the list archives. It's also possible that
> someone has improved upon it in all those years. Has anyone got any code
> for this?
>
> A pointer to a good description of what auto level is would be nice too.
>
> -thanks,
>
> -Chris
>
>
>
> --
> Christopher Barker, Ph.D.
> Oceanographer
>
> NOAA/OR&R/HAZMAT         (206) 526-6959   voice
> 7600 Sand Point Way NE   (206) 526-6329   fax
> Seattle, WA  98115       (206) 526-6317   main reception
>
> [hidden email]
> _______________________________________________
> Image-SIG maillist  -  [hidden email]
> http://mail.python.org/mailman/listinfo/image-sig
>
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Auto level with PIL?

Stefano Masini
In reply to this post by Fredrik Lundh
In this code all_same defaults to 0. The author says:
"I find the BEST way to color/density correct an image is to first set
the black/white point for each of the channels individually, so that
you use the entire available dynamic range."
While this is true, I must point out that you risk skewing the hue for
very dark colors and very bright ones. I think setting all_same to 1
would be a more color conservative option.

cheers,
-stefano

On 9/7/05, Fredrik Lundh <[hidden email]> wrote:

> Chris Barker wrote:
>
> > I need to do something like Photoshop's Auto Level to a bunch of images.
> > Honestly, I have only a vague idea of what it does, but I saw that
> > someone posted a script to do it here about 6 years ago, but I can't
> > seem to get that code from the list archives.
>
> this one?
>
> http://aspn.activestate.com/ASPN/Mail/Message/image-sig/582729
>
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Auto level with PIL?

Chris Barker
Thanks Stefano, this make sit much more clear what's going on. I'll give
this code a try.

-chris
--
Christopher Barker, Ph.D.
Oceanographer
                                     
NOAA/OR&R/HAZMAT         (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

[hidden email]
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Auto level with PIL?

Ben de Luca
In reply to this post by Joao S. O. Bueno Calligaris
Could you point me at some information about the gimp scripting in  
python. The only python things I found were woefully out of date.


On 08/09/2005, at 3:12 AM, Joao S. O. Bueno Calligaris wrote:

> Hi,
>
> If you can't find an autolevel  to use with PIL, maybe yyou can use
> the GIMP instead.
> It is scriptable in python as well.
>
>     JS
>     -><-
>
> On Wednesday 07 September 2005 14:00, Chris Barker wrote:
>
>> Hi all,
>>
>> I need to do something like Photoshop's Auto Level to a bunch of
>> images. Honestly, I have only a vague idea of what it does, but I
>> saw that someone posted a script to do it here about 6 years ago,
>> but I can't seem to get that code from the list archives. It's also
>> possible that someone has improved upon it in all those years. Has
>> anyone got any code for this?
>>
>> A pointer to a good description of what auto level is would be nice
>> too.
>>
>> -thanks,
>>
>> -Chris
>>
> _______________________________________________
> Image-SIG maillist  -  [hidden email]
> http://mail.python.org/mailman/listinfo/image-sig
>

_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Auto level with PIL?

Joao S. O. Bueno Calligaris
On Wednesday 07 September 2005 19:59, Ben de Luca wrote:
> Could you point me at some information about the gimp scripting in
> python. The only python things I found were woefully out of date.
>

The cenonicakl reference is still the aparently out of date page at:

www.jamesh.id.au/software/pygimp/


The way the plugins are built did not change. Basically, the steps are
these:
make a python script, and save it in a gimp - plugins directory (you
can see which are these under the preferences dialog in the GIMP)
Include the module "gimpfu".
At the end of your script call both gimpfu.register function, as
documented in the above url, and gimpfu.main ().
A call for yor script will show up in the menus in the GIMP.

What is newere than these documentation is the GIMP API - but this one
can be consulted on ine with the Procedural Database Browser, in the
GIMP  "Xtns" menu.

regards!
        JS
        -><-


> On 08/09/2005, at 3:12 AM, Joao S. O. Bueno Calligaris wrote:
> > Hi,
> >
> > If you can't find an autolevel  to use with PIL, maybe yyou can
> > use the GIMP instead.
> > It is scriptable in python as well.
> >
> >     JS
> >     -><-
> >
> > On Wednesday 07 September 2005 14:00, Chris Barker wrote:
> >> Hi all,
> >>
> >> I need to do something like Photoshop's Auto Level to a bunch of
> >> images. Honestly, I have only a vague idea of what it does, but
> >> I saw that someone posted a script to do it here about 6 years
> >> ago, but I can't seem to get that code from the list archives.
> >> It's also possible that someone has improved upon it in all
> >> those years. Has anyone got any code for this?
> >>
> >> A pointer to a good description of what auto level is would be
> >> nice too.
> >>
> >> -thanks,
> >>
> >> -Chris
> >
> > _______________________________________________
> > Image-SIG maillist  -  [hidden email]
> > http://mail.python.org/mailman/listinfo/image-sig
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig
Reply | Threaded
Open this post in threaded view
|

Re: Auto level with PIL?

Stefano Masini
On 9/8/05, Joao S. O. Bueno Calligaris <[hidden email]> wrote:
> The cenonicakl reference is still the aparently out of date page at:
>
> www.jamesh.id.au/software/pygimp/

This looks like a way to write python scripts that run _within_ Gimp.
In fact, the documentation says the system also takes care of details
like the gui, and all seems like running interactively.

Chris on the other hand seems like not wanting such an interactive
approach. Or, to say it differently, if he's ok with triggering the
thing with mouse clicks, why doesn't he simply create a Photoshop
action?

So, if what he really needs is batch processing of images, maybe out
of a crontab or driven by the click on a web interface, he can't
really use an interactive tool.

If PIL is not enough, or simply he does not want to go as low level as
reimplementing the algorithms himself, he could use gimp in batch
mode. I found this tutorial talking about it:
http://gimp.org/tutorials/Basic_Batch/

In the example it actually applies an unsharp mask to a bunch of
images in a directory, so probably something very similar to what
Chris needs to do.

Scriptfu can be quite powerful because it's basically a program
written in the Scheme language. But it's not python... :( I don't know
if pygimp allows batch processing as well. If it doesn't, then I guess
the only chance would be to use python to generate scheme code
snippets on the fly and feed them to gimp.

cheers,
stefano
_______________________________________________
Image-SIG maillist  -  [hidden email]
http://mail.python.org/mailman/listinfo/image-sig