Improve icon generation and whitespace detection

This commit is contained in:
LoafyLemon 2024-10-16 20:59:14 +01:00
parent 59788d5f16
commit 65690ca90c
3 changed files with 35 additions and 21 deletions

View File

@ -182,27 +182,27 @@ init python:
matrix = SaturationMatrix(0.0)
sprites = [i for i in self.build_image(hash, matrix=matrix) if not i[0] == "mask"]
try:
bounds = self.get_layers(hash).get("outline", [sprites[0][1]])[0]
except IndexError:
print(f"Missing textures:\n{self.__repr__()}")
return Text(f"TexErr\n{{color=#00ffff}}{{size=-6}}ID:{self.id}{{/size}}{{/color}}", color="#ff0000")
icon_sprite = Fixed(*[i[1] for i in sprites], fit_first=True)
sprites.extend(self.char.body.build_image(self.char.body._hash, matrix=matrix))
sprites.sort(key=itemgetter(2))
wmax, hmax = self.sizes
wmin = hmin = 256
wmin = hmin = 128
padding = 32
x, y, w, h = crop_whitespace(bounds)
xoffset, yoffset = w/2, h/2
x, y, w, h = crop_whitespace_surface(icon_sprite)
xoffset, yoffset = w//2, h//2
w = h = max(w, h)
w = h = max(w, h, wmin, hmin)
if w == 0 or h == 0:
return Null()
w = max(wmin, w + w/2)
h = max(hmin, h + h/2)
w = max(wmin, w + padding)
h = max(hmin, h + padding)
x = clamp( (x - w/2) + xoffset, 0, wmax)
y = clamp( (y - h/2) + yoffset, 0, hmax)
x = clamp( (x - w//2) + xoffset, 0, wmax)
y = clamp( (y - h//2) + yoffset, 0, hmax)
# Forbid exceeding the image height.
if y+h > hmax:

View File

@ -170,6 +170,8 @@ init python:
matrix = SaturationMatrix(0.0)
sprites = [i for i in self.build_image(hash, matrix=matrix) if not i[0] == "mask"]
icon_sprite = Fixed(*[i[1] for i in sprites], fit_first=True)
if not tracking_object is None:
sprites.extend([i for i in tracking_object.build_image(tracking_object._hash, matrix=matrix) if not i[0] == "mask"])
@ -178,18 +180,21 @@ init python:
bounds = self.get_layers(hash).get("outline", [sprites[0][1]])[0]
wmax, hmax = self.sizes
wmin = hmin = 96
wmin = hmin = 128
padding = 32
x, y, w, h = crop_whitespace(bounds)
xoffset, yoffset = w/2, h/2
x, y, w, h = crop_whitespace_surface(icon_sprite)
xoffset, yoffset = w//2, h//2
w = h = max(w, h)
w = h = max(w, h, wmin, hmin)
if w == 0 or h == 0:
return Null()
w = max(wmin, w + w/2)
h = max(hmin, h + h/2)
w = max(wmin, w + padding)
h = max(hmin, h + padding)
x = clamp( (x - w/2) + xoffset, 0, wmax)
y = clamp( (y - h/2) + yoffset, 0, hmax)
x = clamp( (x - w//2) + xoffset, 0, wmax)
y = clamp( (y - h//2) + yoffset, 0, hmax)
# Forbid exceeding the image height.
if y+h > hmax:

View File

@ -31,6 +31,15 @@ init python:
whitespace_dict[path] = tuple(map(int, box))
return box
def crop_whitespace_surface(displayable):
surf = renpy.render_to_surface(displayable, width=1010, height=1200)
size = surf.get_size()
box = tuple(surf.get_bounding_rect())
if size[0] != 1010:
ratio = size[0]/1010
box = tuple(v//ratio for v in box)
return box
def crop_image_zoom(path, xsize, ysize, grayscale=False):
x, y, w, h = crop_whitespace(path)
matrix = SaturationMatrix(0)*BrightnessMatrix(-0.25)*OpacityMatrix(0.5) if grayscale else None