AI-KING-Studio / cover_tools.py
INTERNETCON's picture
Deploy AI KING Studio (CPU basic, MiniMax-H3 excluded, content filter on)
3249e1b verified
Raw History Blame Contribute Delete
2.33 kB
"""Cover Checker + crop/export helpers (Pillow only, runs on free CPU)."""
import os, tempfile
from PIL import Image, ImageFilter, ImageOps
DISTROKID_RULES = (
"DistroKid reminders: no URLs, social handles, platform logos (TikTok/IG/YouTube), third-party brand logos, "
"or text that doesn't match the artist name / release title. No blurry or pixelated art."
)
def check_cover(path: str) -> str:
im = Image.open(path)
w, h = im.size
fmt = (im.format or "").upper()
lines = [f"Size: {w}x{h} | Mode: {im.mode} | Format: {fmt or 'unknown'}"]
ok = True
def flag(cond, good, bad):
nonlocal ok
lines.append(("PASS " + good) if cond else ("FAIL " + bad))
ok = ok and cond
flag(w == h, "Square", "Not square: use 'Make 3000x3000' below")
flag(min(w, h) >= 3000, "At least 3000x3000", "Smaller than 3000x3000: upscale first (Upscale tab) for best quality")
flag(fmt in ("JPEG", "JPG", "PNG"), "JPG/PNG", "Convert to JPG")
flag(im.mode == "RGB", "RGB color", f"Color mode is {im.mode}: export converts to RGB")
lines.append("")
lines.append("READY for DistroKid (still check text/logos by eye)." if ok else "Not ready yet: fix the FAIL lines.")
lines.append(DISTROKID_RULES)
return "\n".join(lines)
def _fit(im: Image.Image, size: tuple[int, int], mode: str) -> Image.Image:
im = ImageOps.exif_transpose(im).convert("RGB")
if mode == "crop":
return ImageOps.fit(im, size, Image.LANCZOS, centering=(0.5, 0.4))
# "pad": blurred-background fill keeps the whole picture (no cropping)
bg = ImageOps.fit(im, size, Image.LANCZOS).filter(ImageFilter.GaussianBlur(40))
fg = ImageOps.contain(im, size, Image.LANCZOS)
bg.paste(fg, ((size[0] - fg.width) // 2, (size[1] - fg.height) // 2))
return bg
def export_sizes(path: str, mode: str = "crop") -> list[str]:
im = Image.open(path)
outdir = tempfile.mkdtemp(prefix="aiking_")
base = os.path.splitext(os.path.basename(path))[0]
outs = []
for name, size in [("cover-3000x3000", (3000, 3000)), ("vertical-1080x1920", (1080, 1920)), ("wide-1920x1080", (1920, 1080))]:
p = os.path.join(outdir, f"{base}-{name}.jpg")
_fit(im, size, mode).save(p, "JPEG", quality=95, subsampling=0, optimize=True)
outs.append(p)
return outs