File size: 12,574 Bytes
5bbe62a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
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)