Compare commits

...

4 Commits

Author SHA1 Message Date
andrzej 58aba48e3c working random wallpaper 2024-10-27 01:48:17 +02:00
andrzej 0655e6a1ee random file 2024-10-27 01:48:00 +02:00
andrzej bb3a2d2de3 image manipulation with GIFT 2024-10-27 01:47:48 +02:00
andrzej 7352e9e999 hyprpaper integration 2024-10-27 01:47:38 +02:00
9 changed files with 184 additions and 0 deletions

7
config.json Normal file
View File

@ -0,0 +1,7 @@
{
"colorize":[ 247,40,60 ],
"contrast":-35,
"gamma":0.8,
"root":"/home/andrzej/bgs"
}

BIN
curr.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB

16
files.go Normal file
View File

@ -0,0 +1,16 @@
package main
import (
"math/rand"
"os"
)
func getRandomFile(dir string) (string, error) {
files, err := os.ReadDir(dir)
if err != nil {
return "", err
}
randomIndex := rand.Intn(len(files))
randomImg := files[randomIndex]
return dir + "/" + randomImg.Name(), nil
}

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module go-img
go 1.23.2
require github.com/disintegration/gift v1.2.1

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/disintegration/gift v1.2.1 h1:Y005a1X4Z7Uc+0gLpSAsKhWi4qLtsdEcMIbbdvdZ6pc=
github.com/disintegration/gift v1.2.1/go.mod h1:Jh2i7f7Q2BM7Ezno3PhfezbR1xpUg9dUg3/RlKGr4HI=

34
hyprpaper.go Normal file
View File

@ -0,0 +1,34 @@
package main
import (
"errors"
"os/exec"
)
func hyprpaperPreload(path string) error {
var err error
var out []byte
out, err = exec.Command("hyprctl", "hyprpaper", "preload", path).Output()
if err != nil {
return err
}
str := string(out)
if str != "ok\n" {
return errors.New(str)
}
return nil
}
func hyprpaperWallpaper(path string) error {
var err error
var out []byte
out, err = exec.Command("hyprctl", "hyprpaper", "wallpaper", ","+path).Output()
if err != nil {
return err
}
str := string(out)
if str != "ok\n" {
return errors.New(str)
}
return nil
}

58
image.go Normal file
View File

@ -0,0 +1,58 @@
package main
import (
"github.com/disintegration/gift"
"image"
"log"
"os"
_ "image/jpeg"
"image/png"
)
type Config struct {
Colorize [3]float32
Contrast int
Gamma float64
Root string
}
func loadImage(filename string) image.Image {
f, err := os.Open(filename)
if err != nil {
log.Fatalf("os.Open failed: %v", err)
}
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
log.Fatalf("image.Decode failed: %v", err)
}
return img
}
func saveImage(filename string, img image.Image) {
f, err := os.Create(filename)
if err != nil {
log.Fatalf("os.Create failed: %v", err)
}
defer f.Close()
err = png.Encode(f, img)
if err != nil {
log.Fatalf("png.Encode failed: %v", err)
}
}
func processImage(img image.Image, config Config) image.Image {
cArgs := config.Colorize
g := gift.New(
gift.Colorize(cArgs[0], cArgs[1], cArgs[2]),
gift.Contrast(float32(config.Contrast)),
gift.Gamma(float32(config.Gamma)),
)
dst := image.NewRGBA(g.Bounds(img.Bounds()))
g.Draw(dst, img)
return dst
}

BIN
in.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

62
main.go Normal file
View File

@ -0,0 +1,62 @@
package main
import (
"encoding/json"
"fmt"
"log"
"os"
)
func main() {
var err error
config := loadConfig()
//get working directory
wd, err := os.Getwd()
if err != nil {
log.Fatal("couldn't find working directory", err)
}
curr := wd + "/curr.png"
// apply curr.png if already preloaded
err = hyprpaperWallpaper(curr)
if err != nil {
//preload curr.png if not
err = hyprpaperPreload(curr)
if err != nil {
fmt.Println("could not preload (this is fine)", err)
}
}
dir := config.Root + "/chsck"
randomImg, _ := getRandomFile(dir)
img := loadImage(randomImg)
dst := processImage(img, config)
saveImage("curr.png", dst)
path := wd + "/curr.png"
err = hyprpaperPreload(path)
if err != nil {
log.Fatal("preload failed!", err)
}
err = hyprpaperWallpaper(path)
if err != nil {
log.Fatal("set wallpaper failed!", err)
}
}
func loadConfig() Config {
configRaw, err := os.ReadFile("./config.json")
if err != nil {
log.Fatal("Couldn't open config file!", err)
}
var config Config
err = json.Unmarshal(configRaw, &config)
if err != nil {
log.Fatal("Couldn't unmarshal config!", err)
}
return config
}