From beb81771a8b6e5d067bd8baa3d20c0c1cdae868e Mon Sep 17 00:00:00 2001 From: julius Date: Sun, 27 Oct 2024 19:46:19 +0100 Subject: [PATCH] as API --- speckles.py | 99 ++++++++++++++++++++++++----------------------------- 1 file changed, 44 insertions(+), 55 deletions(-) diff --git a/speckles.py b/speckles.py index de3b31f..b171c85 100644 --- a/speckles.py +++ b/speckles.py @@ -1,15 +1,44 @@ import itertools +import io +from fastapi.responses import StreamingResponse from pathlib import Path import pyvips import logging from perlin import CoordsGenerator import svg -import json import numpy as np +import uvicorn +from fastapi import FastAPI +from fastapi.middleware.cors import CORSMiddleware + +app = FastAPI(title="Speckles API", root_path="/images") +origins = [ + "http://localhost", + "http://localhost:3000", + "https://localhost", + "https://0124816.xyz", + "http://0124816.xyz:3001", +] + +app.add_middleware( + CORSMiddleware, + allow_origins=origins, + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) RGen = np.random.default_rng() +MEDIA_TYPES = { + "png": "image/png", + "jpeg": "image/jpeg", + "jpg": "image/jpeg", + "svg": "image/svg+xml", + "pdf": "application/pdf", +} + def emoji(marker: str, source="twemoji"): if len(marker) != 1: @@ -181,6 +210,7 @@ def markertoSVG(marker, x, y, c, s): ) +@app.get("/speckles/") def make_wallpaper( speckle_colours: str, density: float = 0.12, @@ -254,60 +284,19 @@ def make_wallpaper( ) content = svg.SVG(width=x, height=y, elements=elements) - if Path(filename).suffix == ".svg": - with open(filename, "wt") as f: - f.write(content.as_str()) - else: - with pyvips.Image.svgload_buffer(content.as_str().encode("utf-8")) as image: - image.write_to_file(filename) + if filename: + if Path(filename).suffix == ".svg": + with open(filename, "wt") as f: + f.write(content.as_str()) + else: + with pyvips.Image.svgload_buffer(content.as_str().encode("utf-8")) as image: + image.write_to_file(filename) + return filename + elif fileformat: + buf = io.BytesIO(content.as_str().encode("utf-8")) + buf.seek(0) + return StreamingResponse(content=buf, media_type=MEDIA_TYPES[fileformat]) if __name__ == "__main__": - palettes = [ - {"colours": ["#DE4A21", "#6F04F2", "#490B1C", "#4a9c9e"], "votes": 1000000}, - {"colours": ["#eee4ab", "#e5cb9f", "#99c4c8", "#68a7ad"], "votes": 100000}, - {"colours": ["#e4d192", "#cba0ae", "#af7ab3", "#80558c"], "votes": 100000}, - {"colours": ["#eeeeee", "#e1d4bb", "#cbb279", "#537188"], "votes": 100000}, - {"colours": ["#c0dbea", "#ba90c6", "#e8a0bf", "#fdf4f5"], "votes": 100000}, - {"colours": ["#f5ffc9", "#b3e5be", "#a86464", "#804674"], "votes": 100000}, - {"colours": ["#ffde7d", "#f6416c", "#f8f3d4", "#00b8a9"], "votes": 100000}, - {"colours": ["#53354a", "#903749", "#e84545", "#2b2e4a"], "votes": 100000}, - {"colours": ["#967e76", "#d7c0ae", "#eee3cb", "#b7c4cf"], "votes": 100000}, - {"colours": ["#fc5185", "#f5f5f5", "#3fc1c9", "#364f6b"], "votes": 100000}, - {"colours": ["#eaeaea", "#ff2e63", "#252a34", "#08d9d6"], "votes": 100000}, - {"colours": ["#eeeeee", "#00adb5", "#393e46", "#222831"], "votes": 100000}, - {"colours": ["#2cd3e1", "#a459d1", "#f266ab", "#ffb84c"], "votes": 100000}, - {"colours": ["#ffe194", "#e8f6ef", "#1b9c85", "#4c4c6d"], "votes": 100000}, - {"colours": ["#146c94", "#19a7ce", "#b0daff", "#feff86"], "votes": 100000}, - {"colours": ["#4c3d3d", "#c07f00", "#ffd95a", "#fff7d4"], "votes": 100000}, - {"colours": ["#8bacaa", "#b04759", "#e76161", "#f99b7d"], "votes": 100000}, - {"colours": ["#146c94", "#19a7ce", "#afd3e2", "#f6f1f1"], "votes": 100000}, - {"colours": ["#9ba4b5", "#212a3e", "#394867", "#f1f6f9"], "votes": 100000}, - {"colours": ["#00ffca", "#05bfdb", "#088395", "#0a4d68"], "votes": 100000}, - {"colours": ["#7c9070", "#9ca777", "#fee8b0", "#f97b22"], "votes": 100000}, - {"colours": ["#002a19", "#000000", "#ffffff", "#e0512f"], "votes": 100000}, - ] - make_wallpaper( - ",".join(["#000000"] + palettes[0]["colours"]), - orientation="2880x1920", - filename="testing.png", - markers="tY.", - size=3, - ) - exit() - - palette_files = ["colorhunt.json"] - for palette_file in palette_files: - with open(palette_file, "rt") as f: - palettes += json.load(f) - - print(len(palettes)) - palettes = [ - palette["colours"] - for palette in palettes - if palette["votes"] > 30000 or len(palette["colours"]) > 5 - ] - print(len(palettes)) - for palette in palettes: - night_sky(palette) - speckles(palette) + uvicorn.run("speckles:app", workers=3, port=8099)