import argparse import uuid import concurrent.futures from alice_to_steve import alice_to_steve from PIL import Image import os import numpy as np import asyncio import mc_render import random from mc_voxel_texture_resolver import resolve_voxel_consistency SCALING_RATIO = 12 IMAGE_WIDTH = 768 IMAGE_HEIGHT = 768 SCALING_RATIO = 12*IMAGE_HEIGHT/768 SKIN_MASK = "skin-mask.png" SKIN_DECOR_MASK = "skin-decor-mask.png" bg = (128,128,128) def create_mask(): if os.path.exists(SKIN_MASK) and os.path.exists(SKIN_DECOR_MASK): return # 64*64 mask = Image.new('RGBA', (64, 64), (0, 0, 0, 0)) decor_mask = Image.new('RGBA', (64, 64), (0, 0, 0, 0)) for (size, offset, decor_offset) in [ # head [(8,8),(0,8),(32,0)], [(8,8),(8,8),(32,0)], [(8,8),(8,0),(32,0)], [(8,8),(16,8),(32,0)], [(8,8),(16,0),(32,0)], [(8,8),(24,8),(32,0)], #left arm [(4,12),(32,52),(16,0)], [(4,12),(32+4,52),(16,0)], [(4,12),(32+8,52),(16,0)], [(4,12),(32+12,52),(16,0)], [(4,4),(32+4,48),(16,0)], [(4,4),(32+8,48),(16,0)], #right arm [(4,12),(40,20),(0,16)], [(4,12),(40+4,20),(0,16)], [(4,12),(40+8,20),(0,16)], [(4,12),(40+12,20),(0,16)], [(4,4),(40+4,16),(0,16)], [(4,4),(40+8,16),(0,16)], #body [(8,4),(20,16),(0,16)], [(8,4),(20+8,16),(0,16)], [(8,12),(20,20),(0,16)], [(8,12),(20+12,20),(0,16)], [(4,12),(16,20),(0,16)], [(4,12),(28,20),(0,16)], #left leg [(4,12),(16,52),(-16,0)], [(4,12),(16+4,52),(-16,0)], [(4,12),(16+8,52),(-16,0)], [(4,12),(16+12,52),(-16,0)], [(4,4),(16+4,48),(-16,0)], [(4,4),(16+8,48),(-16,0)], #right leg [(4,12),(0,20),(0,16)], [(4,12),(0+4,20),(0,16)], [(4,12),(0+8,20),(0,16)], [(4,12),(0+12,20),(0,16)], [(4,4),(0+4,16),(0,16)], [(4,4),(0+8,16),(0,16)], ]: mask.paste(Image.new('RGBA', size, (bg[0], bg[1], bg[2], 255)), offset) decor_mask.paste(Image.new('RGBA', size, (bg[0], bg[1], bg[2], 255)), (offset[0]+decor_offset[0],offset[1]+decor_offset[1])) mask.save(SKIN_MASK) decor_mask.save(SKIN_DECOR_MASK) create_mask() def apply_mask(skin_image, skin_mask): skin_image = Image.composite(skin_image, skin_mask, skin_mask) return skin_image def create_training_image(skin_image): # Mask out any areas not directly mapping to the head, arm, leg, or # torso portion of the character. skin_image_first_layer = apply_mask(skin_image,Image.open(SKIN_MASK)) skin_image_second_layer = apply_mask(skin_image,Image.open(SKIN_DECOR_MASK)) training_image = Image.new('RGBA', (IMAGE_WIDTH, IMAGE_HEIGHT), (*bg, 255)) scaled_skin_image = skin_image.resize((int(32 * SCALING_RATIO), int(32 * SCALING_RATIO)), resample=Image.BOX) #scaled_first_layer = skin_image_first_layer.resize((int(32 * SCALING_RATIO), int(32 * SCALING_RATIO)), # resample=Image.BOX) #scaled_second_layer = skin_image_second_layer.resize((int(32 * SCALING_RATIO), int(32 * SCALING_RATIO)), # resample=Image.BOX) training_image.paste(scaled_skin_image, (0,0)) #training_image.paste(scaled_first_layer, (0,0)) #training_image.paste(scaled_second_layer, (int(32 * SCALING_RATIO),0)) # Optimized: Use NumPy for transparency and dot drawing tr_arr = np.array(training_image) # Fill transparent background areas tr_arr[tr_arr[..., 3] == 0] = [*bg, 255] # Draw white dots for skin transparency skin_arr = np.array(skin_image) y_indices, x_indices = np.where(skin_arr[..., 3] == 0) for x, y in zip(x_indices, y_indices): cx = int(int(x * SCALING_RATIO / 2) + SCALING_RATIO // 4) cy = int(int(y * SCALING_RATIO / 2) + SCALING_RATIO // 4) # Apply 2x2 white dot tr_arr[cy-1:cy+1, cx-1:cx+1] = [255, 255, 255, 255] training_image = Image.fromarray(tr_arr) # skin_image -> np.ndarray r_scale1 = 0.22 r_scale2 = 0.28 r_scale3 = 0.09 r_scale4 = 0.25 full_part = ['head','body','left_arm','right_arm','left_leg','right_leg'] limbs_offset = 0.8 default_pos_args = { 'head': (0, 28, 0), 'body': (0, 18, 0), 'right_arm': (-6 - limbs_offset, 18, 0), 'left_arm': (6 + limbs_offset, 18, 0), 'right_leg': (-2 - limbs_offset, 6, 0), 'left_leg': (2 + limbs_offset, 6, 0), } pos_args2 = { 'head': (0, 28, 0), 'body': (0, 18, 0), 'right_arm': (-6, 18 , 0), 'left_arm': (6, 18 , 0), 'right_leg': (-2, 6, 0), 'left_leg': (2, 6, 0), } for (ortho, s, look_at_y, core_display, decor_display, cam_front, offset,render_size, zoom, pos_args, walk) in [ (False, skin_image,12, full_part, full_part, (0.3, 0.4, 0.5), (int(IMAGE_WIDTH/2), 0), (int(IMAGE_WIDTH/2),int(IMAGE_HEIGHT/2*1.2)), r_scale1, default_pos_args, True), (False, skin_image,12,full_part,full_part, (0.0, 0, 0.5),(0, int(IMAGE_HEIGHT/2)), (int(IMAGE_WIDTH/5),int(IMAGE_HEIGHT/3)), 0.35, pos_args2, False), (False, skin_image,12,full_part,full_part, (-0.0, -0.0, -0.5),(0, int(IMAGE_HEIGHT*3/4)), (int(IMAGE_WIDTH/5),int(IMAGE_HEIGHT/3)), 0.35, pos_args2, False), (False,skin_image_first_layer,12, full_part, full_part, (0.3, 0.4, 0.5), (int(IMAGE_WIDTH/5), int(IMAGE_HEIGHT/2)), (int(IMAGE_WIDTH/5),int(IMAGE_HEIGHT/3)), r_scale2,default_pos_args, False), (False,skin_image_first_layer,12, full_part, full_part, (-0.5, -0.4, -0.5), (int(IMAGE_WIDTH/5), int(IMAGE_HEIGHT*3/4)), (int(IMAGE_WIDTH/5),int(IMAGE_HEIGHT/3)), r_scale2,default_pos_args, False), #left (False,skin_image,12, full_part, full_part, (0.3, 0.4, 0.5), (int(IMAGE_WIDTH*2/5), int(IMAGE_HEIGHT/2)), (int(IMAGE_WIDTH/5),int(IMAGE_HEIGHT/3)), r_scale2,default_pos_args, False), (False,skin_image,12, full_part, full_part, (-0.5, -0.4, -0.5), (int(IMAGE_WIDTH*2/5), int(IMAGE_HEIGHT*3/4)), (int(IMAGE_WIDTH/5),int(IMAGE_HEIGHT/3)), r_scale2,default_pos_args, False), # right (False,skin_image, 12,full_part, full_part, (0.3, -0.4, 0.5),(int(IMAGE_WIDTH*3/5), int(IMAGE_HEIGHT/2)), (int(IMAGE_WIDTH/5),int(IMAGE_HEIGHT/3)), r_scale2,default_pos_args, False), (False,skin_image, 12,full_part, full_part, (-0.5, 0.4, -0.5),(int(IMAGE_WIDTH*3/5), int(IMAGE_HEIGHT*3/4)), (int(IMAGE_WIDTH/5),int(IMAGE_HEIGHT/3)), r_scale2,default_pos_args, False), # head (False,skin_image,28, ['head'], ['head'], (-0.3, -0.4, 0.5),(int(IMAGE_WIDTH*4/5), int(IMAGE_HEIGHT/2)), (int(IMAGE_WIDTH/5),int(IMAGE_HEIGHT/8)), r_scale3,default_pos_args, False), (False,skin_image,28, ['head'], ['head'], (0.3, -0.4, 0.5),(int(IMAGE_WIDTH*4/5), int(IMAGE_HEIGHT/2 + IMAGE_HEIGHT/8)), (int(IMAGE_WIDTH/5),int(IMAGE_HEIGHT/8)), r_scale3,default_pos_args, False), (False,skin_image,28, ['head'], ['head'], (-0.5, -0.4, -0.5),(int(IMAGE_WIDTH*4/5), int(IMAGE_HEIGHT/2+ IMAGE_HEIGHT*2/8)), (int(IMAGE_WIDTH/5),int(IMAGE_HEIGHT/8)), r_scale3,default_pos_args, False), (False,skin_image,28, ['head'], ['head'], (0.5, -0.4, -0.5),(int(IMAGE_WIDTH*4/5), int(IMAGE_HEIGHT/2+ IMAGE_HEIGHT*3/8)), (int(IMAGE_WIDTH/5),int(IMAGE_HEIGHT/8)), r_scale3,default_pos_args, False), (False,skin_image,12, ['body'], ['body'], (0.3, 0.4, 0.5), (int(IMAGE_WIDTH*3/4*1.05), int(IMAGE_HEIGHT*1/5)), (int(IMAGE_WIDTH/4),int(IMAGE_HEIGHT/2)), r_scale4,default_pos_args, False), (False,skin_image,12, ['body'], ['body'], (-0.3, 0.4, 0.5), (int(IMAGE_WIDTH*3/4*1.05), int(-IMAGE_HEIGHT*1/8)), (int(IMAGE_WIDTH/4),int(IMAGE_HEIGHT/2)), r_scale4,default_pos_args, False), (False,skin_image,22, [], ['head'], (0.3, 0.3, 0), (int(IMAGE_WIDTH*1/2*0.95), int(IMAGE_HEIGHT*1/60)), (int(IMAGE_WIDTH/5),int(IMAGE_HEIGHT/4.5)), r_scale4,default_pos_args, False), (False,skin_image,22, [], ['head'], (-0.3, 0.3, 0), (int(IMAGE_WIDTH*1/2*0.95), int(IMAGE_HEIGHT*20/60)), (int(IMAGE_WIDTH/5),int(IMAGE_HEIGHT/4.5)), r_scale4,default_pos_args, False), ]: # Optimized: Get image directly from memory instead of UUID-named temp files img_np = mc_render.render_skin( skin=np.array(s), output_size=render_size, cam_front=cam_front, look_at_y=look_at_y, use_voxels=True, ortho=ortho, core_display=core_display, decor_display=decor_display, show_wireframe=False, pos_args=pos_args, rot_args={ 'rot_head': (0,0,0), 'rot_arm_right': (-30,0,0), 'rot_arm_left': (30,0,0), 'rot_leg_right': (30,0,0), 'rot_leg_left': (-30,0,0), } if walk else { 'rot_head': (0,0,0), 'rot_arm_right': (0,0,0), 'rot_arm_left': (0,0,0), 'rot_leg_right': (0,0,0), 'rot_leg_left': (0,0,0), },save_path=None, transparent_background=True, zoom=zoom, light=True) if img_np is not None: x = Image.fromarray(img_np) training_image.paste(x, offset, x) return training_image def check_skin(img): is_alex = False if img.size != (64, 64): print('size invalid') return False,is_alex skin_image_first_layer = apply_mask(img,Image.open(SKIN_MASK)) # Calculate the total number of opaque pixels count = 0 count2 = 0 for x in range(64): for y in range(64): if img.getpixel((x, y))[3] == 255: count2 +=1 if skin_image_first_layer.getpixel((x, y))[3] == 255: count += 1 if count == 1632 - 4*2*2-12*2*2: is_alex = True if count != 1632 and (not is_alex): print('count invalid', count) return False,is_alex if count2 == 64*64 and (not is_alex): print('count2 invalid') return False,is_alex return True,is_alex def ensure_valid_skin(skin_image): skin_image_first_layer = Image.open(SKIN_MASK) skin_image_second_layer = Image.open(SKIN_DECOR_MASK) # Convert all semi-transparent to opaque for x in range(skin_image.width): for y in range(skin_image.height): if skin_image.getpixel((x, y))[3] != 0 and skin_image.getpixel((x, y))[3] != 255: skin_image.putpixel((x, y), (skin_image.getpixel((x, y))[0], skin_image.getpixel((x, y))[1], skin_image.getpixel((x, y))[2], 255)) # Ignore other pixels in the 1st and 2nd layers of the skin if skin_image_first_layer.getpixel((x, y))[3] != 255 and skin_image_second_layer.getpixel((x, y))[3] != 255: skin_image.putpixel((x, y), (bg[0],bg[1],bg[2], 255)) return skin_image def build_target_img(input_path, output_path): # This might fail if dirs not created yet # But main process creates them try: if not os.path.exists(input_path): print('not exists', input_path) return # Ensure subdir exists in output os.makedirs(os.path.dirname(output_path), exist_ok=True) if os.path.exists(output_path): print('exists target', output_path) return print(f"Processing {input_path}") skin_image = Image.open(input_path) skin_image = skin_image.convert('RGBA') valid,is_alex = check_skin(skin_image) if not valid: print('invalid', input_path) return if is_alex: skin_image = alice_to_steve(skin_image) skin_image = ensure_valid_skin(skin_image) skin_image = resolve_voxel_consistency(skin_image) training_image = create_training_image(skin_image) training_image.save(output_path) except Exception as e: print(f"Error processing {input_path}: {e}") if __name__ == '__main__': parser = argparse.ArgumentParser(description="Process a single Minecraft skin to a target image") parser.add_argument("input_path", help="Path to input skin image") parser.add_argument("output_path", help="Path to output target image") args = parser.parse_args() build_target_img(args.input_path, args.output_path)