Datasets:
File size: 6,070 Bytes
ba2ef65 | 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 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.
"""
# Create a temporary image to store the box area (2w+2d, h+d)
res = Image.new("RGBA", (2*w + 2*d, h + d))
# Crop each face
# Layout: [Right] [Front] [Left] [Back]
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)
# Paste back to the flipped positions
# The new right face is the old left face, and the new left face is the old right face
res.paste(top, (d, 0))
res.paste(bottom, (d + w, 0))
res.paste(left, (0, d)) # New Right (0) <- Old Left
res.paste(front, (d, d)) # Front (d)
res.paste(right, (d + w, d)) # New Left (d+w) <- Old Right
res.paste(back, (2*d + w, d)) # Back (2d+w)
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")
# Unify to 64x64 format
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
# Determine if it is a slim (Alex) skin
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))
# 1. Flip head and hat (symmetrical components)
flipped_img.paste(flip_box(img, 0, 0, 8, 8, 8), (0, 0)) # Head
flipped_img.paste(flip_box(img, 32, 0, 8, 8, 8), (32, 0)) # Hat
# 2. Flip torso and jacket (symmetrical components)
flipped_img.paste(flip_box(img, 16, 16, 8, 12, 4), (16, 16)) # Body
flipped_img.paste(flip_box(img, 16, 32, 8, 12, 4), (16, 32)) # Jacket
# 3. Swap and flip left and right limbs
# Right Arm (40, 16) <-> Left Arm (32, 48)
flipped_img.paste(flip_box(img, 40, 16, w_arm, 12, 4), (32, 48)) # Old Right -> New Left
flipped_img.paste(flip_box(img, 32, 48, w_arm, 12, 4), (40, 16)) # Old Left -> New Right
# Right Leg (0, 16) <-> Left Leg (16, 48)
flipped_img.paste(flip_box(img, 0, 16, 4, 12, 4), (16, 48)) # Old Right -> New Left
flipped_img.paste(flip_box(img, 16, 48, 4, 12, 4), (0, 16)) # Old Left -> New Right
# 4. Flip overlay layers (Sleeves & Pants)
# Right Sleeve (40, 32) <-> Left Sleeve (48, 48)
flipped_img.paste(flip_box(img, 40, 32, w_arm, 12, 4), (48, 48)) # Old Right -> New Left
flipped_img.paste(flip_box(img, 48, 48, w_arm, 12, 4), (40, 32)) # Old Left -> New Right
# Right Pants (0, 32) <-> Left Pants (0, 48)
flipped_img.paste(flip_box(img, 0, 32, 4, 12, 4), (0, 48)) # Old Right -> New Left
flipped_img.paste(flip_box(img, 0, 48, 4, 12, 4), (0, 32)) # Old Left -> New Right
flipped_img.save(output_path)
print(f"Successfully saved flipped UV Map to {output_path}")
# Process description files at the same time
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}")
# Input image id, new_id
# Read control_imgs_v2/{id}.png
# Copy control_imgs_v2/{id}.png to control_imgs_v2/{new_id}.png
# Each image consists of front and back photos of the character (split in the middle, left-right layout, left is front, right is back).
# Now we need to flip the left and right parts horizontally and splice them into a new image.
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
# Read image
img = Image.open(input_path)
width, height = img.size
# Split into left and right parts (split in the middle)
# Left is front, right is back
left_part = img.crop((0, 0, width // 2, height))
right_part = img.crop((width // 2, 0, width, height))
# Horizontally flip the left and right parts respectively
left_flipped = left_part.transpose(Image.FLIP_LEFT_RIGHT)
right_flipped = right_part.transpose(Image.FLIP_LEFT_RIGHT)
# Splice into a new image
new_img = Image.new('RGBA', (width, height))
new_img.paste(left_flipped, (0, 0))
new_img.paste(right_flipped, (width // 2, 0))
# Save the new image
new_img.save(output_path)
print(f"Successfully saved flipped image to {output_path}")
# Process UV Map flipping
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)
|