完成六区域银河城战役与16帧美术资源
This commit is contained in:
@@ -0,0 +1,86 @@
|
||||
"""Remove chroma green and normalize a generated 4x2 dialogue portrait atlas."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
from PIL import Image, ImageChops, ImageDraw
|
||||
|
||||
|
||||
def bounds(length: int, index: int, count: int) -> tuple[int, int]:
|
||||
return round(index * length / count), round((index + 1) * length / count)
|
||||
|
||||
|
||||
def remove_green(image: Image.Image) -> Image.Image:
|
||||
rgba = image.convert("RGBA")
|
||||
pixels = []
|
||||
for red, green, blue, _ in rgba.get_flattened_data():
|
||||
dominance = green - max(red, blue)
|
||||
alpha = max(0, min(255, round((150 - dominance) * 255 / 105)))
|
||||
if alpha < 250:
|
||||
green = min(green, max(red, blue) + 12)
|
||||
pixels.append((red, green, blue, alpha))
|
||||
rgba.putdata(pixels)
|
||||
return rgba
|
||||
|
||||
|
||||
def normalize_cell(cell: Image.Image, size: int) -> Image.Image:
|
||||
alpha = cell.getchannel("A")
|
||||
box = alpha.getbbox()
|
||||
output = Image.new("RGBA", (size, size), (0, 0, 0, 0))
|
||||
if not box:
|
||||
return output
|
||||
subject = cell.crop(box)
|
||||
max_width, max_height = round(size * 0.94), round(size * 0.96)
|
||||
scale = min(max_width / subject.width, max_height / subject.height)
|
||||
subject = subject.resize(
|
||||
(max(1, round(subject.width * scale)), max(1, round(subject.height * scale))),
|
||||
Image.Resampling.LANCZOS,
|
||||
)
|
||||
x = (size - subject.width) // 2
|
||||
y = size - subject.height - round(size * 0.015)
|
||||
output.alpha_composite(subject, (x, y))
|
||||
return output
|
||||
|
||||
|
||||
def make_preview(atlas: Image.Image, output: Path, size: int) -> None:
|
||||
tile = 24
|
||||
preview = Image.new("RGBA", atlas.size, (23, 25, 39, 255))
|
||||
draw = ImageDraw.Draw(preview)
|
||||
for y in range(0, preview.height, tile):
|
||||
for x in range(0, preview.width, tile):
|
||||
if (x // tile + y // tile) % 2:
|
||||
draw.rectangle((x, y, x + tile - 1, y + tile - 1), fill=(36, 39, 57, 255))
|
||||
preview.alpha_composite(atlas)
|
||||
preview = preview.resize((size * 2, size), Image.Resampling.LANCZOS)
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
preview.save(output, optimize=True, compress_level=9)
|
||||
|
||||
|
||||
def prepare(source_path: Path, output: Path, preview: Path, frame_size: int) -> None:
|
||||
source = Image.open(source_path).convert("RGBA")
|
||||
atlas = Image.new("RGBA", (frame_size * 4, frame_size * 2), (0, 0, 0, 0))
|
||||
for row in range(2):
|
||||
top, bottom = bounds(source.height, row, 2)
|
||||
for column in range(4):
|
||||
left, right = bounds(source.width, column, 4)
|
||||
cell = remove_green(source.crop((left, top, right, bottom)))
|
||||
atlas.alpha_composite(normalize_cell(cell, frame_size), (column * frame_size, row * frame_size))
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
atlas.save(output, optimize=True, compress_level=9)
|
||||
make_preview(atlas, preview, frame_size)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--source", type=Path, required=True)
|
||||
parser.add_argument("--output", type=Path, required=True)
|
||||
parser.add_argument("--preview", type=Path, required=True)
|
||||
parser.add_argument("--frame-size", type=int, default=320)
|
||||
args = parser.parse_args()
|
||||
prepare(args.source, args.output, args.preview, args.frame_size)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user