| | |
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:
Solved Threads: 0
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:
(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:
Python Syntax (Toggle Plain Text)
from PIL import Image i = Image.open('Img.bmp') for pixel in i.getdata(): print pixel
•
•
Join Date: Dec 2006
Posts: 1,035
Reputation:
Solved Threads: 293
Python Syntax (Toggle Plain Text)
i_getdata_test = [ \ (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) ] for pixel in i_getdata_test: print pixel ## 0, 1, and 2 are the memory offsets for ## the three integers in the tuple if pixel[2] > 0: print " we want this one" ## if you want to explore what the "2" means, ## then uncomment the lines below ##print "\n" ##for pixel in i_getdata_test: ## print pixel[0], "-->", pixel[1], "-->", pixel[2]
![]() |
Other Threads in the Python Forum
- Previous Thread: simple addition of variables
- Next Thread: Ubuntu and Python
| Thread Tools | Search this Thread |
address anydbm app beginner changecolor cipher class conversion coordinates corners curves definedlines development dictionary dynamic events examples excel feet file float font format ftp function generator getvalue gui handling homework images import input ip java keycontrol line linux list lists loan loop maintain maze millimeter mouse mysqldb number numbers output parsing path permissions port prime programming projects py2exe pygame pymailer python queue random rational raw_input recursion recursive scrolledtext searchingfile shebang slicenotation split string strings table tails terminal text thread threading time tkinter tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 variable variables vigenere web windows wx.wizard wxpython xlwt






