-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevel_33.py
More file actions
74 lines (58 loc) · 1.96 KB
/
level_33.py
File metadata and controls
74 lines (58 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/bin/env python
# coding=utf-8
# http://kohsamui:thailand@www.pythonchallenge.com/pc/rock/beer.html
#
# If you are blinded by the light,
# remove its power, with its might.
# Then from the ashes, fair and square,
# another truth at you will glare.
import re
from io import BytesIO
import numpy as np
import requests
from PIL import Image
PREFIX = "http://kohsamui:thailand@www.pythonchallenge.com/pc/rock/"
url = PREFIX + 'beer2.jpg'
def catch(text, pattern=r'<a href="(.*?)">', cnt=0):
return re.findall(pattern, text, re.DOTALL)[cnt]
def solve(something):
im = Image.open(BytesIO(something))
im.show()
# no, png
url2 = PREFIX + 'beer2.png'
im = Image.open(BytesIO(requests.get(url2).content))
# use numpy to accelerate
data = np.array(im.getdata())
# use histogram to skip non-existing pixels
histogram = im.histogram()
hidden_images = []
for value in range(254, -1, -1):
if histogram[value] == 0:
continue
# Remove the brightest pixels
data = data[data < value]
# Reshape the array to a square image
sq = np.sqrt(len(data))
if sq.is_integer() and sq > 0:
max_val = np.max(data)
ashes = np.array([255 if v == max_val else 0 for v in data], dtype=np.uint8)
image = Image.fromarray(ashes.reshape(int(sq), int(sq)))
hidden_images.append(image)
# combine the images
total_width = hidden_images[0].width*len(hidden_images)
max_height = hidden_images[0].height
composite_image = Image.new('L', (total_width, max_height))
x_offset = 0
for img in hidden_images:
composite_image.paste(img, (x_offset, 0))
x_offset += img.width
return composite_image
def plot(im):
im.show()
if __name__ == "__main__":
r = requests.get(url)
something = r.content
answer = solve(something)
plot(answer)
# gremlins
# http://kohsamui:thailand@www.pythonchallenge.com/pc/rock/gremlins.html