Newbie question about tuples and list comprehensions

2008-06-25 Thread idiolect
Hi all - Sorry to plague you with another newbie question from a
lurker.  Hopefully, this will be simple.

I have a list full of RGB pixel values read from an image.  I want to
test each RGB band value per pixel, and set it to something else if it
meets or falls below a certain threshold - i.e., a Red value of 0
would be changed to 50.

I've built my list by using a Python Image Library statement akin to
the following:

data = list(image.getdata())

Which produces a very long list that looks like [(0,150,175),
(50,175,225),...].  I'm trying to figure out a fast and pythonic way
to perform my operation.  The closest I've come so far to a succinct
statement is a list comprehension along the syntax of:

source = [((x,y,z),(x+50,y+50,z+50))[bool(x or y or z < 50)] for
(x,y,z) in source]

...which kind of approaches the effect I'm looking for, but it doesn't
really test and change each value in the tuple individually.  My
understanding of the things you can do with lists and python in
general is rather naive, so I would appreciate any insight anyone can
offer since I am not sure if I'm even headed down the correct path
with list comprehensions.

Much obliged!
--
http://mail.python.org/mailman/listinfo/python-list


Re: Newbie question about tuples and list comprehensions

2008-06-25 Thread idiolect
On Jun 25, 7:26 pm, Terry Reedy <[EMAIL PROTECTED]> wrote:
> idiolect wrote:
> > Hi all - Sorry to plague you with another newbie question from a
> > lurker.  Hopefully, this will be simple.
>
> > I have a list full of RGB pixel values read from an image.  I want to
> > test each RGB band value per pixel, and set it to something else if it
> > meets or falls below a certain threshold - i.e., a Red value of 0
> > would be changed to 50.
>
> > I've built my list by using a Python Image Library statement akin to
> > the following:
>
> > data = list(image.getdata())
>
> > Which produces a very long list that looks like [(0,150,175),
> > (50,175,225),...].  I'm trying to figure out a fast and pythonic way
> > to perform my operation.  The closest I've come so far to a succinct
> > statement is a list comprehension along the syntax of:
>
> Why are you trying to do this with a list comprehension?  Learn the
> basics first.  Perhaps you have read too many of the recent threads
> presenting diverting challenges for bored experienced programmers.  Some
> of these were definitely not Pythonic code for real use.
>
> First question: do you really want to create a new 'very long list' or
> modify list 'data' in place.  Let's assume the latter.
>
> for i,tup in enumerate(data):
>  data[i] = replace(tup)
>
> where replace(tup) is an expression or function that produces a tuple
> meeting your criteria.  Simplest is
> (max(tup[0],Rthresh), max(tup[1],Gthresh), max(tup[2],Bthresh)).
>
> If nearly all your pixels are ok, add the following before the
> assignment so you only make replacements when actually needed:
> if tup[0] < Rthresh or tup[1] < Gthresh or tup[2] < Bthresh:
>
> Terry Jan Reedy

A giant thank-you to all who've posted in response to my query - these
are all much better approaches to my problem.  I think I got hooked on
using a list comprehension as it seemed the most concise approach vs.
other techniques after a bunch of Google searches, but all of you have
pointed out more efficient methods.  I appreciate your willingness to
indulge a n00b who hasn't thought his problem through, apparently.
I'll try all of these approaches out over the next day and see what
works best, although I suspect you've all posted sufficient solutions.

Can't wait to try these suggestions out - cheers, idiolect
--
http://mail.python.org/mailman/listinfo/python-list