Spaces:
Sleeping
Sleeping
improved quality
Browse files
app.py
CHANGED
|
@@ -19,10 +19,14 @@ def center_crop_square(img: Image.Image) -> Image.Image:
|
|
| 19 |
def generate_favicons(image, name, short_name, theme_color, background_color, tile_color):
|
| 20 |
if image is None:
|
| 21 |
return None
|
| 22 |
-
|
| 23 |
img = Image.open(image).convert("RGBA")
|
| 24 |
img = center_crop_square(img)
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
if not name:
|
| 27 |
name = "My App"
|
| 28 |
if not short_name:
|
|
@@ -34,6 +38,7 @@ def generate_favicons(image, name, short_name, theme_color, background_color, ti
|
|
| 34 |
if not tile_color:
|
| 35 |
tile_color = theme_color
|
| 36 |
|
|
|
|
| 37 |
icon_specs = [
|
| 38 |
(16, "favicon-16x16.png"),
|
| 39 |
(32, "favicon-32x32.png"),
|
|
@@ -54,6 +59,7 @@ def generate_favicons(image, name, short_name, theme_color, background_color, ti
|
|
| 54 |
(256, "android-icon-256x256.png"),
|
| 55 |
(384, "android-icon-384x384.png"),
|
| 56 |
(512, "android-icon-512x512.png"),
|
|
|
|
| 57 |
(150, "ms-icon-150x150.png"),
|
| 58 |
(32, "ms-icon-32x32.png"),
|
| 59 |
(70, "ms-icon-70x70.png"),
|
|
@@ -70,13 +76,13 @@ def generate_favicons(image, name, short_name, theme_color, background_color, ti
|
|
| 70 |
files[filename] = buf.getvalue()
|
| 71 |
|
| 72 |
# Apple touch icon
|
| 73 |
-
|
| 74 |
-
files["apple-touch-icon.png"] = files["apple-icon-180x180.png"]
|
| 75 |
|
| 76 |
# favicon.ico (multi-size)
|
| 77 |
-
ico_sizes = [
|
|
|
|
| 78 |
ico_buf = io.BytesIO()
|
| 79 |
-
|
| 80 |
files["favicon.ico"] = ico_buf.getvalue()
|
| 81 |
|
| 82 |
# manifest.json
|
|
@@ -86,7 +92,7 @@ def generate_favicons(image, name, short_name, theme_color, background_color, ti
|
|
| 86 |
"sizes": f"{sz}x{sz}",
|
| 87 |
"type": "image/png"
|
| 88 |
}
|
| 89 |
-
for sz, fn in icon_specs if fn.endswith(".png")
|
| 90 |
]
|
| 91 |
icons.append({
|
| 92 |
"src": "android-icon-512x512.png",
|
|
@@ -158,9 +164,9 @@ demo = gr.Interface(
|
|
| 158 |
gr.ColorPicker(label="Tile Color", value="#ffffff"),
|
| 159 |
],
|
| 160 |
outputs=gr.File(label="Download Favicons ZIP"),
|
| 161 |
-
title="Favicon Generator",
|
| 162 |
-
description="Upload
|
| 163 |
)
|
| 164 |
|
| 165 |
if __name__ == "__main__":
|
| 166 |
-
demo.launch(
|
|
|
|
| 19 |
def generate_favicons(image, name, short_name, theme_color, background_color, tile_color):
|
| 20 |
if image is None:
|
| 21 |
return None
|
| 22 |
+
|
| 23 |
img = Image.open(image).convert("RGBA")
|
| 24 |
img = center_crop_square(img)
|
| 25 |
|
| 26 |
+
# Ensure a very high-res base image
|
| 27 |
+
base_size = max(1024, img.width, img.height)
|
| 28 |
+
img = img.resize((base_size, base_size), Image.LANCZOS)
|
| 29 |
+
|
| 30 |
if not name:
|
| 31 |
name = "My App"
|
| 32 |
if not short_name:
|
|
|
|
| 38 |
if not tile_color:
|
| 39 |
tile_color = theme_color
|
| 40 |
|
| 41 |
+
# Icon specs: regular + ultra-high for Apple/Android
|
| 42 |
icon_specs = [
|
| 43 |
(16, "favicon-16x16.png"),
|
| 44 |
(32, "favicon-32x32.png"),
|
|
|
|
| 59 |
(256, "android-icon-256x256.png"),
|
| 60 |
(384, "android-icon-384x384.png"),
|
| 61 |
(512, "android-icon-512x512.png"),
|
| 62 |
+
(1024, "apple-icon-1024x1024.png"), # Retina / App Store
|
| 63 |
(150, "ms-icon-150x150.png"),
|
| 64 |
(32, "ms-icon-32x32.png"),
|
| 65 |
(70, "ms-icon-70x70.png"),
|
|
|
|
| 76 |
files[filename] = buf.getvalue()
|
| 77 |
|
| 78 |
# Apple touch icon
|
| 79 |
+
files["apple-touch-icon.png"] = files.get("apple-icon-180x180.png", img.resize((180,180), Image.LANCZOS).tobytes())
|
|
|
|
| 80 |
|
| 81 |
# favicon.ico (multi-size)
|
| 82 |
+
ico_sizes = [16, 32, 48, 64, 128, 256]
|
| 83 |
+
ico_images = [img.resize((s, s), Image.LANCZOS) for s in ico_sizes]
|
| 84 |
ico_buf = io.BytesIO()
|
| 85 |
+
ico_images[0].save(ico_buf, format='ICO', sizes=[(s, s) for s in ico_sizes])
|
| 86 |
files["favicon.ico"] = ico_buf.getvalue()
|
| 87 |
|
| 88 |
# manifest.json
|
|
|
|
| 92 |
"sizes": f"{sz}x{sz}",
|
| 93 |
"type": "image/png"
|
| 94 |
}
|
| 95 |
+
for sz, fn in icon_specs if fn.endswith(".png") and sz <= 512
|
| 96 |
]
|
| 97 |
icons.append({
|
| 98 |
"src": "android-icon-512x512.png",
|
|
|
|
| 164 |
gr.ColorPicker(label="Tile Color", value="#ffffff"),
|
| 165 |
],
|
| 166 |
outputs=gr.File(label="Download Favicons ZIP"),
|
| 167 |
+
title="Ultra High-Quality Favicon Generator",
|
| 168 |
+
description="Upload a high-res image (≥1024px) and generate ultra-high quality favicons for Retina and modern devices."
|
| 169 |
)
|
| 170 |
|
| 171 |
if __name__ == "__main__":
|
| 172 |
+
demo.launch()
|