Python Challenge - Level 7
Problem

There's a gray horizontal stripe running across the middle of the image. The gray-scale values of that stripe encode ASCII characters — read the pixel values left to right, every 7th pixel, and convert to chr().
Solution
Python
Pillow (PIL) makes pixel-level access trivial:
from urllib.request import urlopen
from PIL import Image
import io
data = urlopen("http://www.pythonchallenge.com/pc/def/oxygen.png").read()
img = Image.open(io.BytesIO(data)).convert("RGB")
width, height = img.size
mid_y = height // 2
# Sample every 7th pixel along the middle row; read the R channel (= G = B for gray)
chars = []
for x in range(0, width, 7):
r, g, b = img.getpixel((x, mid_y))
if r < 128: # stop at non-ASCII range
break
chars.append(chr(r))
message = "".join(chars)
print(message)
# → [105, 110, 116, 101, 103, 114, 105, 116, 121]
The first message is a list of ASCII codes — decode them too:
import re
codes = list(map(int, re.findall(r"\d+", message)))
print("".join(chr(c) for c in codes)) # integrity
Go
Go's image/png stdlib handles PNG decoding. color.GrayModel.Convert() extracts the luminance:
package main
import (
"fmt"
"image"
"image/color"
_ "image/png"
"io"
"net/http"
"regexp"
"strconv"
)
func main() {
resp, _ := http.Get("http://www.pythonchallenge.com/pc/def/oxygen.png")
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
img, _, _ := image.Decode(strings.NewReader(string(body)))
// Better: use bytes.NewReader
bounds := img.Bounds()
midY := bounds.Max.Y / 2
var chars []byte
for x := bounds.Min.X; x < bounds.Max.X; x += 7 {
gray := color.GrayModel.Convert(img.At(x, midY)).(color.Gray)
if gray.Y > 127 {
break
}
chars = append(chars, gray.Y)
}
message := string(chars)
fmt.Println(message)
// Second decode: extract numbers from the message
re := regexp.MustCompile(`\d+`)
var result []byte
for _, m := range re.FindAllString(message, -1) {
n, _ := strconv.Atoi(m)
result = append(result, byte(n))
}
fmt.Println(string(result)) // integrity
}
Rust
The image crate handles PNG decoding. Pixel access uses get_pixel:
use image::{GenericImageView, Pixel};
use regex::Regex;
fn main() {
let bytes = reqwest::blocking::get(
"http://www.pythonchallenge.com/pc/def/oxygen.png"
).unwrap().bytes().unwrap();
let img = image::load_from_memory(&bytes).unwrap();
let (width, height) = img.dimensions();
let mid_y = height / 2;
let chars: Vec<u8> = (0..width)
.step_by(7)
.map(|x| img.get_pixel(x, mid_y).to_luma().0[0])
.take_while(|&v| v <= 127)
.collect();
let message = String::from_utf8(chars).unwrap();
println!("{}", message);
// Second decode
let re = Regex::new(r"\d+").unwrap();
let answer: String = re.find_iter(&message)
.map(|m| m.as_str().parse::<u8>().unwrap() as char)
.collect();
println!("{}", answer); // integrity
}
Language Comparison
| Feature | Python | Go | Rust |
|---|---|---|---|
| PNG decode | PIL.Image.open() + .convert("RGB") |
image/png stdlib (import side-effect) |
image crate — load_from_memory() |
| Pixel access | img.getpixel((x, y)) — returns (R,G,B) tuple |
img.At(x, y) returns color.Color interface |
img.get_pixel(x, y) returns Rgba<u8> |
| Grayscale conversion | .convert("L") or read R channel directly |
color.GrayModel.Convert() |
.to_luma() |
| Step iteration | range(0, width, 7) |
for x := 0; x < w; x += 7 |
(0..width).step_by(7) |
Key insight: All three languages decode PNG entirely in memory with no temp files. The key difference is type safety for pixel values: Python returns a loosely typed tuple; Go returns a color.Color interface requiring a type assertion; Rust's get_pixel returns a typed Rgba<u8> struct. This means Rust catches mismatched channel assumptions at compile time that would be silent bugs in Python.

