示例#1
0
文件: main.go 项目: avesanen/neuro
func main() {

	// input files
	files := []string{"01.png", "02.png", "03.png"}

	// load images and make 100x100 thumbnails of them
	var images []image.Image
	for _, file := range files {
		img, err := imaging.Open(file)
		if err != nil {
			panic(err)
		}
		images = append(images, img)
	}

	n := neuro.NewNetwork([]int{3, 3, 3})

	for y := 0; y < images[0].Bounds().Dy(); y++ {
		for x := 0; x < images[0].Bounds().Dx(); x++ {
			imgR, imgG, imgB, _ := images[0].At(x, y).RGBA()
			r := float64(imgR) / 65535
			g := float64(imgG) / 65535
			b := float64(imgB) / 65535
			n.FeedForward([]float64{r, g, b})

			imgR2, imgG2, imgB2, _ := images[1].At(x, y).RGBA()
			r2 := float64(imgR2) / 65535
			g2 := float64(imgG2) / 65535
			b2 := float64(imgB2) / 65535
			n.BackPropagation([]float64{r2, g2, b2})
		}
	}

	m := image.NewRGBA(image.Rect(0, 0, 128, 128))
	for y := 0; y < images[2].Bounds().Dy(); y++ {
		for x := 0; x < images[2].Bounds().Dx(); x++ {
			imgR, imgG, imgB, _ := images[2].At(x, y).RGBA()
			r := float64(imgR) / 65535
			g := float64(imgG) / 65535
			b := float64(imgB) / 65535
			n.FeedForward([]float64{r, g, b})
			results := n.Results()
			c := color.RGBA{uint8(results[0] * 255), uint8(results[1] * 255), uint8(results[2] * 255), 255}
			m.Set(x, y, c)
		}
	}

	dst := imaging.New(256, 256, color.NRGBA{0, 0, 0, 0})
	dst = imaging.Paste(dst, images[0], image.Pt(0, 0))
	dst = imaging.Paste(dst, images[1], image.Pt(128, 0))
	dst = imaging.Paste(dst, images[2], image.Pt(0, 128))
	dst = imaging.Paste(dst, m, image.Pt(128, 128))

	// save the combined image to file
	err := imaging.Save(dst, "dst.jpg")
	if err != nil {
		panic(err)
	}
}
示例#2
0
文件: sample.go 项目: kamoljan/sushi
func main() {
	// use all CPU cores for maximum performance
	runtime.GOMAXPROCS(runtime.NumCPU())

	// input files
	files := []string{"1.jpg", "2.jpg", "3.jpg", "4.jpg"}

	// load images and make 100x100 thumbnails of them
	var thumbnails []image.Image
	for _, file := range files {
		img, err := imaging.Open(file)
		if err != nil {
			panic(err)
		}
		thumb := imaging.Thumbnail(img, 100, 100, imaging.CatmullRom)
		thumbnails = append(thumbnails, thumb)
	}

	// create a new blank image
	dst := imaging.New(100*len(thumbnails), 100, color.NRGBA{0, 0, 0, 0})

	f, err := os.OpenFile("db", os.O_APPEND|os.O_WRONLY, 0644)
	if err != nil {
		panic(err)
	}
	defer f.Close()

	// paste thumbnails into the new image side by side
	for i, thumb := range thumbnails {
		if _, err = f.Write([]byte(thumb)); err != nil {
			panic(err)
		}
		dst = imaging.Paste(dst, thumb, image.Pt(i*100, 0))
	}

	// save the combined image to file
	err = imaging.Save(dst, "dst.jpg")
	if err != nil {
		panic(err)
	}
}
示例#3
0
func create_thumbnail(file string, thumbnail_name string) {
	img, err := imaging.Open(file)
	if err != nil {
		return
	}
	thumb := imaging.Thumbnail(img, 50, 50, imaging.CatmullRom)
	// thumbnails = append(thumbnails, thumb)

	// create a new blank image
	dst := imaging.New(50, 50, color.NRGBA{0, 0, 0, 0})

	// paste thumbnails into the new image side by side
	// for i, thumb := range thumbnails {
	dst = imaging.Paste(dst, thumb, image.Pt(0, 0))
	// }
	// save the combined image to file
	err = imaging.Save(dst, thumbnail_name)
	if err != nil {
		log.Println(err)
	}
}
示例#4
0
func (cont *StreamController) Post() {

	stream_content := cont.Input().Get("stream_content")
	if stream_content == "" {
		beego.Error("content is empty.")
		cont.Redirect("/stream", 301) // for return this is necessary

		return // if return directry don't execut redirecto, beego notice havn't post.tpl.

	}
	//get picture of stream

	_, picture, err := cont.GetFile("stream_picture") // notice> it is GetFile not GetFiles
	//record err
	if err != nil {
		beego.Error(err)
		cont.Redirect("/stream", 301)
	}
	//save the picture to attachmenmt
	var picture_name string

	if picture != nil {
		picture_name = picture.Filename
		//picture_name = filepath.Ext(picture_name)
		beego.Info(picture_name)                                                                      // output to std
		err = cont.SaveToFile("stream_picture", path.Join("attachment/stream/picture", picture_name)) //stream_picture frome .GetFile("stream_picture")
		if err != nil {
			beego.Error(err)
		}

	}

	//resize image
	runtime.GOMAXPROCS(runtime.NumCPU())

	// input files
	var file_Name = path.Join("attachment/stream/picture", picture_name)
	files := []string{file_Name}

	// load images and make 1024x1024 thumbnails of them
	var thumbnails []image.Image
	for _, file := range files {
		img, err := imaging.Open(file)
		if err != nil {
			panic(err)
		}
		//thumb := imaging.AdjustBrightness(img, -25)
		thumb := imaging.Thumbnail(img, 1024, 780, imaging.CatmullRom)
		thumbnails = append(thumbnails, thumb)
	}

	// create a new blank image
	dst := imaging.New(1024*len(thumbnails), 780, color.NRGBA{0, 0, 0, 0}) // width and height

	// paste thumbnails into the new image side by side
	for i, thumb := range thumbnails {
		dst = imaging.Paste(dst, thumb, image.Pt(i*400, 0))
	}
	// get the file name extension and and trim the suffix
	file_Suffix := path.Ext(file_Name)
	file_Name = strings.TrimSuffix(file_Name, file_Suffix) //从文件名里去除后缀名

	// save the combined image to file
	err = imaging.Save(dst, file_Name+file_Suffix)
	if err != nil {
		panic(err)
	}

	//add stream to database
	models.AddStream(stream_content, picture_name)
	cont.Redirect("/stream", 301)

}