By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,550 Members | 1,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,550 IT Pros & Developers. It's quick & easy.

Why does if else statement always return if code even when failed?

P: 8
The problem is that the "if-else" command doesn't work properly. the program always returns entire "else" commands means all black pixels in end.bmp . but there is some pixels that have the same values "(224 238 223)" in "model1.jpg".
thanks for your help.


Code :
Expand|Select|Wrap|Line Numbers
  1. import Image
  2. model1 = Image.open("model1.jpg")
  3. test = Image.open("test.jpg")
  4. im  = Image.new("RGB",(500,500))
  5.  
  6. rr1=224
  7. gg1=238
  8. bb1=223
  9.  
  10. for i in range(500):
  11.  for j in range(500):
  12.  
  13.   rtest,gtest,btest=test.getpixel((i,j))
  14.   r1,g1,b1=model1.getpixel((i,j))
  15.  
  16.   if r1==rr1 & g1==gg1 & b1==bb1:
  17.    realtestr=rtest
  18.    realtestg=gtest
  19.    realtestb=btest
  20.   else:
  21.    realtestr=0
  22.    realtestg=0
  23.    realtestb=0
  24.   im.putpixel((i,j),(realtestr,realtestg,realtestb)),
  25.  
  26. im.save('end.bmp')
  27. im.show()
  28.  
Feb 4 '11 #1

✓ answered by bvdet

Don't use the bitwise and operator & - use and. The & operator takes precedence over == and == takes precedence over and.

Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
Don't use the bitwise and operator & - use and. The & operator takes precedence over == and == takes precedence over and.
Feb 4 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.