addendum Re: working with images (PIL ?)

Poppy znfmail-pythonlang at yahoo.com
Mon May 19 10:18:00 EDT 2008


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\\")


"Ken Starks" <straton at lampsacos.demon.co.uk> wrote in message 
news:g0o0h3$jd4$1$8300dec7 at news.demon.co.uk...
> As others have said, PIL has the 'histogram' method to do most of the 
> work. However, as histogram works on each band separately, you have
> a bit of preliminary programming first to combine them.
>
> The ImageChops darker method is one easy-to-understand way (done twice),
> but there are lots of alternatives, I am sure.
>
>
> # ------------------------------------
>
> import Image
> import ImageChops
>
> Im = Image.open("\\\\server\\vol\\temp\\image.jpg")
> R,G,B = Im.split()
>
> Result=ImageChops.darker(R,G)
> Result=ImageChops.darker(Result,B)
>
> WhiteArea=Result.histogram()[0]
> TotalArea=Im.size[0] * Im.size[1]
> PercentageWhite = (WhiteArea * 100.0)/TotalArea
>
>
>
>
>
> Poppy wrote:
>> I've put together some code to demonstrate what my goal is though looping 
>> pixel by pixel it's rather slow.
>>
>> import Image
>>
>> def check_whitespace():
>>     im = Image.open("\\\\server\\vol\\temp\\image.jpg")
>>
>>     size = im.size
>>
>>     i = 0
>>     whitePixCount = 0
>>     while i in range(size[1]):
>>         j = 0
>>         while j in range(size[0]):
>>             p1 = im.getpixel((j,i))
>>             if p1 == (255, 255, 255):
>>                 whitePixCount = whitePixCount + 1
>>                 if whitePixCount >= 492804:  ## ((image dimensions 1404 x 
>> 1404) / 4) 25%
>>                     return "image no good"
>>             j = j + 1
>>         i = i + 1
>>
>>     print whitePixCount
>>
>>     return "image is good"
>>
>> print check_whitespace()
>>
>>
>> "Poppy" <znfmail-pythonlang at yahoo.com> wrote in message news:...
>>> I need to write a program to examine images (JPG) and determine how much 
>>> area is whitespace. We need to throw a returned image out if too much of 
>>> it is whitespace from the dataset we're working with. I've been 
>>> examining the Python Image Library and can not determine if it offers 
>>> the needed functionality. Does anyone have suggestions of other image 
>>> libraries I should be looking at it, or if PIL can do what I need?
>>>
>> 




More information about the Python-list mailing list