removed environment variables for the amount of colors and the algorithm for the generation of the color palette in favor of query parameters

This commit is contained in:
Daniel Sommer 2023-11-29 16:43:30 +01:00
parent c6b268c8ab
commit a3830ff1d5
5 changed files with 14 additions and 30 deletions

View file

@ -10,7 +10,7 @@ a simple web server serving (scaled) images and color palettes for [slideshow](h
- `/`: request the name of a random image
- `/image/<name-of-an-image.jpeg>[?resolution=1920x1080]`: request the named image (in the specified resolution)
- `/palette/<name-of-an-image.jpeg>`: request the color palette of the named image
- `/palette/<name-of-an-image.jpeg>[?colors=16&algorithm=wsm]`: request the color palette of the named image (with the specified amount of colors and algorithm)
## configuration
@ -22,8 +22,6 @@ configuration is entirely done via environment variables.
| SLIDESHOW_PORT | 3000 | the port of the web server |
| SLIDESHOW_DIRECTORY | "$HOME" | path to a directory containing images |
| SLIDESHOW_SCANINTERVAL | 60 | the interval for directory scans in seconds |
| SLIDESHOW_PALETTE_ALGORITHM | "wsm" | the algorithm used to generate the color palette |
| SLIDESHOW_PALETTE_COLORS | 16 | the amount of colors generated |
| SLIDESHOW_LOGLEVEL | "info" | the log level |
**note:**

View file

@ -66,5 +66,9 @@ func servePalette(writer http.ResponseWriter, request *http.Request) {
response.send(writer)
return
}
streamColorPalette(writer, image)
amount, _ := strconv.Atoi(request.URL.Query().Get("colors"))
if amount <= 0 {
amount = 16
}
streamColorPalette(writer, image, amount, request.URL.Query().Get("algorithm"))
}

View file

@ -26,11 +26,15 @@ func streamImage(writer http.ResponseWriter, image string, resolution string) {
}
// stream a color palette
func streamColorPalette(writer http.ResponseWriter, image string) {
func streamColorPalette(writer http.ResponseWriter, image string, amount int, algorithm string) {
timestamp := time.Now().UnixMilli()
var response response
amount := config.PaletteColors
colors, error := color_thief.GetPaletteFromFile(image, amount, config.PaletteAlgorithm)
algo := 1
algorithm = strings.ToLower(algorithm)
if algorithm == "wu" {
algo = 0
}
colors, error := color_thief.GetPaletteFromFile(image, amount, algo)
if error != nil {
loggo.Error("encountered an error getting the color palette from image '"+image+"'", error.Error())
response.error = error
@ -51,7 +55,7 @@ func streamColorPalette(writer http.ResponseWriter, image string) {
}
data := []byte(palette)
writer.Write(data)
loggo.InfoTimed("successfully streamed color palette for image '"+image+"'", timestamp, "size: "+internal.FormatBytes(int64(len(data))))
loggo.InfoTimed("successfully streamed color palette for image '"+image+"'", timestamp, "size: "+internal.FormatBytes(int64(len(data))), "colors: "+strconv.Itoa(amount), "algorithm: "+algorithm)
}
// stream an unscaled image

View file

@ -14,9 +14,6 @@ var ServerAddress string
var ServerPort int
var Directory string
var ScanInterval time.Duration
var PaletteAlgorithm int
var PaletteColors int
var BufferSize int
var ConvertAvailable bool
// initialize the config
@ -48,22 +45,6 @@ func Initialize() {
tmpInt = 60
}
ScanInterval = time.Duration(tmpInt) * time.Second
tmpString := os.Getenv("SLIDESHOW_PALETTE_ALGORITHM")
if strings.ToLower(tmpString) == "wu" {
PaletteAlgorithm = 0
} else {
PaletteAlgorithm = 1
}
tmpInt, _ = strconv.Atoi(os.Getenv("SLIDESHOW_PALETTE_COLORS"))
if tmpInt <= 0 {
tmpInt = 16
}
PaletteColors = tmpInt
tmpInt, _ = strconv.Atoi(os.Getenv("SLIDESHOW_BUFFERSIZE"))
if tmpInt <= 0 {
tmpInt = 4096
}
BufferSize = tmpInt
checkConvertCommand()
}

View file

@ -9,9 +9,6 @@ Environment="SLIDESHOW_ADDRESS=0.0.0.0"
Environment="SLIDESHOW_PORT=3000"
Environment="SLIDESHOW_DIRECTORY=$HOME"
Environment="SLIDESHOW_SCANINTERVAL=60"
Environment="SLIDESHOW_RESOLUTION="
Environment="SLIDESHOW_PALETTE_ALGORITHM=wsm"
Environment="SLIDESHOW_PALETTE_COLORS=16"
Environment="SLIDESHOW_LOGLEVEL=info"
ExecStart=/opt/slideshow-api/slideshow-api