Files
harbour-henflow/icons/make_icons.py
T

85 lines
2.7 KiB
Python

#!/usr/bin/env python3
"""Genera le icone dell'app in stile Sailfish OS a partire dall'artwork
originale dell'app (assets/icon_source.png: gallina su un uovo con una foglia).
Sfondo: rettangolo arrotondato con gradiente bianco -> verde tenue.
Soggetto: ritagliato dallo sfondo bianco PER CONNESSIONE AI BORDI (flood fill),
cosi' il bianco interno dell'uovo non viene forato.
Dimensioni: 86, 108, 128, 172 (struttura standard hicolor).
"""
import os
from PIL import Image, ImageDraw, ImageFilter
SRC = "assets/icon_source.png"
C_TOP = (255, 255, 255)
C_BOT = (233, 243, 226) # #E9F3E2 verde tenue
def gradient(size, c1, c2):
img = Image.new("RGB", (size, size))
d = ImageDraw.Draw(img)
for y in range(size):
t = y / (size - 1)
d.line([(0, y), (size, y)],
fill=(int(c1[0] + (c2[0] - c1[0]) * t),
int(c1[1] + (c2[1] - c1[1]) * t),
int(c1[2] + (c2[2] - c1[2]) * t)))
return img
def subject_with_alpha():
"""Ritorna l'artwork RGBA con lo sfondo bianco reso trasparente."""
im = Image.open(SRC).convert("RGB")
w, h = im.size
# flood fill dai quattro angoli sullo sfondo bianco connesso ai bordi
marked = im.copy()
for xy in ((0, 0), (w - 1, 0), (0, h - 1), (w - 1, h - 1)):
ImageDraw.floodfill(marked, xy, (255, 0, 255), thresh=60)
alpha = Image.new("L", (w, h), 255)
px_m, px_a = marked.load(), alpha.load()
for y in range(h):
for x in range(w):
if px_m[x, y] == (255, 0, 255):
px_a[x, y] = 0
# bordo morbido: piccola sfocatura + soglia, per togliere i frastagli
# del JPEG lungo i contorni
alpha = alpha.filter(ImageFilter.GaussianBlur(0.8)).point(lambda v: 0 if v < 110 else 255)
out = im.convert("RGBA")
out.putalpha(alpha)
return out
def draw_icon(size, subject):
m = size * 0.045
r = size * 0.22
out = Image.new("RGBA", (size, size), (0, 0, 0, 0))
grad = gradient(size, C_TOP, C_BOT).convert("RGBA")
mask = Image.new("L", (size, size), 0)
ImageDraw.Draw(mask).rounded_rectangle([m, m, size - m, size - m],
radius=r, fill=255)
out.paste(grad, (0, 0), mask)
# soggetto centrato, all'82% dell'area utile
box = size - 2 * m
art = subject.copy()
art.thumbnail((int(box * 0.82), int(box * 0.82)), Image.LANCZOS)
out.alpha_composite(art, (int((size - art.width) / 2), int((size - art.height) / 2)))
return out
def main():
subject = subject_with_alpha()
for s in (86, 108, 128, 172):
os.makedirs(f"icons/{s}x{s}", exist_ok=True)
draw_icon(s, subject).save(f"icons/{s}x{s}/harbour-henflow.png")
print("icone scritte: 86, 108, 128, 172")
if __name__ == "__main__":
main()