Spaces:
Build error
Build error
File size: 1,524 Bytes
911c66e | 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 43 44 45 | 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.")
|