| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132 |
- """Slice the generated sheet and register the independent DeepSeek character.
- Only packaging operations (crop, uniform resize and PNG encoding) are performed;
- the poses and animation artwork come from the image generation tool.
- """
- import hashlib
- from copy import deepcopy
- import json
- from pathlib import Path
- import numpy as np
- from PIL import Image, ImageDraw
- ROOT = Path(__file__).resolve().parents[2]
- HERE = Path(__file__).resolve().parent
- PREFIX = 'deepseek_bluewhale'
- CHARACTER = 'CharacterDeepseekBlueWhale'
- HERO = 'DeepseekBlueWhale'
- DIRECTIONS = ('R', 'U', 'L', 'D')
- def write_json(path, value):
- path.parent.mkdir(parents=True, exist_ok=True)
- path.write_text(json.dumps(value, ensure_ascii=False, indent=4) + '\n', encoding='utf-8')
- def main():
- sheet = Image.open(HERE / 'source_sheet.png').convert('RGBA')
- alpha = np.asarray(sheet)[:, :, 3]
- width, height = sheet.size
- row_edges = [0]
- for row in range(1, 4):
- expected = round(height * row / 4)
- low, high = max(0, expected - 18), min(height, expected + 18)
- row_edges.append(low + int(np.argmin((alpha[low:high] > 64).sum(axis=1))))
- row_edges.append(height)
- cells = []
- for row in range(4):
- y0, y1 = row_edges[row:row + 2]
- column_edges = [0]
- for column in range(1, 7):
- expected = round(width * column / 7)
- low, high = max(0, expected - 18), min(width, expected + 18)
- projection = (alpha[y0:y1, low:high] > 64).sum(axis=0)
- column_edges.append(low + int(np.argmin(projection)))
- column_edges.append(width)
- poses = []
- for col in range(7):
- cell = sheet.crop((column_edges[col], y0, column_edges[col + 1], y1))
- bounds = cell.getchannel('A').point(lambda value: 255 if value > 64 else 0).getbbox()
- if bounds is None:
- raise ValueError(f'Empty sprite at {row}, {col}')
- poses.append(cell.crop(bounds))
- cells.append(poses)
- # One uniform scale for all 28 poses; do not independently stretch each pose.
- scale = min(58 / max(im.height for row in cells for im in row),
- 60 / max(im.width for row in cells for im in row))
- output = ROOT / 'res/img/cloth'
- character_path = ROOT / f'game/frame/character/{CHARACTER}.json'
- hero_path = ROOT / f'game/hero/{HERO}.json'
- if character_path.exists() or hero_path.exists() or list(output.glob(PREFIX + '_*.png')):
- raise FileExistsError('Character already exists; do not overwrite edited game assets.')
- assets, textures = [], []
- for row, direction in enumerate(DIRECTIONS):
- poses = []
- for col, image in enumerate(cells[row]):
- resized = image.resize((round(image.width * scale), round(image.height * scale)), Image.Resampling.LANCZOS)
- canvas = Image.new('RGBA', (64, 64))
- canvas.alpha_composite(resized, ((64 - resized.width) // 2, 60 - resized.height))
- motion, frame = ('stand', 0) if col == 0 else ('walk', col - 1)
- filename = f'{PREFIX}_{motion}_{row}_{frame}.png'
- canvas.save(output / filename)
- assets.append({'path': str((output / filename).relative_to(ROOT)),
- 'direction': direction, 'motion': motion, 'frame': frame,
- 'size': [64, 64], 'visible_bounds': canvas.getbbox(),
- 'sha256': hashlib.sha256((output / filename).read_bytes()).hexdigest()})
- poses.append(canvas)
- textures.append(poses)
- data = {'NAME': CHARACTER, 'HEAD_POS': [-17, -1]}
- for index, direction in enumerate(DIRECTIONS):
- for standing in (True, False):
- motion, count = ('stand', 1) if standing else ('walk', 6)
- data[('STAND_' if standing else '') + direction] = {
- 'Len': count, 'Cx': -32, 'Cy': -53,
- 'Cloth': {'IMG': [f'{PREFIX}_{motion}_{index}_{frame}.png' for frame in range(count)],
- 'CX': [0] * count, 'CY': [0] * count}}
- # Reuse the front standing image as a safe defeat-state fallback.
- data['LOSE'] = deepcopy(data['STAND_D'])
- data['INTERVAL'] = 100
- write_json(character_path, data)
- hero = json.loads((ROOT / 'game/hero/Cola.json').read_text(encoding='utf-8-sig'))
- hero.update(name=HERO, character=CHARACTER, icon_img='icon_deepseek_bluewhale')
- hero['decorations']['bomb_skin'] = 'bomb1'
- write_json(hero_path, hero)
- icon = textures[3][0].crop(textures[3][0].getbbox())
- icon.thumbnail((53, 53), Image.Resampling.LANCZOS)
- icon_canvas = Image.new('RGBA', (55, 55))
- icon_canvas.alpha_composite(icon, ((55 - icon.width) // 2, 55 - icon.height))
- icon_canvas.save(ROOT / 'res/img/ui/game/icon_deepseek_bluewhale.png')
- # Enlarged contact sheet and a 40 px grid animation for reviewing the result.
- contact = Image.new('RGB', (7 * 168, 4 * 216), '#edf3f8')
- draw = ImageDraw.Draw(contact)
- for row in range(4):
- for col in range(7):
- x, y = col * 168, row * 216
- contact.paste(textures[row][col].resize((160, 160), Image.Resampling.NEAREST),
- (x + 4, y + 24), textures[row][col].resize((160, 160), Image.Resampling.NEAREST))
- draw.text((x + 8, y + 190), DIRECTIONS[row] + (' stand' if col == 0 else f' walk {col - 1}'), fill='#203349')
- contact.save(HERE / 'contact_sheet.png')
- sequence = []
- for frame in range(6):
- scene = Image.new('RGB', (400, 180), '#263447')
- draw = ImageDraw.Draw(scene)
- for gx in range(0, 401, 40):
- draw.line((gx, 0, gx, 180), fill='#526174')
- for gy in range(0, 181, 40):
- draw.line((0, gy, 400, gy), fill='#526174')
- for row in range(4):
- sprite = textures[row][frame + 1]
- scene.paste(sprite, (28 + row * 96, 47), sprite)
- draw.text((44 + row * 96, 130), DIRECTIONS[row], fill='white')
- sequence.append(scene.resize((800, 360), Image.Resampling.NEAREST))
- sequence[0].save(HERE / 'walk_preview.gif', save_all=True, append_images=sequence[1:], duration=100, loop=0)
- write_json(HERE / 'asset_manifest.json', {'hero': HERO, 'character': CHARACTER,
- 'source_sheet': 'source_sheet.png', 'source_size': list(sheet.size), 'row_edges': row_edges,
- 'uniform_scale': scale, 'canvas': [64, 64], 'foot_baseline': 60,
- 'character_offset': [-32, -53], 'assets': assets})
- print(f'Created {len(assets)} frames, character JSON, hero JSON, icon and previews.')
- if __name__ == '__main__':
- main()
|