Spaces:
Build error
Build error
| import os | |
| from PIL import Image | |
| # Cấu hình đường dẫn | |
| input_dir = "static/characters" | |
| output_dir = "static/characters_green" | |
| backup_dir = "static/characters_backup" | |
| # Tạo các thư mục nếu chưa có | |
| os.makedirs(output_dir, exist_ok=True) | |
| os.makedirs(backup_dir, exist_ok=True) | |
| print(f"Starting to process images from {input_dir}...") | |
| green_color = (0, 255, 0) # Mã màu xanh lá chuẩn (RGB) | |
| files = [f for f in os.listdir(input_dir) if f.endswith(".png")] | |
| total = len(files) | |
| for i, filename in enumerate(files): | |
| input_path = os.path.join(input_dir, filename) | |
| output_path = os.path.join(output_dir, filename) | |
| try: | |
| with Image.open(input_path) as img: | |
| # 1. Chuyển sang RGBA nếu chưa có | |
| img = img.convert("RGBA") | |
| # 2. Tạo một nền xanh thuần túy cùng kích thước | |
| background = Image.new("RGB", img.size, green_color) | |
| # 3. Dán ảnh nhân vật lên nền xanh (dùng chính kênh alpha của ảnh làm mask) | |
| background.paste(img, (0, 0), img) | |
| # 4. Lưu lại thành phẩm | |
| background.save(output_path, "PNG") | |
| print(f"[{i+1}/{total}] Processed: {filename}") | |
| except Exception as e: | |
| print(f"[!] Error processing {filename}: {e}") | |
| print(f"\n--- DONE! ---") | |
| print(f"Total: {total} images saved to: {output_dir}") | |
| print(f"Note: Original files are untouched. Please check characters_green folder.") | |