Files
workspace/code/app/.workbuddy/check_bg.py
T
2026-06-29 22:21:28 +08:00

118 lines
4.0 KiB
Python

# -*- coding: utf-8 -*-
import sys
import io
import urllib.request
from PIL import Image
URL = ("http://118.89.70.199:9001/test/finance-category-default/income/hongbao.webp"
"?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD"
"&X-Amz-Credential=54WPUR104SA9X00EKWS3%2F20260629%2Fus-east-1%2Fs3%2Faws4_request"
"&X-Amz-Date=20260629T105003Z&X-Amz-Expires=86400"
"&X-Amz-Security-Token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJleHAiOjE3ODI3NzMyMDUsInBhcmVudCI6Im9uZWFvIn0.zkP6L1zg_LPy1UuK-3-VT6LEWuhLNickKWTWDly_PzJCnwIRPS4naSZE9Lx84HtXPVVhxaZguHXl65AXjROJZA"
"&X-Amz-Signature=b763181ad8bf74deb2ac55ac18402d26a91075088c9c109cfd3a4da6cca7b38f"
"&X-Amz-SignedHeaders=host&x-amz-checksum-mode=ENABLED&x-id=GetObject")
out_path = r"D:\workspace\code\app\.workbuddy\hongbao.webp"
req = urllib.request.Request(URL, headers={"User-Agent": "Mozilla/5.0"})
with urllib.request.urlopen(req, timeout=60) as resp:
data = resp.read()
print("Downloaded bytes:", len(data))
with open(out_path, "wb") as f:
f.write(data)
img = Image.open(out_path)
print("Format:", img.format)
print("Size:", img.size)
print("Mode:", img.mode)
# Force load pixel data
img.load()
has_alpha = img.mode in ("RGBA", "LA") or (img.mode == "P" and "transparency" in img.info)
print("Has alpha channel:", has_alpha)
if img.mode == "P":
# Convert palette to RGBA to inspect real pixels
rgba = img.convert("RGBA")
elif img.mode != "RGBA":
rgba = img.convert("RGBA")
else:
rgba = img
w, h = rgba.size
px = rgba.load()
def info(x, y):
r, g, b, a = px[x, y]
return f"({x},{y}) RGBA=({r},{g},{b},{a})"
print("\n--- Corner pixels ---")
print("TL", info(0, 0))
print("TR", info(w - 1, 0))
print("BL", info(0, h - 1))
print("BR", info(w - 1, h - 1))
print("\n--- Edge midpoints ---")
print("Top-mid", info(w // 2, 0))
print("Bottom-mid", info(w // 2, h - 1))
print("Left-mid", info(0, h // 2))
print("Right-mid", info(w - 1, h // 2))
# Statistics over all pixels
total = w * h
transparent = 0 # alpha == 0
near_transparent = 0 # alpha < 255 and > 0
fully_opaque = 0 # alpha == 255
white_pixels = 0 # opaque & RGB ~ (255,255,255)
near_white_bg = 0 # alpha==0 OR rgb near white
for y in range(h):
for x in range(w):
r, g, b, a = px[x, y]
if a == 0:
transparent += 1
elif a < 255:
near_transparent += 1
else:
fully_opaque += 1
if r > 245 and g > 245 and b > 245:
white_pixels += 1
print("\n--- Global statistics ---")
print("Total pixels:", total)
print("Fully transparent (a==0):", transparent, f"({100*transparent/total:.2f}%)")
print("Semi-transparent (0<a<255):", near_transparent, f"({100*near_transparent/total:.2f}%)")
print("Fully opaque (a==255):", fully_opaque, f"({100*fully_opaque/total:.2f}%)")
print("Opaque & near-white:", white_pixels, f"({100*white_pixels/total:.2f}%)")
# Border-only statistics (outer 2px ring) - this is what reveals background
def is_border(x, y, w, h, t=2):
return x < t or y < t or x >= w - t or y >= h - t
border_total = 0
border_trans = 0
border_white = 0
for y in range(h):
for x in range(w):
if is_border(x, y, w, h):
border_total += 1
r, g, b, a = px[x, y]
if a == 0:
border_trans += 1
elif r > 245 and g > 245 and b > 245:
border_white += 1
print("\n--- Border ring (outer 2px) ---")
print("Border pixels:", border_total)
print("Border transparent:", border_trans, f"({100*border_trans/border_total:.2f}%)")
print("Border near-white opaque:", border_white, f"({100*border_white/border_total:.2f}%)")
print("\n=== CONCLUSION ===")
if border_trans / border_total > 0.5:
print("BACKGROUND IS TRANSPARENT (alpha channel, mostly a==0 at edges)")
elif border_white / border_total > 0.5:
print("BACKGROUND IS WHITE (opaque near-white pixels at edges)")
else:
print("Background is neither clearly transparent nor white - inspect manually")