codeblock

2016年3月29日 星期二

Python Challenge level 7

http://www.pythonchallenge.com/pc/def/channel.html

- To do this challenge, please make sure you have already installed PILLOW.
https://pypi.python.org/pypi/Pillow
- This is such a huge task. First of all, the code is hidden in the picture. See the grayscale bar across the picture?
- Try to locate the index of pixel of the code bar. Find the height index.
from PIL import Image
oxygen = Image.open("oxygen.png")
width, height = oxygen.size
pixels = oxygen.load()
for i in range(height):
 [r, g, b, a] = pixels[0, i]
 if r==g and g==b and r==b:
  print i, [r, g, b, a]
- We could know the index is 43~51.
- And the find the end of the grayscale bar.
for i in range(width):
 [r, g, b, a] = pixels[i, 50]
 if r==g and g==b and r==b:
  print i, [r, g, b, a]
- The end index is 607.
- Now we could find the code by the ASCII code of the pixel number.
code_list = []
count = 0
for i in range(0, 607):
 if len(code_list) == 0:
  code_list.append(chr(pixels[i, 50][0]))
  print code_list[-1],
 else:
  if chr(pixels[i, 50][0]) != code_list[-1] or count >= 7:
   code_list.append(chr(pixels[i, 50][0]))
   count = 0
   print code_list[-1],
  else:
   count = count + 1
- The plain text is:
- The final step: invert the list to ASCII code again
answerlist = [105, 110, 116, 101, 103, 114, 105, 116, 121]
for i in range(len(answerlist)):
 print chr(answerlist[i]),
- The final answer is:

沒有留言:

張貼留言