Datasets:
Download merge_skins.py from EntropyDrop/Sking: direct link, hf CLI and curl.
- Browser
- Download file 2.57 kB
-
https://huggingface.co/datasets/EntropyDrop/Sking/resolve/main/merge_skins.py
- Command line
-
hf download hf://datasets/EntropyDrop/Sking/merge_skins.py
-
curl -L -o merge_skins.py https://huggingface.co/datasets/EntropyDrop/Sking/resolve/main/merge_skins.py
2.57 kB
| import argparse | |
| from PIL import Image | |
| def get_head_uvs(): | |
| """ | |
| Returns the (x, y, w, h) of each 2D texture block of the head. | |
| """ | |
| head_uvs = [] | |
| # [ [ [size, offset], ... ], decor_offset ] | |
| part = [ | |
| [ | |
| [(8,8,8),(8,8)], # front | |
| [(8,8,8),(24,8)], # back | |
| [(8,8,8),(16,8)], # left | |
| [(8,8,8),(0,8)], # right | |
| [(8,8,8),(8,0)], # top | |
| [(8,8,8),(16,0)], # bottom | |
| ], (32,0) | |
| ] | |
| decor_offset = part[1] | |
| for size_info, offset in part[0]: | |
| # Take the first two values from the 3D size (dx, dy, dz) as the width and height of the 2D texture map | |
| w, h = size_info[0], size_info[1] | |
| # Base head area | |
| head_uvs.append((offset[0], offset[1], w, h)) | |
| # Head decoration (decor) area | |
| decor_x = offset[0] + decor_offset[0] | |
| decor_y = offset[1] + decor_offset[1] | |
| head_uvs.append((decor_x, decor_y, w, h)) | |
| return head_uvs | |
| def merge_skins(skin_a_path, skin_b_path, output_path): | |
| # Open images and ensure they have an alpha channel | |
| img_a = Image.open(skin_a_path).convert("RGBA") | |
| img_b = Image.open(skin_b_path).convert("RGBA") | |
| # Check if dimensions are compatible | |
| if img_a.size != img_b.size: | |
| print(f"Warning: Skin A ({img_a.size}) and Skin B ({img_b.size}) sizes do not match.") | |
| # Use B (provides body and limbs) as the base background | |
| img_c = img_b.copy() | |
| # Get head regions that need to be copied from A | |
| head_uvs = get_head_uvs() | |
| for x, y, w, h in head_uvs: | |
| box = (x, y, x + w, y + h) | |
| # Crop the corresponding head block from A | |
| region_a = img_a.crop(box) | |
| # Paste onto C. Do not use mask to ensure complete coverage (including transparent pixels covering B's original pixels) | |
| img_c.paste(region_a, box) | |
| img_c.save(output_path) | |
| print(f"Successfully merged: A's head ({skin_a_path}) + B's body/limbs ({skin_b_path}) -> {output_path}") | |
| if __name__ == "__main__": | |
| parser = argparse.ArgumentParser(description="Combine skins: Take the head of Skin A and the body and limbs of Skin B.") | |
| parser.add_argument("skin_a", help="Path to Skin A (provides the head)") | |
| parser.add_argument("skin_b", help="Path to Skin B (provides the body and limbs)") | |
| parser.add_argument("-o", "--output", default="skin_c.png", help="Output path for Skin C") | |
| args = parser.parse_args() | |
| merge_skins(args.skin_a, args.skin_b, args.output) | |