Example #1
0
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)
	}
}
Example #2
0
func fractal(w, h int, offsetX, offsetY, zoom float64, iterations int) image.Image {
	img := imaging.New(w, h, color.White)
	for x := 0; x < w; x++ {
		for y := 0; y < h; y++ {
			sx, sy := pixelToReal(w, h, x, y, offsetX, offsetY, zoom)
			j := complex(sx, sy)
			i := iteration(j, iterations)
			c := colorCode(i, iterations)
			img.Set(x, y, c)
		}
	}
	return img
}
Example #3
0
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)
	}
}
Example #4
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)
	}
}
Example #5
0
// 根据需求实时获取缩略图片
// way string 缩略图生成方式:
//      'auto': 原图全显优先,获取width或height最大,原图不会被截断
//      'full': 图片填充优先,将widthd和height,按照比例全部填充, 原图将会被截断
//
func Thumb(way string, width uint, height uint, picturePath string) (*os.File, error) {
	/* 获取配置文件中上传根路径 */
	revel.Config.SetSection(revel.RunMode)
	uploadRoot, _ := revel.Config.String("uploadpath")

	ext := filepath.Ext(picturePath)

	/* 检测是否是获取一个远端url图片的缩率图 */
	picUrl, err := url.ParseRequestURI(picturePath)
	isRemotePic := false
	//if err == nil && picUrl.Scheme == "http" && (picUrl.Host == "img.show.wepiao.com" || picUrl.Host == "img.wxmovie.com" || picUrl.Host == "static.show.wepiao.com" || picUrl.Host == "static.task.18.tl:86") {
	if err == nil && picUrl.Scheme == "http" {
		isRemotePic = true
		uploadRoot += "/upload"
		savePath, saveName := HelperForFilePathCreator(uploadRoot, picturePath, false)
		picturePath = savePath + "/" + saveName + ext
	}

	if way != "full" {
		way = "auto"
	}

	fname := uploadRoot + "/" + picturePath
	thumbFile := fmt.Sprintf("%s_%s_%v_%v%v", fname, way, width, height, ext)

	/* 检查有没有现成的缩略图 */
	_, err = os.Stat(thumbFile)
	if err == nil { // 如果有,直接读出
		thumb, _ := os.Open(thumbFile)
		return thumb, nil
	}
	// fmt.Printf("没有现成缩率图\n")

	/* 没有现成的缩略图,需要实施生成, 查找原图片文件是否存在 */
	finfo, err := os.Stat(fname)
	if err != nil {
		// 如果是获取远端图片,那就先获取.
		if isRemotePic {
			// fmt.Printf("是远端图片\n")

			client := http.Client{}
			reqImg, err := client.Get(picUrl.Scheme + "://" + picUrl.Host + picUrl.Path)
			defer reqImg.Body.Close()
			if err != nil {
				return nil, err
			}
			//如果是4开头的错误状态 则生成默认图片
			if '4' == reqImg.Status[0] {
				fname = "/opt/www/static/pc/img/logo.png"
				way = "auto"
			} else {
				out, err := os.Create(fname)
				if err != nil {
					return nil, err
				}
				defer out.Close()
				io.Copy(out, reqImg.Body)
			}
			finfo, err = os.Stat(fname)
			// fmt.Printf("存储远端图片到: %#v\n", fname)
		} else if os.IsNotExist(err) || err.(*os.PathError).Err == syscall.ENOTDIR {
			return nil, errors.New("20108")
		} else {
			return nil, err
		}
	}
	if finfo.Mode().IsDir() {
		return nil, errors.New("10016")
	}

	file, err := os.Open(fname)
	if err != nil {
		if os.IsNotExist(err) {
			return nil, errors.New("20108")
		}
		return nil, err
	}
	defer file.Close()

	/* 生成缩略图 */
	var img image.Image
	img, _, err = image.Decode(file)

	if err != nil {
		return nil, err
	}

	// 产生缩略图,等比例缩放
	out, errF := os.Create(thumbFile)
	if errF != nil {
		return nil, errors.New("20108")
	}
	defer out.Close()

	var m image.Image
	// fmt.Printf("生成缩率图\n")

	origBounds := img.Bounds()
	origWidth := uint(origBounds.Dx())
	origHeight := uint(origBounds.Dy())
	if way == "full" {
		dst := image.NewRGBA(image.Rect(0, 0, int(width), int(height)))
		graphics.Thumbnail(dst, img)
		m = dst
	} else if width >= origWidth && height >= origHeight {
		bg := imaging.New(int(width), int(height), color.NRGBA{255, 255, 255, 255})
		m = imaging.PasteCenter(bg, img)
	} else {
		m = resize.Thumbnail(width, height, img, resize.Lanczos3)
	}

	/* 生成文件 */
	err = jpeg.Encode(out, m, &jpeg.Options{90})
	if err != nil {
		err = png.Encode(out, m)
	}
	if err != nil {
		err = gif.Encode(out, m, nil)
	}
	if err != nil {
		return nil, err
	}

	/* 这里不知道如何将 image.Image类型转换为 io.Writer类型,
	   所以只有暂时在保存后,又重新读了次文件 */
	thumb, _ := os.Open(thumbFile)

	return thumb, nil
}
Example #6
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)

}