ThreeKingdomsSlingshot/Tools/tavern_floor.py

108 lines
5.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""주점 마루바닥 다시 깔기(2026-11-18 PD 「1막 주점 마루바닥 텍스쳐가 너무 커서 바닥으로 보이지 않음 · 패턴 크기를 줄여 자연스러운 마루바닥」).
원본 bg_ev_tavern.png(2000×3200 · 판 20×32칸 = 1칸 100 px)의 벽 · 술독 · 등 · 문 · 긴 의자는 그대로 두고 바닥 널판만:
- 원본 널판 6장(폭 약 225 px · 판 끝까지 한 장)의 결을 잘라 1/3 로 줄인 널판(폭 약 75 px = 0.75칸 · 길이 2.4~4.8칸 · 이음 엇갈림)으로 다시 깜
- 원본 바닥의 밝기(위 벽 그늘 · 의자 그림자 · 가운데 밝음)는 크게 흐린 밝기 지도로 곱해 그대로 살림
사용: python -I tavern_floor.py <원본> <출력> [씨앗]
"""
import random, sys
from PIL import Image, ImageDraw, ImageFilter, ImageChops, ImageStat
SRC, OUT = sys.argv[1], sys.argv[2]
SEED = int(sys.argv[3]) if len(sys.argv) > 3 else 11
random.seed(SEED)
X0, X1, Y0, Y1 = 276, 1768, 292, 2894 # 바닥 칸(원본 실측: 세로 · 가로 밝기 골)
SEAMS = [318, 532, 766, 988, 1225, 1474, 1683] # 원본 널판 이음선
BENCH = [(430, 140), (680, 160), (680, 340), (430, 1110), (135, 1075), (150, 990)] # 바닥 위 긴 의자(남김)
SCALE = 1 / 3 # 널판 결 축소
COLS = 20 # 널판 줄 수(1492 / 20 ≈ 75 px)
LEN_MIN, LEN_MAX = 240, 480 # 널판 길이(px · 2.4~4.8칸)
SEAM_W, JOINT_W = 3, 3
src = Image.open(SRC).convert("RGB")
W, H = X1 - X0, Y1 - Y0
# 1) 원본 널판 결(밝기 흐름을 뺀 무늬만) — 이음선 사이 · 가운데 높이(밝은 곳)
strips = []
for a, b in zip(SEAMS[:-1], SEAMS[1:]):
s = src.crop((a + 8, 900, b - 8, 2400))
low = s.resize((max(1, s.width // 16), max(1, s.height // 16)), Image.BOX).resize(s.size, Image.BICUBIC)
mean = ImageStat.Stat(s).mean
flat = ImageChops.multiply(ImageChops.divide(s, low, scale=1 / 0.5), Image.new("RGB", s.size, tuple(int(m * 0.5) for m in mean))) if False else None
# 나눗셈 근사: s / low × 평균 (채널별)
sp, lp = s.load(), low.load()
out = Image.new("RGB", s.size)
op = out.load()
for y in range(s.height):
for x in range(s.width):
r, g, bb = sp[x, y]
lr, lg, lb = lp[x, y]
op[x, y] = (min(255, int(r * mean[0] / max(1, lr))), min(255, int(g * mean[1] / max(1, lg))), min(255, int(bb * mean[2] / max(1, lb))))
out = out.resize((max(8, int(out.width * SCALE)), int(out.height * SCALE)), Image.LANCZOS)
strips.append(out)
gm = [sum(ImageStat.Stat(s).mean[i] for s in strips) / len(strips) for i in range(3)]
for i, s in enumerate(strips):
m = ImageStat.Stat(s).mean
strips[i] = Image.merge('RGB', [ch.point(lambda v, f=gm[j] / max(1, m[j]): min(255, int(v * f))) for j, ch in enumerate(s.split())])
# 이음선 색(원본 골 색) — 가운데 높이 평균
seam_col = tuple(int(c) for c in ImageStat.Stat(src.crop((SEAMS[2] - 2, 1200, SEAMS[2] + 3, 2000))).mean)
hi_col = tuple(min(255, int(c * 1.08)) for c in ImageStat.Stat(src.crop((SEAMS[2] + 10, 1200, SEAMS[2] + 40, 2000))).mean)
# 2) 새 바닥(평평한 밝기)
floor = Image.new("RGB", (W, H))
d = ImageDraw.Draw(floor)
edges = [round(i * W / COLS) for i in range(COLS + 1)]
for c in range(COLS):
cx0, cx1 = edges[c], edges[c + 1]
cw = cx1 - cx0
y = -random.randint(0, LEN_MAX) # 줄마다 이음 엇갈림
while y < H:
L = random.randint(LEN_MIN, LEN_MAX)
st = random.choice(strips)
top = random.randint(0, max(0, st.height - L))
piece = st.crop((0, top, st.width, top + min(L, st.height)))
if piece.height < L:
piece = piece.resize((piece.width, L), Image.BICUBIC)
piece = piece.resize((cw, L), Image.LANCZOS)
if random.random() < 0.5:
piece = piece.transpose(Image.FLIP_TOP_BOTTOM)
if random.random() < 0.5:
piece = piece.transpose(Image.FLIP_LEFT_RIGHT)
k = random.uniform(0.96, 1.03) # 널판마다 살짝 다른 톤
piece = piece.point(lambda v: min(255, int(v * k)))
floor.paste(piece, (cx0, y))
jy = y + L
if 0 <= jy < H: # 맞댄 이음
d.rectangle((cx0, jy - JOINT_W // 2, cx1 - 1, jy + JOINT_W // 2), fill=seam_col)
y = jy
# 줄 사이 이음선 + 오른쪽 밝은 테
if c > 0:
d.rectangle((cx0 - SEAM_W // 2, 0, cx0 + SEAM_W // 2, H), fill=seam_col)
d.line((cx0 + SEAM_W // 2 + 1, 0, cx0 + SEAM_W // 2 + 1, H), fill=hi_col, width=1)
floor = floor.filter(ImageFilter.GaussianBlur(0.6))
# 3) 원본 밝기 지도(크게 흐림 · 위 벽 그늘 · 의자 그림자 · 가운데 밝음) 곱하기
orig = src.crop((X0, Y0, X1, Y1)).convert("L")
small = orig.resize((W // 8, H // 8), Image.BOX).filter(ImageFilter.GaussianBlur(12))
lum = small.resize((W, H), Image.BICUBIC)
ref = ImageStat.Stat(orig.crop((W // 4, H // 3, 3 * W // 4, 2 * H // 3))).mean[0]
lum = lum.point(lambda v: max(0, min(255, int(v / ref * 200)))) # 200 = 밝기 1.0 근처
lit = ImageChops.multiply(floor, Image.merge("RGB", (lum, lum, lum)))
lit = lit.point(lambda v: min(255, int(v * 255 / 200 * 1.05)))
# 4) 붙이기 — 바닥 칸(가장자리 살짝 흐림) − 긴 의자(조금 넓혀 테 보존)
mask = Image.new("L", src.size, 0)
md = ImageDraw.Draw(mask)
md.rectangle((X0 + 3, Y0 + 3, X1 - 3, Y1 - 3), fill=255)
bench = Image.new("L", src.size, 0)
ImageDraw.Draw(bench).polygon(BENCH, fill=255)
bench = bench.filter(ImageFilter.MaxFilter(7))
mask = ImageChops.subtract(mask, bench).filter(ImageFilter.GaussianBlur(2.5))
full = src.copy()
layer = src.copy()
layer.paste(lit, (X0, Y0))
full = Image.composite(layer, src, mask)
full.save(OUT, optimize=True)
print("ok", OUT, "plank px", W / COLS, "seam", seam_col)