CoolFace
Apppublic

iruno/test_wprm3

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
utils.py42 linesDownload Raw Back to root
1import tokenize2import io3import base644import numpy as np5from PIL import Image6 7def remove_inline_comments_safe(code: str) -> str:8    result = []9    tokens = tokenize.generate_tokens(io.StringIO(code).readline)10 11    last_line = -112    current_line = ''13    for tok_type, tok_string, (srow, scol), (_, _), _ in tokens:14        if srow != last_line:15            if current_line:16                result.append(current_line.rstrip())17            current_line = ''18            last_line = srow19 20        if tok_type == tokenize.COMMENT:21            # 주석 무시 (아무 것도 추가하지 않음)22            continue23 24        current_line += tok_string25 26    if current_line:27        result.append(current_line.rstrip())28 29    return '\n'.join(result)30 31 32def image_to_jpg_base64_url(image: Image.Image | np.ndarray) -> str:33    """Return a base64 *JPEG* data‑URL from a PIL image or NumPy array."""34    if isinstance(image, np.ndarray):35        image = Image.fromarray(image)36    if image.mode in {"RGBA", "LA"}:37        image = image.convert("RGB")38    with io.BytesIO() as buffer:39        image.save(buffer, format="JPEG")40        encoded: str = base64.b64encode(buffer.getvalue()).decode()41    return f"data:image/jpeg;base64,{encoded}"42