File size: 1,242 Bytes
498ffec
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import tokenize
import io
import base64
import numpy as np
from PIL import Image

def remove_inline_comments_safe(code: str) -> str:
    result = []
    tokens = tokenize.generate_tokens(io.StringIO(code).readline)

    last_line = -1
    current_line = ''
    for tok_type, tok_string, (srow, scol), (_, _), _ in tokens:
        if srow != last_line:
            if current_line:
                result.append(current_line.rstrip())
            current_line = ''
            last_line = srow

        if tok_type == tokenize.COMMENT:
            # 주석 무시 (아무 것도 추가하지 않음)
            continue

        current_line += tok_string

    if current_line:
        result.append(current_line.rstrip())

    return '\n'.join(result)


def image_to_jpg_base64_url(image: Image.Image | np.ndarray) -> str:
    """Return a base64 *JPEG* data‑URL from a PIL image or NumPy array."""
    if isinstance(image, np.ndarray):
        image = Image.fromarray(image)
    if image.mode in {"RGBA", "LA"}:
        image = image.convert("RGB")
    with io.BytesIO() as buffer:
        image.save(buffer, format="JPEG")
        encoded: str = base64.b64encode(buffer.getvalue()).decode()
    return f"data:image/jpeg;base64,{encoded}"