WeChall - Simply Red

Challenge

Find the sentence hidden in me or I'll have to destroy you.

一张 256x256 的纯红色图像 op.png,只有 R 通道有数据(G=B=0)。

Solution

图像的 R 通道值范围 0-253,共 242 个唯一值。直接读取像素值无法得到可读文本(多数值 > 127,超出 ASCII 可打印范围)。

关键思路:素数筛选。只保留 R 通道值为素数的像素,其余设为白色。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from PIL import Image
from math import sqrt

def is_prime(n):
if n < 2: return False
if n == 2: return True
if n % 2 == 0: return False
for i in range(3, int(sqrt(n)) + 1, 2):
if n % i == 0: return False
return True

img = Image.open('op.png')
w, h = img.size
result = Image.new('RGB', (w, h), (255, 255, 255))

for y in range(h):
for x in range(w):
r, g, b = img.getpixel((x, y))
if is_prime(r):
result.putpixel((x, y), (0, 0, 0))
else:
result.putpixel((x, y), (255, 255, 255))

result.save('simply_red_primes.png')

素数像素形成大号文字,拼出一句名言。这句是 Optimus Prime(变形金刚)的台词。

No sacrifice is too great in the service of freedom.