Created
September 26, 2020 14:38
-
-
Save jvmvik/96e10a512bc5da10cb0d4aaff135cd51 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Generate gift paper from random images. | |
*/ | |
package main | |
import ( | |
"fmt" | |
"image" | |
"image/jpeg" | |
"math/rand" | |
"os" | |
"path/filepath" | |
"sync" | |
"time" | |
"github.com/deiwin/picasso" | |
"github.com/labstack/gommon/log" | |
) | |
const ( | |
Max = 100 | |
root = "/Volumes/family_drive/photo/google/Takeout/Google Photos" | |
) | |
func main() { | |
var wg sync.WaitGroup | |
for year := 2017; year <= 2020; year++ { | |
wg.Add(1) | |
go paper(year, &wg) | |
} | |
wg.Wait() | |
} | |
func paper(year int, wg *sync.WaitGroup) { | |
defer wg.Done() | |
picks := make([]image.Image, 0) | |
q := fmt.Sprintf("%s/%d*", root, year) | |
fmt.Println("glob: " + q) | |
dirs, err := filepath.Glob(q) | |
if err != nil { | |
panic(err) | |
} | |
// shuffle dirs order | |
rand.Seed(time.Now().UnixNano()) | |
rand.Shuffle(len(dirs), func(i, j int) { dirs[i], dirs[j] = dirs[j], dirs[i] }) | |
// for each directory | |
for _, dir := range dirs { | |
if len(picks) > Max { | |
continue | |
} | |
photos, err := filepath.Glob(dir + "/*.jpg") | |
if err != nil { | |
panic(err) | |
} | |
if len(photos) == 0 { | |
continue | |
} | |
p, err := pick(photos) | |
if err != nil { | |
log.Error(err) | |
continue | |
} | |
picks = append(picks, p) | |
} | |
fmt.Printf("found: %d photos\n", len(picks)) | |
// gray := color.RGBA{0xaf, 0xaf, 0xaf, 0xff} | |
paper := picasso.DrawGridLayout(picks, 2550) | |
fn := fmt.Sprintf("paper_%d_%d.jpg", year, Max) | |
fh, err := os.Create(fn) | |
defer fh.Close() | |
if err != nil { | |
panic(err) | |
} | |
jpeg.Encode(fh, paper, nil) | |
fmt.Printf("save: %s\n", fn) | |
} | |
func pick(photos []string) (image.Image, error) { | |
randomIndex := rand.Intn(len(photos)) | |
pick := photos[randomIndex] | |
fh, err := os.Open(pick) | |
if err != nil { | |
return nil, err | |
} | |
defer fh.Close() | |
return jpeg.Decode(fh) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment