Example #1
0
// ReadImage loads an image from an image file and generates a vector which represents its black and
// white pixels.
func ReadImage(path string) (*Image, error) {
	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}
	image, _, err := image.Decode(file)
	if err != nil {
		return nil, err
	}
	res := &Image{
		Vector: make([]float64, image.Bounds().Dx()*image.Bounds().Dy()),
		Width:  image.Bounds().Dx(),
		Height: image.Bounds().Dy(),
	}
	idx := 0
	for y := 0; y < image.Bounds().Dy(); y++ {
		for x := 0; x < image.Bounds().Dx(); x++ {
			r, g, b, _ := image.At(x, y).RGBA()
			black := float64(r+g+b) / float64(0xffff*3)
			res.Vector[idx] = black
			idx++
		}
	}
	return res, nil
}
Example #2
0
func AnalyzeImage(inp string) (color [3]float64, err error) {
	reader, err := os.Open(inp)
	if err != nil {
		return
	}
	defer reader.Close()
	image, _, err := image.Decode(reader)
	if err != nil {
		return
	}
	bounds := image.Bounds()
	var reds uint32
	var greens uint32
	var blues uint32
	var pixels uint32
	for i := 0; i <= bounds.Max.X; i++ {
		for j := 0; j <= bounds.Max.Y; j++ {
			pixel := image.At(i, j)
			red, green, blue, _ := pixel.RGBA()
			reds += red
			blues += blue
			greens += green
			pixels += 1
		}
	}
	color = [3]float64{float64(reds) / float64(pixels), float64(blues) / float64(pixels), float64(greens) / float64(pixels)}
	return
}
Example #3
0
func main() {
	if len(os.Args) <= 1 {
		fmt.Fprint(os.Stderr, "usage: go-colorweave <filename>")
		return
	}
	reader, err := os.Open(os.Args[1])
	if err != nil {
		fmt.Fprintf(os.Stderr, "%v\n", err)
	}
	defer reader.Close()

	image, _, err := image.Decode(reader)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s", err)
	}

	// Resize the image to smaller scale for faster computation
	image = resize.Resize(100, 0, image, resize.Bilinear)
	bounds := image.Bounds()

	ColorCounter := make(map[string]int)
	Limit := 5 // Limiting how many colors to be displayed in output
	TotalPixels := bounds.Max.X * bounds.Max.Y

	for i := 0; i <= bounds.Max.X; i++ {
		for j := 0; j <= bounds.Max.Y; j++ {
			pixel := image.At(i, j)
			red, green, blue, _ := pixel.RGBA()
			RGBTuple := []int{int(red / 255), int(green / 255), int(blue / 255)}
			ColorName := FindClosestColor(RGBTuple, "css21")
			_, present := ColorCounter[ColorName]
			if present {
				ColorCounter[ColorName] += 1
			} else {
				ColorCounter[ColorName] = 1
			}
		}
	}

	// Sort by the frequency of each color
	keys := make([]int, 0, len(ColorCounter))
	for _, val := range ColorCounter {
		keys = append(keys, val)
	}
	sort.Sort(sort.Reverse(sort.IntSlice(keys)))

	ReverseColorCounter := ReverseMap(ColorCounter)

	// Display the top N dominant colors from the image
	for _, val := range keys[:Limit] {
		fmt.Printf("%s %.2f%%\n", ReverseColorCounter[val], ((float64(val) / float64(TotalPixels)) * 100))
	}
}
Example #4
0
func decodeImage(file string) {
	info := ""

	reader, err := os.Open(file)
	if err != nil {
		fmt.Printf("--- Open error ---\n")
	}
	defer reader.Close()

	//image contain RGBA colors
	image, _, err := image.Decode(reader)
	if err != nil {
		fmt.Printf("--- Decode error ---\n")
	}

	bound := image.Bounds()

	var msg []string
	for i := 0; i < bound.Max.Y; i++ {
		for j := 0; j < bound.Max.X; j++ {
			pixel := image.At(j, i)
			r, _, _, _ := pixel.RGBA()

			if r == 0 {
				info = info + black
			} else {
				info = info + red
			}
		}

		num, err := strconv.ParseInt(info, 2, 0)
		if err != nil {
			panic(err)
		}

		msg = append(msg, string(int(num)))
		info = ""
	}

	fmt.Printf("\nMessage:\n%s\n\n", strings.Join(msg, ""))
}
Example #5
0
		`,
		Done: func(c *TestData) {
			c.Assert(c.createdRect, HasLen, 0)

			window := c.component.CreateWindow(nil)
			defer window.Destroy()
			window.Show()

			// Qt doesn't hide the Window if we call it too quickly. :-(
			time.Sleep(100 * time.Millisecond)

			c.Assert(c.createdRect, HasLen, 1)
			c.Assert(c.createdRect[0].PaintCount, Equals, 1)

			image := window.Snapshot()
			c.Assert(image.At(25, 25), Equals, color.RGBA{0, 0, 0, 255})
			c.Assert(image.At(100, 100), Equals, color.RGBA{255, 0, 0, 255})
		},
	},
	{
		Summary: "Set a property with the wrong type",
		QML: `
			import QtQuick.Window 2.0
			Window { Rectangle { objectName: "rect" } }
		`,
		Done: func(c *TestData) {
			window := c.component.CreateWindow(nil)
			defer window.Destroy()

			root := window.Root() // It's the window itself in this case
			rect := root.ObjectByName("rect")