addendum Re: working with images (PIL ?)

Ken Starks straton at lampsacos.demon.co.uk
Mon May 19 13:00:43 EDT 2008


I would still be concerned that you are checking against the percentage
of the 768 bins returned by the histogram method. Two pixels of
widely different colour end up in the same bin, so long as just ONE
of the Red, Green, or Blue components is equal.

So, for example, colours (2, 27, 200) and (200, 27, 2) are both in
the bin for G=27. But they are very different colours.

There are actualy 256 * 256 * 256 colours, but I don't suppose
you want that many bins!

What you want is a much smaller number of bins, with pixels
of 'close' colours (whatever that means) put into the same bin.

What 'close' means for colours, is quite a difficult thing, and
the consensus is that using the three RGB coordinates is not
as good as certain other colour spaces.

You could use the ImageOps.posterize method to reduce the number of 
colours in the image, but whether 'close' colours end up together,
I don't know.

You might try the PIL special interest group (SIG) 'image-sig'

http://mail.python.org/mailman/listinfo/image-sig

(If you want to know exactly how many unique colours an image actually
has, load the image into the 'GIMP' assuming you have it,
and go to :

Menubar --> Filters --> Colours --> Colourcube analysis...

)










Poppy wrote:
> Thanks, since posting I  figured out how to interpret the histogram results, 
> which seems to be the consensus in responses. I wrote a check image program 
> and have been periodically calling it against a folder where I make a copy 
> of our images used for production. My method right now is to check what we 
> send for errors, but is not preventive.
> 
> Also I determined whitespace is not the only issue, any color that 
> dominates. I'm considering rewriting this code below to setup bins, so if 
> combined neighboring colors exceeds the threshold then reject the image. I 
> have examples where half the image appears black, but actually varies 
> throughout.
> 
> Since my image is RGB I'm looping through a 768 element list.
> 
> Zach-
> 
> import Image, os
> 
> 
> def check_image(file):
> 
>     try:
>         im = Image.open(file)
>     except:
>         return "Can't open file %s " % file
> 
>     imData = im.histogram()
>     i = 0
>     for ea in imData:
>         if ea > ((im.size[0] * im.size[1]) / 4): ## 25% of image size
>             return "bad image %s - %s element num is %s " % (file, ea, 
> str(i))
>         i = i + 1
> 
>     return "good image %s, image size is %s" % (file, im.size)
> 
> 
> def main(dir):
>     data = ""
>     try:
>         files = os.listdir(dir)
>         for ea in files:
>             data = data + str(check_image(os.path.join(dir,ea))) + "\n"
>     except:
>         return "Can't get files in %s" % dir
>     return data
> 
> print main("\\\\host\\path\\to\\image_folder\\")
> 
> 



More information about the Python-list mailing list