Spaces:
Sleeping
Sleeping
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}" | |