當前位置: 首頁>>代碼示例>>Golang>>正文


Golang resize.Resize函數代碼示例

本文整理匯總了Golang中github.com/nfnt/resize.Resize函數的典型用法代碼示例。如果您正苦於以下問題:Golang Resize函數的具體用法?Golang Resize怎麽用?Golang Resize使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了Resize函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: resizeImage

func resizeImage(im image.Image, width int, height int) image.Image {
	if width > height {
		return resize.Resize(0, uint(height), im, resize.Lanczos3)
	}

	return resize.Resize(uint(width), 0, im, resize.Lanczos3)
}
開發者ID:jmptrader,項目名稱:purrraceholder,代碼行數:7,代碼來源:image_handler.go

示例2: Transform

func (p *photo) Transform(crop image.Rectangle, width, height uint, dontenlarge bool, format string, resample resize.InterpolationFunction) (Image, error) {
	img, err := p.Image()
	if err != nil {
		return nil, e.Forward(err)
	}

	imgResized := img
	if !(width == 0 && height == 0) {
		rect := img.Bounds()
		w := uint(rect.Max.X - rect.Min.X)
		h := uint(rect.Max.Y - rect.Min.Y)

		if (width >= w || height >= h) && !dontenlarge {
			imgResized = resize.Resize(width, height, img, resample)
		} else if width < w && height < h {
			imgResized = resize.Resize(width, height, img, resample)
		}
	}

	b := make([]byte, 0, 1024)
	buf := bytes.NewBuffer(b)

	err = jpeg.Encode(buf, imgResized, nil)
	if err != nil {
		return nil, e.Forward(err)
	}

	return &photo{Buf: buf.Bytes()}, nil
}
開發者ID:fcavani,項目名稱:image,代碼行數:29,代碼來源:cache-disk_test.go

示例3: downloadImage

//download image from town
func (t *Townparser) downloadImage(url string, name string) error {
	defer func() {
		if err := recover(); err != nil {
			log.Info("%s recovered from panic", TAG)
			return
		}
	}()
	if url == "0" {
		return nil
	}
	exist, err := exists("templates/static/images/" + name + ".jpg")
	if err != nil {
		log.Error("%s %s", TAG, err.Error())
	}

	if !exist {
		resp, err := t.Tc.Get(url)
		if err != nil {
			log.Error("%s image download failed, name: %v, url: %v", TAG, name, url)
			log.Error("%s %s", TAG, err.Error())
			return err
		}
		defer resp.Body.Close()
		if strings.Contains(url, "jpg") || strings.Contains(url, "jpeg") {
			img, _ := jpeg.Decode(resp.Body)
			m := resize.Resize(300, 0, img, resize.Lanczos2Lut)
			out, err := os.Create("templates/static/images/" + name + ".jpg")
			if err != nil {
				log.Error("%s %s", TAG, err.Error())
				return nil
			}

			// write new image to file
			jpeg.Encode(out, m, nil)
			out.Close()
		} else if strings.Contains(url, "png") {
			img, err := png.Decode(resp.Body)
			if err != nil {
				log.Error("%s %s", TAG, err.Error())
			}
			m := resize.Resize(300, 0, img, resize.Lanczos2Lut)
			out, err := os.Create("templates/static/images/" + name + ".png")
			if err != nil {
				log.Error("%s %s", TAG, err.Error())
				return nil
			}

			// write new image to file
			jpeg.Encode(out, m, nil)
			out.Close()
		}
	}
	time.Sleep(200 * time.Millisecond)
	return nil
}
開發者ID:Bootz,項目名稱:nzbcrawler,代碼行數:56,代碼來源:t_parser.go

示例4: thumbnailHandler

func thumbnailHandler() http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		filename, err := url.QueryUnescape(r.URL.String())
		checkErr(err)

		fmt.Println("handling request for", filename)

		file, err := os.Open(filename)
		checkErr(err)
		defer file.Close()

		img, filetype, err := image.Decode(file)
		checkErr(err)

		var newImage image.Image

		if thumbnailSize < img.Bounds().Dx() || thumbnailSize < img.Bounds().Dy() {
			if img.Bounds().Dx() > img.Bounds().Dy() {
				newImage = resize.Resize(uint(thumbnailSize), 0, img, resize.Lanczos3)
			} else {
				newImage = resize.Resize(0, uint(thumbnailSize), img, resize.Lanczos3)
			}
		} else {
			newImage = img
		}

		switch filetype {
		case "png":
			err = png.Encode(w, newImage)
		case "gif":
			err = gif.Encode(w, newImage, nil)
		case "jpeg", "jpg":
			err = jpeg.Encode(w, newImage, nil)
		case "bmp":
			err = bmp.Encode(w, newImage)
		case "tiff":
			err = tiff.Encode(w, newImage, nil)
		default:
			// not sure how you got here but what are we going to do with you?
			fmt.Println("Unknown image type: ", filetype)
			io.Copy(w, file)
		}
		checkErr(err)
		fmt.Println("done handling request")

	})
}
開發者ID:xcsrz,項目名稱:SurfMedia,代碼行數:48,代碼來源:images.go

示例5: ImgRZ

//funcion resize imagen
func (u Util) ImgRZ(s string) {

	//cargamos archivo de imageb jpg
	file, err := os.Open("static/images/fotos_ori/" + s)
	if err != nil {
		log.Fatal("en este 1", err)
	}

	// decode jpeg  image.Image
	img, err := jpeg.Decode(file)
	if err != nil {
		log.Fatal("en este 2", err)
	}
	file.Close()

	// resize to width 1000 using Lanczos resampling
	// and preserve aspect ratio
	m := resize.Resize(216, 162, img, resize.Lanczos3)

	out, err := os.Create("static/images/fotos_rz/" + s)
	if err != nil {
		log.Fatal(err)
	}
	defer out.Close()

	// write new image to file
	jpeg.Encode(out, m, nil)

	return

}
開發者ID:carlosjml4,項目名稱:artyprovee,代碼行數:32,代碼來源:util.go

示例6: MakeThumb

// Make thumbnail for the selected file
func MakeThumb(src string, dst_sane string, dst_thumb string) error {
	file, err := os.Open(src)
	defer file.Close()
	if err != nil {
		return err
	}

	img, err := jpeg.Decode(file)
	if err != nil {
		return err
	}

	sane := resize.Resize(1080, 0, img, resize.Bilinear)

	out_sane, err := os.Create(dst_sane)
	if err != nil {
		return err
	}
	defer out_sane.Close()

	jpeg.Encode(out_sane, sane, nil)

	thumb := resize.Thumbnail(200, 200, img, resize.Bilinear)

	out_thumb, err := os.Create(dst_thumb)
	if err != nil {
		return err
	}
	defer out_thumb.Close()

	jpeg.Encode(out_thumb, thumb, nil)
	return nil
}
開發者ID:htruong,項目名稱:oryza,代碼行數:34,代碼來源:site.go

示例7: createSmallPic

func createSmallPic(file io.Reader, fileSmall string, w, h int) error {
	// decode jpeg into image.Image
	img, err := jpeg.Decode(file)
	if err != nil {
		return err
	}
	b := img.Bounds()
	if w > b.Dx() {
		w = b.Dx()
	}
	if h > b.Dy() {
		h = b.Dy()
	}
	// resize to width 1000 using Lanczos resampling
	// and preserve aspect ratio
	m := resize.Resize(uint(w), uint(h), img, resize.Lanczos3)

	out, err := os.Create(fileSmall)
	if err != nil {
		return err
	}
	defer out.Close()

	// write new image to file
	return jpeg.Encode(out, m, nil)
}
開發者ID:gusa1120,項目名稱:beego_sample,代碼行數:26,代碼來源:article.go

示例8: ConvertMenuIcon

func ConvertMenuIcon(i image.Image) *BitmapImage {
	menubarHeigh := GetSystemMenuImageSize()

	c := resize.Resize(menubarHeigh, menubarHeigh, i, resize.Lanczos3)

	return BitmapImageNew(c)
}
開發者ID:kien8995,項目名稱:desktop,代碼行數:7,代碼來源:desktop_win_systray.go

示例9: renderThumbs

func renderThumbs() {
	images, err := ioutil.ReadDir("src/assets/photos")
	if err != nil {
		log.Printf("Error: %v\n", err.Error())
		return
	}

	for _, img := range images {
		if !img.IsDir() {
			f, err := os.Open(fmt.Sprintf("src/assets/photos/%v", img.Name()))
			if err != nil {
				log.Printf("Error: %v\n", err.Error())
			} else {
				i, err := jpeg.Decode(f)
				if err != nil {
					log.Printf("Error: %v\n", err.Error())
				} else {
					f.Close()

					thumb := resize.Resize(400, 0, i, resize.Lanczos3)

					out, err := os.Create(fmt.Sprintf("src/assets/photos/thumbs/%v", img.Name()))
					if err != nil {
						log.Printf("Error: %v\n", err.Error())
					} else {
						defer out.Close()
						jpeg.Encode(out, thumb, nil)
					}
				}
			}
		}
	}
}
開發者ID:dan9186,項目名稱:gobaby,代碼行數:33,代碼來源:render.go

示例10: Convert

func (r *ResizeConverter) Convert(inFile string, outFile string, rect Rect) error {
	in, err := os.Open(inFile)
	if err != nil {
		log.Fatalf("Runner(File): cannot open source file '%s' for reading (%s).", inFile, err)
	}
	defer in.Close()

	out, err := os.Create(outFile)
	if err != nil {
		log.Printf("ERROR: cannot create new file %s (%s)", outFile, err)
	}
	defer out.Close()

	source, _, err := image.Decode(in)
	if err != nil {
		return err
	}

	interp, ok := interpolations[r.Interpolation]
	if !ok {
		interp = interpolations["l3"]
	}

	resized := resize.Resize(rect.Width, rect.Height, source, interp)
	png.Encode(out, resized)
	return nil
}
開發者ID:noscripter,項目名稱:blade,代碼行數:27,代碼來源:resize_converter.go

示例11: NewThumbnail

func NewThumbnail(uri string, width, height uint) (filepath, mediatype string, w, h int, err error) {
	fp, mediatype, _, err := document.DefaultCurl().Download(uri)
	if err != nil {
		return
	}
	f, err := os.Open(fp)
	if err != nil {
		return
	}
	defer f.Close()
	img, mediatype, err := image.Decode(f)
	if err != nil {
		return
	}
	imgnew := resize.Resize(width, height, img, resize.MitchellNetravali)
	w = imgnew.Bounds().Max.X
	h = imgnew.Bounds().Max.Y

	of, err := ioutil.TempFile("", "")
	if err != nil {
		return
	}
	defer of.Close()
	err = jpeg.Encode(of, imgnew, &jpeg.Options{90})
	if err != nil {
		return
	}
	filepath = of.Name()
	return
}
開發者ID:heartszhang,項目名稱:gextract,代碼行數:30,代碼來源:thumbnail.go

示例12: writeAnsiImage

func writeAnsiImage(img image.Image, file *os.File, width int) {
	m := resize.Resize(uint(width), uint(float32(width)*PROPORTION), img, resize.Lanczos3)
	var current, previous string
	bounds := m.Bounds()
	for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
		for x := bounds.Min.X; x < bounds.Max.X; x++ {
			current = toAnsiCode(m.At(x, y))
			if current != previous {
				fmt.Print(current)
				file.WriteString(current)
			}
			if ANSI_RESET != current {
				char := string(CHARACTERS[rand.Int()%len(CHARACTERS)])
				fmt.Print(char)
				file.WriteString(char)
			} else {
				fmt.Print(" ")
				file.WriteString(" ")
			}
		}
		fmt.Print("\n")
		file.WriteString("\n")
	}
	fmt.Print(ANSI_RESET)
	file.WriteString(ANSI_RESET)
}
開發者ID:pedrojimenez,項目名稱:ansize,代碼行數:26,代碼來源:ansize.go

示例13: generate

// Generates a resized image image from the original.
func (s *Store) generate(name, size, interp string) (path string, err error) {
	uSize, err := strconv.ParseUint(size, 10, 0)

	if err != nil {
		return
	}
	interpolation, ok := interpolations[interp]

	if !ok {

		return "", errors.New("Unknown interpolation: " + interp)
	}
	f, err := os.Open(s.hashedPath(name, "orig"))

	if err != nil {
		return
	}
	defer f.Close()

	img, _, err := image.Decode(f)

	if err != nil {
		return
	}
	newImg := resize.Resize(uint(uSize), 0, img, interpolation)

	return s.store(filepath.Join(size, interp), name, newImg)
}
開發者ID:kjk,項目名稱:goresizetest,代碼行數:29,代碼來源:store.go

示例14: resizeImage2file

//截圖生成文件
func resizeImage2file(src_img, dest_img string) {
	file, err := os.Open(src_img)
	if err != nil {
		fmt.Println(err)
		return
	}

	// decode jpeg into image.Image
	img, err := jpeg.Decode(file)
	if err != nil {
		fmt.Println(err)
		return
	}

	file.Close()
	m := resize.Resize(0, 1000, img, resize.Lanczos2)
	out, err := os.Create(dest_img)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer out.Close()

	err = jpeg.Encode(out, m, nil)
	if err != nil {
		fmt.Println(err)
		return
	}
}
開發者ID:widaT,項目名稱:batch_resize,代碼行數:30,代碼來源:batch_resize.go

示例15: imgToFile

func imgToFile(ic *iconf) (string, error) {
	img := resize.Resize(ic.width, 0, ic.image, resize.NearestNeighbor)
	ic.height = uint(img.Bounds().Size().Y)
	ic.fid = fmt.Sprintf("%s-%s-%s-%s-%s-%s-%d-%d", ic.machine, ic.format, ic.density, ic.ui, ic.hash, ic.color, ic.width, ic.height)
	dir := fmt.Sprintf("%s/%s/%s/%s/%s/%s/%s", conf.SushiobrolStore, ic.machine, ic.format, ic.density, ic.ui, ic.hash[0:2], ic.hash[2:4])
	ic.path = fmt.Sprintf("%s/%s", dir, ic.fid)
	out, err := os.Create(ic.path)
	if err != nil {
		log.Println(err)
		return "", err
	}
	defer out.Close()
	if conf.InputType == "jpeg" {
		err = jpeg.Encode(out, img, nil) // write image to file
		if err != nil {
			fmt.Println("ERROR: Unable to Encode into webp")
			return "", err
		}
	} else {
		if webp.Encode(out, img, &webp.Options{conf.Lossless, conf.Quality}); err != nil {
			fmt.Println("ERROR: Unable to Encode into webp")
			return "", err
		}
	}
	return ic.fid, err
}
開發者ID:kamoljan,項目名稱:sushiobrol,代碼行數:26,代碼來源:post.go


注:本文中的github.com/nfnt/resize.Resize函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。