| import os |
| import argparse |
| import shutil |
| from PIL import Image |
|
|
| def flip_box(img, x, y, w, h, d): |
| """ |
| Horizontally flips a 3D box in the Minecraft skin UV map. |
| This includes swapping the left and right faces, and horizontally flipping all faces. |
| """ |
| |
| res = Image.new("RGBA", (2*w + 2*d, h + d)) |
| |
| |
| |
| top = img.crop((x + d, y, x + d + w, y + d)).transpose(Image.FLIP_LEFT_RIGHT) |
| bottom = img.crop((x + d + w, y, x + d + 2*w, y + d)).transpose(Image.FLIP_LEFT_RIGHT) |
| |
| right = img.crop((x, y + d, x + d, y + d + h)).transpose(Image.FLIP_LEFT_RIGHT) |
| front = img.crop((x + d, y + d, x + d + w, y + d + h)).transpose(Image.FLIP_LEFT_RIGHT) |
| left = img.crop((x + d + w, y + d, x + 2*d + w, y + d + h)).transpose(Image.FLIP_LEFT_RIGHT) |
| back = img.crop((x + 2*d + w, y + d, x + 2*d + 2*w, y + d + h)).transpose(Image.FLIP_LEFT_RIGHT) |
| |
| |
| |
| res.paste(top, (d, 0)) |
| res.paste(bottom, (d + w, 0)) |
| res.paste(left, (0, d)) |
| res.paste(front, (d, d)) |
| res.paste(right, (d + w, d)) |
| res.paste(back, (2*d + w, d)) |
| |
| return res |
|
|
| def process_uvmap(img_id, new_id): |
| """ |
| Flips the UV Map of img_label/{id}.png. |
| Requires swapping left and right limbs, and flipping the textures of each component. |
| """ |
| dir_name = "img_label" |
| input_path = f"{dir_name}/{img_id}.png" |
| output_path = f"{dir_name}/{new_id}.png" |
| |
| if not os.path.exists(input_path): |
| print(f"Warning: {input_path} does not exist, skipping UV Map flipping.") |
| return |
|
|
| img = Image.open(input_path).convert("RGBA") |
| |
| if img.size == (64, 32): |
| new_img = Image.new("RGBA", (64, 64), (0, 0, 0, 0)) |
| new_img.paste(img, (0, 0)) |
| img = new_img |
| elif img.size != (64, 64): |
| print(f"Error: Unsupported image size {img.size}") |
| return |
| |
| |
| is_slim = img.getpixel((47, 52))[3] == 0 |
| w_arm = 3 if is_slim else 4 |
| |
| flipped_img = Image.new("RGBA", (64, 64), (0, 0, 0, 0)) |
| |
| |
| flipped_img.paste(flip_box(img, 0, 0, 8, 8, 8), (0, 0)) |
| flipped_img.paste(flip_box(img, 32, 0, 8, 8, 8), (32, 0)) |
| |
| |
| flipped_img.paste(flip_box(img, 16, 16, 8, 12, 4), (16, 16)) |
| flipped_img.paste(flip_box(img, 16, 32, 8, 12, 4), (16, 32)) |
| |
| |
| |
| flipped_img.paste(flip_box(img, 40, 16, w_arm, 12, 4), (32, 48)) |
| flipped_img.paste(flip_box(img, 32, 48, w_arm, 12, 4), (40, 16)) |
| |
| |
| flipped_img.paste(flip_box(img, 0, 16, 4, 12, 4), (16, 48)) |
| flipped_img.paste(flip_box(img, 16, 48, 4, 12, 4), (0, 16)) |
| |
| |
| |
| flipped_img.paste(flip_box(img, 40, 32, w_arm, 12, 4), (48, 48)) |
| flipped_img.paste(flip_box(img, 48, 48, w_arm, 12, 4), (40, 32)) |
| |
| |
| flipped_img.paste(flip_box(img, 0, 32, 4, 12, 4), (0, 48)) |
| flipped_img.paste(flip_box(img, 0, 48, 4, 12, 4), (0, 32)) |
| |
| flipped_img.save(output_path) |
| print(f"Successfully saved flipped UV Map to {output_path}") |
|
|
| |
| txt_input = f"{dir_name}/{img_id}.txt" |
| txt_output = f"{dir_name}/{new_id}.txt" |
| if os.path.exists(txt_input): |
| shutil.copy(txt_input, txt_output) |
| print(f"Copied description file to {txt_output}") |
|
|
|
|
| |
| |
|
|
| |
| |
| |
|
|
| def process_image(img_id, new_id): |
| dir_name = "control_imgs_v2" |
| |
| input_path = f"{dir_name}/{img_id}.png" |
| output_path = f"{dir_name}/{new_id}.png" |
| |
| if not os.path.exists(input_path): |
| print(f"Error: {input_path} does not exist.") |
| return |
|
|
| |
| img = Image.open(input_path) |
| width, height = img.size |
| |
| |
| |
| left_part = img.crop((0, 0, width // 2, height)) |
| right_part = img.crop((width // 2, 0, width, height)) |
| |
| |
| left_flipped = left_part.transpose(Image.FLIP_LEFT_RIGHT) |
| right_flipped = right_part.transpose(Image.FLIP_LEFT_RIGHT) |
| |
| |
| new_img = Image.new('RGBA', (width, height)) |
| new_img.paste(left_flipped, (0, 0)) |
| new_img.paste(right_flipped, (width // 2, 0)) |
| |
| |
| new_img.save(output_path) |
| print(f"Successfully saved flipped image to {output_path}") |
| |
| |
| process_uvmap(img_id, new_id) |
|
|
| if __name__ == "__main__": |
| parser = argparse.ArgumentParser(description="Horizontally flip the front and back views of a character and splice them back together.") |
| parser.add_argument("id", type=str, help="Original image ID") |
| parser.add_argument("new_id", type=str, help="New image ID") |
| |
| args = parser.parse_args() |
| process_image(args.id, args.new_id) |
|
|