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


Golang resize.Thumbnail函數代碼示例

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


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

示例1: createThumbPic

func createThumbPic(source, to string, width, height uint) error {
	source_file, err := os.Open(source)
	if err != nil {
		return err
	}
	defer source_file.Close()

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

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

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

	// write new image to file
	if err := jpeg.Encode(thumb_pic, m, nil); err != nil {
		return err
	}
	return nil
}
開發者ID:xiongpf,項目名稱:Jeremiah,代碼行數:29,代碼來源:resource_controller.go

示例2: resizeImage

func resizeImage(inDir string, name string, size uint, outDir string) {
	file, err := os.Open(inDir + name)
	if err != nil {
		log.Fatal(err)
	}

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

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

	out, err := os.Create(outDir + name)
	if err != nil {
		log.Fatal(err)
	}
	defer out.Close()

	// write new image to file
	jpeg.Encode(out, m, nil)
}
開發者ID:sklise,項目名稱:golang-resize-tasks,代碼行數:26,代碼來源:Godofile.go

示例3: SaveImage

//Save a User Avatar Image
func (uam *UserAvatarManager) SaveImage(id string, rev string, attType string,
	data io.Reader, curUser *CurrentUserInfo) (string, error) {
	auth := curUser.Auth
	// Decode the image
	image, _, err := image.Decode(data)
	if err != nil {
		return "", err
	}
	// We need two image sizes, 200px, and a 32px thumbnail
	largeSize := resize.Resize(200, 0, image, resize.Bicubic)
	lRev, err := uam.saveImage(id, rev, "largeSize", largeSize, auth)
	if err != nil {
		return "", err
	}
	thumbnail := resize.Thumbnail(32, 32, image, resize.Bicubic)
	tRev, err := uam.saveImage(id, lRev, "thumbnail", thumbnail, auth)
	if err != nil {
		return "", err
	}
	um := new(UserManager)
	user := User{}
	uRev, err := um.Read(id, &user, curUser)
	if err != nil {
		return "", err
	}
	theUri := services.ApiPrefix() + "/users" + avatarUri
	user.Public.Avatar = strings.Replace(theUri+"/image", "{user-id}", id, 1)
	user.Public.AvatarThumbnail = strings.Replace(theUri+"/thumbnail", "{user-id}", id, 1)
	if uRev, err = um.Update(id, uRev, &user, curUser); err != nil {
		return "", err
	} else {
		return tRev, nil
	}
}
開發者ID:rhinoman,項目名稱:wikifeat,代碼行數:35,代碼來源:user_avatar_manager.go

示例4: imageResize

func imageResize(filePath, newFilePath string, width, height uint, interp resize.InterpolationFunction) error {
	imgb, err := os.Open(filePath)
	if err != nil {
		return err
	}
	defer imgb.Close()

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

	m := resize.Thumbnail(width, height, img, interp)

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

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

	return nil
}
開發者ID:e154,項目名稱:www.novshin.ru,代碼行數:25,代碼來源:tirecatalogimage.go

示例5: main

func main() {
	flag.Parse()
	imgs := flag.Args()

	if len(imgs) == 0 {
		fmt.Printf("usage: %s imagefile ...\n", os.Args[0])
		flag.PrintDefaults()
		os.Exit(1)
	}

	for _, i := range imgs {
		img, err := openImage(i)
		if err != nil {
			panic(err)
		}

		t := resize.Thumbnail(*flagBox, *flagBox, img, resampler(*flagResampling))
		out, err := os.Create(outName(i))
		if err != nil {
			panic(err)
		}

		err = png.Encode(out, t)
		if err != nil {
			panic(err)
		}
		out.Close()
	}
}
開發者ID:suapapa,項目名稱:tools,代碼行數:29,代碼來源:main.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: generateThumbnail

func generateThumbnail(source, destination string) error {
	file, err := os.Open(source)
	if err != nil {
		return err
	}
	var img image.Image
	var imageType string
	img, imageType, err = image.Decode(file)
	if err != nil {
		return err
	}
	file.Close()

	m := resize.Thumbnail(maxWidth, maxHeight, img, resize.Lanczos3)

	out, err := os.Create(destination)
	if err != nil {
		return err
	}
	defer out.Close()
	switch imageType {
	case "gif":
		return gif.Encode(out, m, nil)
	case "jpeg":
		return jpeg.Encode(out, m, nil)
	case "png":
		return png.Encode(out, m)
	}
	return nil
}
開發者ID:gophergala2016,項目名稱:pushop,代碼行數:30,代碼來源:image.go

示例8: printImage

func printImage(img image.Image, width uint, height uint) {
	resized := resize.Thumbnail(width, height, img, resize.NearestNeighbor)
	bounds := resized.Bounds()

	for y := bounds.Min.Y; y < bounds.Max.Y; y += 2 {
		for x := bounds.Min.X; x < bounds.Max.X; x++ {
			upperRed, upperGreen, upperBlue, _ := resized.At(x, y).RGBA()
			lowerRed, lowerGreen, lowerBlue, _ := resized.At(x, y+1).RGBA()

			// Only print the background if upper and lower row are same color
			if upperRed == lowerRed && upperGreen == lowerGreen && upperBlue == lowerBlue {
				fmt.Printf(
					"\x1b[48;2;%d;%d;%dm ",
					upperRed/256,
					upperGreen/256,
					upperBlue/256,
				)
			} else {
				fmt.Printf(
					"\x1b[48;2;%d;%d;%dm\x1b[38;2;%d;%d;%dm▄",
					upperRed/256,
					upperGreen/256,
					upperBlue/256,
					lowerRed/256,
					lowerGreen/256,
					lowerBlue/256,
				)
			}
		}
		fmt.Println("\x1b[0m")
	}
}
開發者ID:Tenzer,項目名稱:explosion,代碼行數:32,代碼來源:explosion.go

示例9: CropImage

// cropImage permet de redimensionnement d'une image
// @todo il faut permettre le crop de jpg, png et gif
// pour le moment la fonction permet de réaliser des crop uniquemen pour des jpeg
func CropImage(imageUrl string, toW uint, toH uint) error {

	// open file
	rfile, err := os.Open(imageUrl)
	defer rfile.Close()
	if err != nil {
		return errors.New("le fichier n' a pas les droits pour être ouvert")
	}

	// decode jpeg into image.Image
	rimg, err := jpeg.Decode(rfile)
	if err != nil {
		return errors.New("l'image n'a pas put être décodé")
	}
	//rfile.Close()

	image := resize.Thumbnail(toW, toH, rimg, resize.Bilinear)

	out, err := os.Create(imageUrl)
	defer out.Close()
	if err != nil {
		return errors.New("l'image n'a pas put être créé")
	}

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

	return nil
}
開發者ID:konginteractive,項目名稱:cme,代碼行數:32,代碼來源:upload_crop_image.go

示例10: generateThumbnail

func generateThumbnail(r io.Reader) (b []byte, mimeType string, err error) {
	img, imgType, err := image.Decode(r)
	if err != nil {
		return
	}

	thumb := resize.Thumbnail(256, 192, img, resize.Lanczos3)
	buf := util.BufferPool.GetBuffer()
	defer util.BufferPool.Put(buf)

	switch imgType {
	case "gif":
		if err = gif.Encode(buf, thumb, nil); err != nil {
			return
		}

		mimeType = "image/gif"
	default:
		if err = jpeg.Encode(buf, thumb, &jpeg.Options{Quality: 80}); err != nil {
			return
		}

		mimeType = "image/jpeg"
	}

	b = buf.Bytes()
	return
}
開發者ID:urandom,項目名稱:readeef,代碼行數:28,代碼來源:common.go

示例11: Resize

func Resize(oriImgPath, destImgPath, format string, width, height uint, force bool) (string, error) {
	oriImgFullPath, err := filepath.Abs(oriImgPath)

	if err != nil {
		return "", err
	}

	destImgFullPath, err := filepath.Abs(destImgPath)

	if err != nil {
		return "", err
	}

	imgFile, err := os.Open(oriImgFullPath)

	if err != nil {
		return "", err
	}

	var img image.Image

	if format == "jpeg" || format == "jpg" {
		img, err = jpeg.Decode(imgFile)
	} else if format == "png" {
		img, err = png.Decode(imgFile)
	} else {
		return "", errors.New("unknown format")
	}

	imgFile.Close()
	if err != nil {
		return "", err
	}

	if force {
		img = resize.Resize(width, height, img, resize.Lanczos3)
	} else {
		img = resize.Thumbnail(width, height, img, resize.Lanczos3)
	}

	out, err := os.Create(destImgFullPath)
	defer out.Close()

	if err != nil {
		return "", err
	}

	if format == "jpeg" || format == "jpg" {
		jpeg.Encode(out, img, nil)
	} else {
		// png
		png.Encode(out, img)
	}

	return destImgFullPath, nil
}
開發者ID:coderhaoxin,項目名稱:resize,代碼行數:56,代碼來源:img.go

示例12: fillStorageUser

// Fills the content of an internal (storage) User from the external representation.
// All security and authorization checks must have been done before calling this
// method.
func (au *ApiUser) fillStorageUser(u *model.User) (err error) {
	if au.Email != nil {
		u.Email = *au.Email
	}
	if au.Password != nil {
		err = u.SetPassword(*au.Password)
		if err != nil {
			return err
		}
	}
	if au.Name != nil {
		u.Name = *au.Name
	}
	if au.Description != nil {
		u.Description = *au.Description
	}
	if au.Disability != nil {
		u.Disability = *au.Disability
	}
	if au.DisabilityType != nil {
		u.DisabilityType = *au.DisabilityType
	}
	if au.Phone != nil {
		u.Phone = *au.Phone
	}
	if au.EmergencyPhone != nil {
		u.EmergencyPhone = *au.EmergencyPhone
	}
	if au.Image != nil && len(*au.Image) != 0 {
		reader := bytes.NewReader(*au.Image)
		image, err := jpeg.Decode(reader)
		if err != nil {
			return err
		}
		if image.Bounds().Dx() > 128 || image.Bounds().Dy() > 128 {
			resizedImage := resize.Thumbnail(128, 128, image, resize.Bicubic)
			out := new(bytes.Buffer)
			jpeg.Encode(out, resizedImage, nil)
			u.Image = out.Bytes()
		} else {
			u.Image = *au.Image
		}
	}
	if au.PushToken != nil {
		u.PushToken = *au.PushToken
	}
	if au.AgentId != nil {
		u.AgentId = *au.AgentId
	}
	if au.IsAgent != nil {
		u.IsAgent = *au.IsAgent
	}
	return err
}
開發者ID:JohnAtWassa,項目名稱:hackcessangels,代碼行數:57,代碼來源:user_handler.go

示例13: FilterResizeImage

func FilterResizeImage(image image.Image) (image.Image, error) {
	width := image.Bounds().Max.X - image.Bounds().Min.X
	height := image.Bounds().Max.Y - image.Bounds().Min.Y
	if (width < 300 && height < 300) || height > width*13/10 {
		return image, errors.New("Bad image format")
	}
	if width > 1000 {
		image = resize.Thumbnail(600, 600, image, resize.NearestNeighbor)
	}
	return image, nil
}
開發者ID:beppeben,項目名稱:m3m3,代碼行數:11,代碼來源:global.go

示例14: calcDir

func calcDir() (lookup []imageLookup) {

	var lookupTable []imageLookup

	dirOpen, dirOpenErr := os.Open(sourceDir)
	if dirOpenErr != nil {
		log.Println("Directory (Open): ", dirOpenErr)
	}
	defer dirOpen.Close()

	dirFiles, dirFilesErr := dirOpen.Readdir(-1)
	if dirFilesErr != nil {
		log.Println("Directory (Read Files): ", dirFilesErr)
	}

	for _, file := range dirFiles {

		if !file.Mode().IsRegular() { // ensures no mode bits are set
			fmt.Printf("Unable to access %s as mode bits are set (this usually means it's a directory)...", file.Name())
			continue
		}

		if filepath.Ext(file.Name()) != ".JPG" && filepath.Ext(file.Name()) != ".jpg" {
			fmt.Printf("Unable to process %s as it does not have a JPG extension ...", file.Name())
			continue
		}

		imgFile, openErr := os.Open(sourceDir + "/" + file.Name())
		if openErr != nil {
			log.Println("Source File (Open): ", openErr)
		}

		imgData, imgFormat, decodeErr := image.Decode(imgFile)
		if decodeErr != nil {
			log.Println("Source File (Decode): ", decodeErr)
			continue
		}

		imgFile.Close() // closes the image file, to avoid "too many open files" error

		if !(imgFormat == "jpeg") { // checks if decoded format is JPEG
			fmt.Printf("Unable to decode %s as it is not JPEG\n", file.Name())
			continue
		}

		tileSizeU := uint(tileSize)
		imgSmall := resize.Thumbnail(tileSizeU, tileSizeU, imgData, resize.NearestNeighbor)
		imgColor := averageColor(imgSmall)
		fileID := imageLookup{file.Name(), imgColor}
		lookupTable = append(lookupTable, fileID)
		fmt.Printf("Processing %s ... ", file.Name())
	}
	return lookupTable
}
開發者ID:billyboar,項目名稱:GCSolutions,代碼行數:54,代碼來源:main.go

示例15: createThumbnail

func (r *Resize) createThumbnail(writer *bytes.Buffer, img *image.Image, ext string) {
	m := resize.Thumbnail(r.width, r.height, *img, r.interpolation)

	switch {
	case ext == "jpg":
		jpeg.Encode(writer, m, nil)
	case ext == "png":
		png.Encode(writer, m)
	case ext == "gif":
		gif.Encode(writer, m, nil)
	}
}
開發者ID:peteraba,項目名稱:go-blah,代碼行數:12,代碼來源:resize.go


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