Big images do not fit in a terminal, so text renderers shrink them first by averaging blocks of pixels.
What to build
A program that shrinks an image by averaging k × k blocks and prints the result as numbers and as text shading.
Requirements
- Split the image into k × k blocks starting at the top-left. Blocks at the right and bottom edges may be smaller; average only the pixels they contain.
- Each block's value is the sum of its pixels divided by their count, rounded down.
- Print the block values, one row of blocks per line, separated by spaces. Then print the same rows as characters from
.:-=+*o#%@, choosing character v × 10 // 256 for each value v.
Input
Line 1: H, W and the block size k. Then H rows of W values from 0 to 255.
Output
The rows of block values, then the same number of rows of characters.
Constraints
1 ≤ H, W ≤ 100; 1 ≤ k ≤ 20.
Example
Input:
2 2 1
0 255
128 64
Output:
0 255
128 64
.@
*-
Why it works
Averaging a block keeps its overall brightness while throwing away detail, which is exactly what shrinking needs; the ramp then maps each brightness to a character of similar darkness.
Examples
2 2 1 0 255 128 64
0 255 128 64 .@ *-
4 4 2 0 0 200 200 0 0 200 200 100 100 255 255 100 100 255 255
0 200 100 255 .# =@
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Step through the image in blocks with `range(0, h, k)` and `range(0, w, k)`; slicing past the edge simply gives a shorter piece.
Hint 2
The character for a value v is `RAMP[v * 10 // 256]` with `RAMP = ".:-=+*o#%@"`.
Solution
Show one correct solution (try it yourself first)
RAMP = ".:-=+*o#%@"
h, w, k = map(int, input().split())
img = [list(map(int, input().split())) for _ in range(h)]
means = []
for top in range(0, h, k):
row = []
for left in range(0, w, k):
block = [v for line in img[top:top + k] for v in line[left:left + k]]
row.append(sum(block) // len(block))
means.append(row)
for row in means:
print(" ".join(map(str, row)))
for row in means:
print("".join(RAMP[v * 10 // 256] for v in row))