Error .. Please Help

Dave Angel d at davea.name
Thu Dec 13 10:14:43 EST 2012


On 12/13/2012 04:23 AM, inshu chauhan wrote:
>>
>> if-else doesn't define a loop, but each of the for statements do.
>>
>> You have defined a classification for 8 of the possible colors, leaving
>> millions of them undefined.  If the first time through the loop you
>> manage to hit one of those undefined ones, you'll have no value for
>> classification.  So you get an exception.
>>
> 
> Yes, You are right that I will hit an exception if classification doesnot
> have any value.
> But as for the colors, actually I myself have colored the image using these
> 8 colors
> but still I think in some pixels I have put more than 1 color, thats why
> different colors are being encountered.
> 
>>
>> Worse, if you manage to get past that first pixel with a valid
>> classification, then for remaining pixels, you'll be using the last
>> 'valid' classification encountered.  This is the kind of "quiet failure"
>> that programmers dread.  Something that seems to work, but isn't even
>> close.
>>
> 
> For this I put an else clause at end but is there a better way to avoid
> this kind of situation ??

Why would you want to avoid it?  It has showed you there's a bug
somewhere.  That's better than silently doing something wrong.  As
ChrisA has said, you probably should raise your own exception in the
else clause, explaining to the user that the data file is invalid, and
exactly why.


> 
>>
>> If you're going to print (to file) on each iteration through the loop
>> (except 0,0,0), you need to make sure there's always a valid value.  So
>> you need at least one more classification value, and an else clause to
>> assign it, as ChrisA pointed out.
>>
>> Do you have a reason for treating (0,0,0) specially?  When that value is
>> seen, the logic skips the print as well, since continue skips to the
>> next loop iteration.
>>
> 
> Yes , I have to skip (0,0,0), that is a kind of requirement.
>

That's fine.

>>
>> Are you really getting floating point values, or are they always going
>> to be equal to an integer?  Those if/elif statements might be a problem
>> if you ever need to compare to a value like (128.4, 255.0, 255.0).
>>
> 
> I dont have values with decimal part, All values are integer.
> 

Then why are you comparing to floats?  It'd be faster and clearer and
less typing if you just use ints.  That is of course assuming that the
values returned by image[y,x] are a tuple of ints.  You can check that
by doing something like
    print [type(i) for i in color]

Is there any chance the image is 16 bits per pixel?

-- 

DaveA



More information about the Python-list mailing list