LiftLocaldemo / inspect_logo.py
kcooley03's picture
Add LiftLocal demo files
4405280
Raw History Blame Contribute Delete
2.02 kB
from PIL import Image
import os
HERE = os.path.dirname(__file__)
IMG_PATH = os.path.join(HERE, 'liftlocall.png')
OUT_PATH = os.path.join(HERE, 'logo_preview.png')
BG_COLOR = (0xF5, 0xF1, 0xE6) # page background from CSS
if not os.path.exists(IMG_PATH):
print('Image not found:', IMG_PATH)
raise SystemExit(2)
im = Image.open(IMG_PATH).convert('RGBA')
w, h = im.size
px = im.load()
opaque = 0
semi = 0
transparent = 0
alpha_total = 0
for y in range(h):
for x in range(w):
r,g,b,a = px[x,y]
alpha_total += a
if a == 255:
opaque += 1
elif a == 0:
transparent += 1
else:
semi += 1
total = w*h
print(f'Image: {IMG_PATH}')
print(f'Size: {w}x{h} => {total} pixels')
print(f'Opaque (a=255): {opaque} ({opaque/total:.2%})')
print(f'Semi-transparent (0<a<255): {semi} ({semi/total:.2%})')
print(f'Fully transparent (a=0): {transparent} ({transparent/total:.2%})')
print(f'Average alpha: {alpha_total/total:.1f} (0..255)')
# Create two composites side-by-side: left over page bg, right over black
bg = Image.new('RGBA', (w, h), BG_COLOR + (255,))
bk = Image.new('RGBA', (w, h), (0,0,0,255))
comp_bg = Image.alpha_composite(bg, im)
comp_bk = Image.alpha_composite(bk, im)
# Make preview canvas
preview = Image.new('RGBA', (w*2 + 10, h + 40), (255,255,255,255))
preview.paste(comp_bg, (0, 0))
preview.paste(comp_bk, (w+10, 0))
# add simple labels using Pillow's ImageDraw if available
try:
from PIL import ImageDraw, ImageFont
draw = ImageDraw.Draw(preview)
font = None
# try to load a default truetype font, fallback to default bitmap
try:
font = ImageFont.truetype('arial.ttf', 14)
except Exception:
font = ImageFont.load_default()
draw.text((10, h+6), 'Composited on page background (#f5f1e6)', fill=(0,0,0), font=font)
draw.text((w+20, h+6), 'Composited on black background', fill=(0,0,0), font=font)
except Exception:
pass
preview.save(OUT_PATH)
print('Wrote preview to', OUT_PATH)