# Pixel Verification Scripts

Reusable numpy-based pixel scanning scripts for verifying Pillow-generated images
when vision models cannot view them.

## Cover Layout Verification

```python
from PIL import Image
import numpy as np

cover = Image.open('封面.png')
arr = np.array(cover)
h, w = arr.shape[:2]

blocks = []
in_block = False; start = 0
for y in range(300, 1150):
    row = arr[y, 100:w-100, :]
    white = (row[:,0].astype(int)>200)&(row[:,1].astype(int)>200)&(row[:,2].astype(int)>200)
    orange = (row[:,0].astype(int)>180)&(row[:,1].astype(int)>80)&(row[:,1].astype(int)<180)&(row[:,2].astype(int)<80)
    count = np.sum(white) + np.sum(orange)
    if count > 10 and not in_block:
        start = y; in_block = True
    elif count <= 10 and in_block:
        blocks.append((start, y-1)); in_block = False
if in_block: blocks.append((start, 1149))

for j, (t, b) in enumerate(blocks):
    hp = b - t + 1
    mid_y = t + hp//2
    row = arr[mid_y, 100:w-100, :]
    wc = np.sum((row[:,0]>200)&(row[:,1]>200)&(row[:,2]>200))
    oc = np.sum((row[:,0]>180)&(row[:,1]>80)&(row[:,1]<180))
    color = 'WHITE' if wc > oc else 'ORANGE'
    label = 'Title' if hp > 70 else 'Tag'
    print(f'{label} {j}: y={t}-{b} (h={hp}px, center={mid_y}) [{color}]')

gaps = [f'{blocks[k+1][0]-blocks[k][1]}px' for k in range(len(blocks)-1)]
print(f'Gaps: {" → ".join(gaps)}')
```

## Inner Page Element Detection

```python
from PIL import Image
import numpy as np

inner = Image.open('内页.png')
arr = np.array(inner)
h, w = arr.shape[:2]

# Top bar
bar = arr[10:70, 100:w-100, :]
has_bar = bool(np.any((bar[:,:,0] < 50) & (bar[:,:,1] > 50)))

# Card shadows (data page)
shadow_zone = arr[260:360, 100:w-100, :]
shadow_px = int(np.sum((shadow_zone[:,:,0] > 200) & (shadow_zone[:,:,1] > 220)))
has_cards = shadow_px > 300

# Step circles
circle_zone = arr[280:400, 100:200, :]
circle_px = int(np.sum((circle_zone[:,:,0] < 50) & (circle_zone[:,:,1] > 50)))
has_circles = circle_px > 100

# Table header (full-width dark green)
table_zone = arr[280:350, 200:w-200, :]
table_px = int(np.sum((table_zone[:,:,0] < 50) & (table_zone[:,:,1] > 50)))
has_table = table_px > 500

# Orange numbers
orange_zone = arr[280:550, 100:w-100, :]
orange_px = int(np.sum((orange_zone[:,:,0] > 200) & (orange_zone[:,:,1] > 80) & (orange_zone[:,:,1] < 180)))
has_orange = orange_px > 100

# Footer
footer_zone = arr[h-80:h-50, 300:w-300, :]
has_footer = bool(np.any((footer_zone[:,:,0] > 80) & (footer_zone[:,:,0] < 130) & (footer_zone[:,:,1] > 130)))

print(f'Top bar: {has_bar}, Cards: {has_cards}, Circles: {has_circles}')
print(f'Table: {has_table}, Orange: {has_orange}, Footer: {has_footer}')
```

## Finding "News" or Design Elements on Template

```python
from PIL import Image
import numpy as np

tmpl = Image.open('template.png')
arr = np.array(tmpl)
h, w = arr.shape[:2]

# Find non-background areas by color deviation
bg_r, bg_g, bg_b = 21, 86, 44  # sample background color

for y in range(0, h):
    row = arr[y, :, :]
    r, g, b = row[:,0].astype(int), row[:,1].astype(int), row[:,2].astype(int)
    diff = np.abs(r-bg_r) + np.abs(g-bg_g) + np.abs(b-bg_b)
    outliers = np.sum(diff > 40)
    if outliers > 50:
        outlier_positions = np.where(diff > 40)[0]
        left = outlier_positions.min()
        right = outlier_positions.max()
        avg_r = np.mean(r[diff>40])
        avg_g = np.mean(g[diff>40])
        avg_b = np.mean(b[diff>40])
        print(f'y={y}: {outliers} outliers, x=[{left}-{right}], avg=({avg_r:.0f},{avg_g:.0f},{avg_b:.0f})')
```

## Edge Detection for Text Finding

```python
from PIL import Image, ImageFilter
import numpy as np

img = Image.open('image.png')
edges = img.filter(ImageFilter.FIND_EDGES)
edge_arr = np.array(edges)

for y in range(0, 1660, 5):
    row = edge_arr[y, 100:1142, :]
    bright = np.sum(np.max(row.astype(int), axis=1) > 50)
    if bright > 100:
        print(f'y={y}: {bright} edge pixels')
```
