func Example() { f, _ := os.Open("firefox.png") img, _, _ := image.Decode(f) f.Close() fmt.Println(dominantcolor.Hex(dominantcolor.Find(img))) // Output: #CA5527 }
func TestFind(t *testing.T) { f, err := os.Open("firefox.png") if err != nil { t.Fatal(err) } img, _, err := image.Decode(f) if err != nil { t.Fatal(err) } f.Close() c := dominantcolor.Find(img) d := distance(c, firefoxOrange) t.Log("Found dominant color:", dominantcolor.Hex(c)) t.Log("Firefox orange: ", dominantcolor.Hex(firefoxOrange)) t.Logf("Distance: %.2f", d) if d > 50 { t.Errorf("Found color is not close.") } }
// ProcessImage reads image by reading info and resize optionally to desiredSize, we just print errors and skip to the next element func (inputInfo ImageInputInfo) ProcessImage(relativerootpath string, basepath string) { /* first, fetch the data */ f, err := os.Open(inputInfo.InURL) if err != nil { fmt.Fprintln(os.Stderr, "Error while reading image ", inputInfo.InURL, err) return } defer f.Close() sourceimg, _, err := image.Decode(f) if err != nil { fmt.Fprintln(os.Stderr, "Couldn't decode image ", inputInfo.InURL, err) return } size := sourceimg.Bounds().Size() averagecolor := dominantcolor.Hex(dominantcolor.Find(sourceimg)) for scalefactor, widths := range IMGBREAKPOINTS { for _, width := range widths { if width > size.X { fmt.Fprintln(os.Stderr, "Couldn't scale back to ", strconv.Itoa(width), " as the image is ", strconv.Itoa(size.X), " width for ", inputInfo.InURL) continue } outputInfo, err := generateImageAndInfos(inputInfo, relativerootpath, sourceimg, width, int(float32(inputInfo.Cropheight)*scalefactor), averagecolor, basepath) if err != nil { fmt.Fprintln(os.Stderr, "An error occured while extracting or saving ", inputInfo.InURL, err) continue } fmt.Print(outputInfo) } } // Add image (eventually cropped) at full width outputInfo, err := generateImageAndInfos(inputInfo, relativerootpath, sourceimg, -1, inputInfo.Cropheight, averagecolor, basepath) if err != nil { fmt.Fprintln(os.Stderr, "An error occured while extracting or saving ", inputInfo.InURL, err) } fmt.Print(outputInfo) }