Fit slightly rectangular ore layers without changing their proportions

This commit is contained in:
unimatrixgit 2026-12-04 00:06:24 +01:00
commit 2077c17b23
2 changed files with 11 additions and 7 deletions

View file

@ -6,7 +6,7 @@ import json
from pathlib import Path
import numpy as np
from PIL import Image, ImageDraw, ImageEnhance, ImageFilter
from PIL import Image, ImageDraw, ImageEnhance, ImageFilter, ImageOps
SIZE = 256
EDGE = 12
@ -80,13 +80,17 @@ def seamless(image):
def source_tile(path, mode):
with Image.open(path) as image:
if image.width != image.height or image.width < SIZE:
raise ValueError(f"{path.name} must be a square image of at least {SIZE} pixels")
if min(image.size) < SIZE:
raise ValueError(f"{path.name} must be at least {SIZE} pixels on each side")
if mode == "RGBA":
alpha = image.convert("RGBA").getchannel("A")
if alpha.getextrema()[0] != 0 or alpha.getextrema()[1] < 128:
layer = image.convert("RGBA")
minimum, maximum = layer.getchannel("A").getextrema()
if minimum != 0 or maximum < 128:
raise ValueError(f"{path.name} requires a transparent background and visible deposits")
return image.convert(mode).resize((SIZE, SIZE), Image.Resampling.LANCZOS)
return ImageOps.pad(layer, (SIZE, SIZE), method=Image.Resampling.LANCZOS, color=(0, 0, 0, 0))
if image.width != image.height:
raise ValueError(f"{path.name} must be a square albedo")
return image.convert("RGB").resize((SIZE, SIZE), Image.Resampling.LANCZOS)
def masonry(image, columns, rows, stagger):

View file

@ -30,7 +30,7 @@ class StoneFamilyTest(unittest.TestCase):
inputs.mkdir()
for name in compiler.CORE + compiler.OPTIONAL:
Image.new("RGB", (256, 256), (122, 118, 110)).save(inputs / f"{name}-source.png")
ore = Image.new("RGBA", (256, 256))
ore = Image.new("RGBA", (260, 256))
ImageDraw.Draw(ore).rectangle((104, 104, 151, 151), fill=(200, 30, 55, 255))
for name in compiler.MINERALS + ("quartz", "ancient_debris"):
ore.save(inputs / f"{name}-deposit.png")