"""Procedural product icons for the synthetic Next Match catalog (48x48 RGB, uint8).""" import numpy as np from PIL import Image, ImageDraw from match_core import RGB S = 48 def _shape(draw, cat, k, ox, oy): """Draw category silhouette (fill=255) into an L-mode mask. k = scale, (ox, oy) = offset.""" P = lambda pts: [(ox + k * x, oy + k * y) for x, y in pts] # noqa: E731 if cat == "tshirt": draw.polygon(P([(8, 10), (17, 6), (31, 6), (40, 10), (44, 19), (36, 22), (35, 42), (13, 42), (12, 22), (4, 19)]), fill=255) elif cat == "mug": draw.rectangle(P([(10, 12), (32, 42)]), fill=255) draw.ellipse(P([(26, 18), (42, 34)]), fill=255) draw.ellipse(P([(30, 22), (38, 30)]), fill=0) elif cat == "sneaker": draw.polygon(P([(4, 34), (6, 22), (16, 20), (24, 28), (40, 31), (44, 36), (44, 41), (4, 41)]), fill=255) elif cat == "bag": draw.rectangle(P([(8, 18), (40, 42)]), fill=255) draw.arc(P([(15, 6), (33, 30)]), 180, 360, fill=255, width=max(1, int(3 * k))) elif cat == "hat": draw.ellipse(P([(4, 30), (44, 40)]), fill=255) draw.pieslice(P([(12, 10), (36, 46)]), 180, 360, fill=255) elif cat == "chair": draw.rectangle(P([(12, 6), (18, 42)]), fill=255) draw.rectangle(P([(12, 24), (38, 30)]), fill=255) draw.rectangle(P([(32, 28), (37, 42)]), fill=255) elif cat == "lamp": draw.polygon(P([(14, 8), (34, 8), (40, 22), (8, 22)]), fill=255) draw.rectangle(P([(22, 22), (26, 38)]), fill=255) draw.rectangle(P([(14, 38), (34, 42)]), fill=255) elif cat == "bottle": draw.rectangle(P([(20, 4), (28, 12)]), fill=255) draw.polygon(P([(20, 12), (28, 12), (34, 20), (34, 43), (14, 43), (14, 20)]), fill=255) def render(cat, color, pattern, rng): """rng: numpy RandomState for jitter (position, scale, background, pattern phase, noise).""" k = rng.uniform(0.82, 1.0) ox, oy = rng.uniform(0, S - 48 * k + 1e-6), rng.uniform(0, S - 48 * k + 1e-6) mask = Image.new("L", (S, S), 0) _shape(ImageDraw.Draw(mask), cat, k, ox, oy) m = np.asarray(mask, dtype=np.float64) / 255.0 bg = rng.uniform(214, 240) img = np.ones((S, S, 3)) * np.array([bg, bg - rng.uniform(0, 8), bg - rng.uniform(0, 8)]) base = np.array(RGB[color], dtype=np.float64) accent = np.where(base > 128, base * 0.45, np.minimum(255, base + 150)) yy, xx = np.mgrid[0:S, 0:S] if pattern == "striped": phase = rng.uniform(0, 6) sel = ((xx + yy + phase) % 7) < 3 elif pattern == "dotted": px, py = rng.uniform(0, 6), rng.uniform(0, 6) sel = (((xx + px) % 7 - 3.5) ** 2 + ((yy + py) % 7 - 3.5) ** 2) < 3.2 else: sel = np.zeros((S, S), bool) fill = np.where(sel[:, :, None], accent, base) img = img * (1 - m[:, :, None]) + fill * m[:, :, None] # dark outline so white items stay visible edge = (np.abs(np.diff(np.pad(m, 1), axis=0))[:-1, 1:-1] + np.abs(np.diff(np.pad(m, 1), axis=1))[1:-1, :-1]) > 0.3 img[edge] = img[edge] * 0.35 img += rng.normal(0, 3.0, img.shape) return np.clip(np.round(img), 0, 255).astype(np.uint8)