Exemplo n.º 1
0
func loadBuiltInCollections() {
	collections = map[string]*imageCollection{
		// these would also normally be dynamically loaded from a config file or something but focus moved away from web app.
		"Design-seeds": {"Thumbnails scraped from design-seeds.com. Great for color variety.", "designseeds", mosaics.NewLibrary(mosaics.AveragingEvaluator()), 0},
		"Aww":          {"top images from /r/aww", "reddits/aww", mosaics.NewLibrary(mosaics.AveragingEvaluator()), 0},
		"Cats":         {"top images from /r/cats", "reddits/cats", mosaics.NewLibrary(mosaics.AveragingEvaluator()), 0},
		"Food":         {"top images from /r/food", "reddits/food", mosaics.NewLibrary(mosaics.AveragingEvaluator()), 0},
		"Flowers":      {"top images from /r/flowers", "reddits/flowers", mosaics.NewLibrary(mosaics.AveragingEvaluator()), 0},
		"Humans":       {"top images from /r/humanPorn", "reddits/humanPorn", mosaics.NewLibrary(mosaics.AveragingEvaluator()), 0},
		"Earth":        {"top images from /r/earthPorn", "reddits/earthPorn", mosaics.NewLibrary(mosaics.AveragingEvaluator()), 0},
		"bttf":         {"Stills from the best movie ever made", "bttf", mosaics.NewLibrary(mosaics.AveragingEvaluator()), 0},
		"nemo":         {"Stills from a colorful movie", "nemo", mosaics.NewLibrary(mosaics.AveragingEvaluator()), 0},
	}
	all := &imageCollection{"All built-in collections combined for maximum variety. May take a long time to build.", "", mosaics.NewLibrary(mosaics.AveragingEvaluator()), 0}
	log.Printf("Loading %d built-in collections\n", len(collections))
	for name, collection := range collections {
		dir := filepath.Join(collectionRoot, collection.path)
		log.Printf("\tLoading %s from %s\n", name, dir)
		files, err := ioutil.ReadDir(dir)
		if err != nil {
			log.Fatal(err)
		}
		log.Printf("\t\tLoading %d images", len(files))
		targetPercent := .1
		for i, file := range files {
			f, err := os.Open(filepath.Join(dir, file.Name()))
			if err != nil {
				log.Fatal(err)
			}

			img, _, err := image.Decode(f)
			if err != nil {
				log.Fatal(err)
			}
			f.Close()
			collection.library.AddImage(img)
			collection.Count++
			all.library.AddImage(img)
			all.Count++
			progress := float64(i) / float64(len(files))
			if progress > targetPercent {
				log.Printf("%.0f%%\n", progress*100)
				targetPercent += .1
			}
		}
	}
	collections["all"] = all
	log.Println("Done loading built-in collections")
}
Exemplo n.º 2
0
func main() {
	flag.Parse()
	var comparision mosaics.Evaluator
	if *grid == 0 {
		comparision = mosaics.AveragingEvaluator()
	} else {
		comparision = mosaics.GridEvaluator(*grid)
	}
	lib := mosaics.NewLibrary(comparision)
	files, err := ioutil.ReadDir(*thumbDir)
	if err != nil {
		check(err)
	}
	log.Println("Loading thumbnails...")
	for _, file := range files {
		img := parseFile(*thumbDir, file.Name())
		lib.AddImage(img)
	}
	log.Println("Loading target image...")
	var r io.Reader
	if (*img)[0:4] == "http" {
		resp, err := http.Get(*img)
		check(err)
		r = resp.Body
	} else {
		f, err := os.Open(*img)
		check(err)
		defer f.Close()
		r = f
	}

	progress := make(chan float64)
	go func() {
		for pct := range progress {
			log.Printf("%.2f percent done\n", pct)
		}
	}()

	if !*doGif {
		target, _, err := image.Decode(r)
		check(err)
		log.Println("Building mosaic...")
		mos := mosaics.BuildMosaicFromLibrary(target, lib, progress)
		output, err := os.Create(*outputFile)
		check(err)
		defer output.Close()
		err = jpeg.Encode(output, mos, &jpeg.Options{20})
		check(err)
	} else {
		g, err := gif.DecodeAll(r)
		check(err)
		log.Println("Building mosaic...")
		mos, err := mosaics.BuildGifzaic(g, lib, progress)
		check(err)
		output, err := os.Create(*outputFile)
		check(err)
		defer output.Close()
		err = gif.EncodeAll(output, mos)
		check(err)
	}
}