If statement for printing pixel values

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2008
Posts: 43
Reputation: kiddo39 is an unknown quantity at this point 
Solved Threads: 0
kiddo39 kiddo39 is offline Offline
Light Poster

If statement for printing pixel values

 
0
  #1
May 4th, 2009
I am printing out the R,G,B values of an image and get results like this:

(30, 0, 0)
(29, 0, 0)
(28, 0, 0)
(27, 0, 0)
(26, 0, 0)
(25, 0, 0)
(24, 0, 6)
(23, 0, 0)
(22, 0, 0)
(21, 0, 0)

That is just a partial list of the values but you can see that in the one grouping it has (24,0,6) which is what I'm after. How can I get just that group or any others like it to print out, instead of the entire list? I'm assuming I need an if statement with > 0 but am not sure how to implement it. This is the initial code:

  1. from PIL import Image
  2. i = Image.open('Img.bmp')
  3. for pixel in i.getdata():
  4. print pixel
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,035
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 293
woooee woooee is offline Offline
Veteran Poster

Re: If statement for printing pixel values

 
0
  #2
May 4th, 2009
  1. i_getdata_test = [ \
  2. (30, 0, 0), \
  3. (29, 0, 0), \
  4. (28, 0, 0), \
  5. (27, 0, 0), \
  6. (26, 0, 0), \
  7. (25, 0, 0), \
  8. (24, 0, 6), \
  9. (23, 0, 0), \
  10. (22, 0, 0), \
  11. (21, 0, 0) ]
  12.  
  13. for pixel in i_getdata_test:
  14. print pixel
  15. ## 0, 1, and 2 are the memory offsets for
  16. ## the three integers in the tuple
  17. if pixel[2] > 0:
  18. print " we want this one"
  19.  
  20. ## if you want to explore what the "2" means,
  21. ## then uncomment the lines below
  22. ##print "\n"
  23. ##for pixel in i_getdata_test:
  24. ## print pixel[0], "-->", pixel[1], "-->", pixel[2]
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 43
Reputation: kiddo39 is an unknown quantity at this point 
Solved Threads: 0
kiddo39 kiddo39 is offline Offline
Light Poster

Re: If statement for printing pixel values

 
0
  #3
May 5th, 2009
Works great, thanks!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC