Datasets:
File size: 2,571 Bytes
bf97493 | 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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | import os
import numpy as np
from PIL import Image
def main():
base_dir = os.path.dirname(os.path.abspath(__file__))
skins_dir = os.path.join(base_dir, "skins")
mask_path = os.path.join(base_dir, "skin-mask.png")
decor_mask_path = os.path.join(base_dir, "skin-decor-mask.png")
if not os.path.exists(skins_dir):
print(f"Directory {skins_dir} does not exist.")
return
# Load masks
try:
mask1 = Image.open(mask_path).convert("RGBA")
mask2 = Image.open(decor_mask_path).convert("RGBA")
except Exception as e:
print(f"Error loading masks: {e}")
return
# Convert to numpy arrays
arr_mask1 = np.array(mask1)
arr_mask2 = np.array(mask2)
# Combined valid mask where either alpha is > 0
# mask alpha channel is index 3
valid_mask = (arr_mask1[:, :, 3] > 0) | (arr_mask2[:, :, 3] > 0)
# Process skins
processed_count = 0
cleaned_count = 0
for filename in os.listdir(skins_dir):
if not filename.lower().endswith(".png"):
continue
filepath = os.path.join(skins_dir, filename)
try:
skin = Image.open(filepath).convert("RGBA")
arr_skin = np.array(skin)
# Match dimensions
if arr_skin.shape[:2] != valid_mask.shape:
if arr_skin.shape[:2] == (32, 64): # 64x32 skin (height 32, width 64)
current_valid_mask = valid_mask[:32, :]
else:
print(f"Skipping {filename}: unexpected size {skin.size}")
continue
else:
current_valid_mask = valid_mask
# Find extra pixels: skin alpha > 0 but not in valid mask
extra_pixels = (arr_skin[:, :, 3] > 0) & (~current_valid_mask)
if extra_pixels.any():
print(f"Found extra pixels in {filename}. Cleaning...")
# Set extra pixels to transparent
arr_skin[extra_pixels] = [0, 0, 0, 0]
# Save the cleaned skin, overwrite original
cleaned_skin = Image.fromarray(arr_skin, "RGBA")
cleaned_skin.save(filepath)
cleaned_count += 1
processed_count += 1
except Exception as e:
print(f"Error processing {filename}: {e}")
print(f"\nDone! Processed {processed_count} skins. Cleaned extra pixels in {cleaned_count} skins.")
if __name__ == "__main__":
main()
|