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


Golang gif.Encode函數代碼示例

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


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

示例1: main

func main() {
	op := gif.Options{256, nil, nil}
	gifWriter, err := os.Create("new.gif")
	if err != nil {
		fmt.Println("something went wrong")
	}
	image1, _ := os.Open("image1.jpeg")
	image2, _ := os.Open("image2.jpeg")
	jpeg1, _ := jpeg.Decode(image1)
	jpeg2, _ := jpeg.Decode(image2)
	gif.Encode(gifWriter, jpeg1, &op)
	gif.Encode(gifWriter, jpeg2, &op)

}
開發者ID:haetze,項目名稱:DemoGO,代碼行數:14,代碼來源:createAgif.go

示例2: main

func main() {
	flag.Parse()
	runtime.GOMAXPROCS(runtime.NumCPU())

	// open a new file
	f, err := os.Create(*output)
	if err != nil {
		log.Fatal(err)
	}

	// create the image
	img := mandelbrot.Create(*height, *width)

	// and encoding it
	fmt := filepath.Ext(*output)
	switch fmt {
	case ".png":
		err = png.Encode(f, img)
	case ".jpg", ".jpeg":
		err = jpeg.Encode(f, img, nil)
	case ".gif":
		err = gif.Encode(f, img, nil)
	default:
		err = errors.New("unkwnown format " + fmt)
	}
	// unless you can't
	if err != nil {
		log.Fatal(err)
	}
}
開發者ID:nivoc,項目名稱:mandelbrot,代碼行數:30,代碼來源:main.go

示例3: encode_img

func encode_img(img image.Image, content_type string) ([]byte, error) {
	var (
		buffer bytes.Buffer
		err    error
	)

	switch content_type {
	case "image/jpeg":
		err = jpeg.Encode(&buffer, img, nil)
	case "image/gif":
		err = gif.Encode(&buffer, img, nil)
	case "image/png":
		err = png.Encode(&buffer, img)

	default:
		err = errors.New("Unknown content type " + content_type)
	}

	if err != nil {
		log.Println(err)
		return nil, err
	}

	return buffer.Bytes(), nil
}
開發者ID:Bowbaq,項目名稱:downsizr,代碼行數:25,代碼來源:resize.go

示例4: 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

示例5: resizeFile

func resizeFile(filename string, xPx, yPx float64) {
	fmt.Println("Processing", filename)

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

	img, imgFmt, err := image.Decode(file)
	check(err)

	if xPx < 1 {
		xPx = float64(img.Bounds().Size().X) * xPx
	}
	if yPx < 1 {
		yPx = float64(img.Bounds().Size().Y) * yPx
	}
	m := resize.Resize(uint(xPx), uint(yPx), img, resize.Bilinear)

	out, err := os.Create(PATH + "/" + filename)
	check(err)
	defer out.Close()

	if imgFmt == "jpeg" {
		err = jpeg.Encode(out, m, nil)
	} else if imgFmt == "gif" {
		err = gif.Encode(out, m, nil)
	} else if imgFmt == "png" {
		err = png.Encode(out, m)
	} else {
		fmt.Println("Unrecognized format:", imgFmt)
		return
	}
	check(err)
}
開發者ID:jakecoffman,項目名稱:image-resizer,代碼行數:34,代碼來源:main.go

示例6: GIF

//GIF 保存為GIF格式圖片
func (VC *VerifyCode) GIF(text string, w io.Writer) error {
	imageImage, err := VC.Draw(text)
	if err != nil {
		return err
	}
	return gif.Encode(w, imageImage, &gif.Options{NumColors: 256})
}
開發者ID:456vv,項目名稱:verifycode,代碼行數:8,代碼來源:verifycode.go

示例7: Resized

func Resized(ext string, data []byte, width, height int) (resized []byte, w int, h int) {
	if width == 0 && height == 0 {
		return data, 0, 0
	}
	srcImage, _, err := image.Decode(bytes.NewReader(data))
	if err == nil {
		bounds := srcImage.Bounds()
		var dstImage *image.NRGBA
		if bounds.Dx() > width && width != 0 || bounds.Dy() > height && height != 0 {
			if width == height && bounds.Dx() != bounds.Dy() {
				dstImage = imaging.Thumbnail(srcImage, width, height, imaging.Lanczos)
				w, h = width, height
			} else {
				dstImage = imaging.Resize(srcImage, width, height, imaging.Lanczos)
			}
		} else {
			return data, bounds.Dx(), bounds.Dy()
		}
		var buf bytes.Buffer
		switch ext {
		case ".png":
			png.Encode(&buf, dstImage)
		case ".jpg", ".jpeg":
			jpeg.Encode(&buf, dstImage, nil)
		case ".gif":
			gif.Encode(&buf, dstImage, nil)
		}
		return buf.Bytes(), dstImage.Bounds().Dx(), dstImage.Bounds().Dy()
	} else {
		glog.Error(err)
	}
	return data, 0, 0
}
開發者ID:netroby,項目名稱:seaweedfs,代碼行數:33,代碼來源:resizing.go

示例8: convertToGIF

// convertToGIF converts from any recognized format to GIF.
func convertToGIF(w io.Writer, r io.Reader) error {
	img, _, err := image.Decode(r)
	if err != nil {
		return err
	}
	return gif.Encode(w, img, &gif.Options{NumColors: 256})
}
開發者ID:collinglass,項目名稱:imgconv,代碼行數:8,代碼來源:main.go

示例9: writeGIF

func writeGIF(filename string, imgs []image.Image) error {
	f, err := os.Create(filename)
	if err != nil {
		return err
	}
	defer f.Close()

	g := gif.GIF{
		Image: make([]*image.Paletted, len(imgs)),
		Delay: make([]int, len(imgs)),
	}
	b := make([]byte, 0, 1024)
	for i, img := range imgs {
		buf := bytes.NewBuffer(b)
		if err = gif.Encode(buf, img, &gif.Options{NumColors: 256}); err != nil {
			return err
		}
		gimg, err := gif.DecodeAll(buf)
		if err != nil {
			return err
		}
		g.Delay[i] = 0
		g.Image[i] = gimg.Image[0]
	}

	return gif.EncodeAll(f, &g)
}
開發者ID:markgunnels,項目名稱:go-marvel,代碼行數:27,代碼來源:main.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: Transform

// Transform the provided image.  img should contain the raw bytes of an
// encoded image in one of the supported formats (gif, jpeg, or png).  The
// bytes of a similarly encoded image is returned.
func Transform(img []byte, opt Options) ([]byte, error) {
	if opt == emptyOptions {
		// bail if no transformation was requested
		return img, nil
	}

	// decode image
	m, format, err := image.Decode(bytes.NewReader(img))
	if err != nil {
		return nil, err
	}

	m = transformImage(m, opt)

	// encode image
	buf := new(bytes.Buffer)
	switch format {
	case "gif":
		gif.Encode(buf, m, nil)
	case "jpeg":
		quality := opt.Quality
		if quality == 0 {
			quality = defaultQuality
		}

		jpeg.Encode(buf, m, &jpeg.Options{Quality: quality})
	case "png":
		png.Encode(buf, m)
	}

	return buf.Bytes(), nil
}
開發者ID:Hapara,項目名稱:imageproxy,代碼行數:35,代碼來源:transform.go

示例12: writeImage

func writeImage(dst string, mf string, m image.Image) {
	var err error
	w := os.Stdout
	if dst != "-" {
		w, err = os.Create(dst)
		if err != nil {
			log.Fatal(err)
		}
	}
	if ext := filepath.Ext(dst); mf == "" && ext != "" {
		mf = ext[1:]
	}
	switch mf {
	default:
		err = png.Encode(w, m)
	case "jpg", "jpeg":
		err = jpeg.Encode(w, m, nil)
	case "gif":
		err = gif.Encode(w, m, nil)
	case "tif", "tiff":
		err = tiff.Encode(w, m, nil)
	case "bmp":
		err = bmp.Encode(w, m)
	}
	if err != nil {
		log.Fatal(err)
	}
}
開發者ID:crhym3,項目名稱:imgdiff,代碼行數:28,代碼來源:io.go

示例13: saveGIFToFolder

func saveGIFToFolder(name string, img image.Image, numCol int) {
	f, e := os.Create(name)
	palImg, found := img.(*image.Paletted)

	if e != nil {
		log.Println("saveGIFToFolder Failed to Create", name, e)
		return
	}

	if found {
		gif.Encode(f, palImg, &gif.Options{NumColors: len(palImg.Palette)})
	} else {
		gif.Encode(f, img, &gif.Options{NumColors: numCol})
	}
	f.Close()
}
開發者ID:Kimau,項目名稱:GoCam,代碼行數:16,代碼來源:image.go

示例14: main

func main() {
	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: %s -output format < input > output\n", filepath.Base(os.Args[0]))
		flag.PrintDefaults()
	}
	flag.Parse()

	img, kind, err := image.Decode(os.Stdin)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Fprintln(os.Stderr, "Input format =", kind)

	switch *output {
	case "gif":
		err = gif.Encode(os.Stdout, img, &gif.Options{})
	case "jpg", "jpeg":
		err = jpeg.Encode(os.Stdout, img, &jpeg.Options{Quality: 95})
	case "png":
		err = png.Encode(os.Stdout, img)
	default:
		log.Fatalf("Unsupported output image format: %s", *output)
	}

	if err != nil {
		log.Fatal(err)
	}
}
開發者ID:seikichi,項目名稱:gopl,代碼行數:28,代碼來源:main.go

示例15: Encode

// Encode writes the image img to w in the specified format (JPEG, PNG, GIF, TIFF or BMP).
func Encode(w io.Writer, img image.Image, format Format) error {
	var err error
	switch format {
	case JPEG:
		var rgba *image.RGBA
		if nrgba, ok := img.(*image.NRGBA); ok {
			if nrgba.Opaque() {
				rgba = &image.RGBA{
					Pix:    nrgba.Pix,
					Stride: nrgba.Stride,
					Rect:   nrgba.Rect,
				}
			}
		}
		if rgba != nil {
			err = jpeg.Encode(w, rgba, &jpeg.Options{Quality: 95})
		} else {
			err = jpeg.Encode(w, img, &jpeg.Options{Quality: 95})
		}

	case PNG:
		err = png.Encode(w, img)
	case GIF:
		err = gif.Encode(w, img, &gif.Options{NumColors: 256})
	case TIFF:
		err = tiff.Encode(w, img, &tiff.Options{Compression: tiff.Deflate, Predictor: true})
	case BMP:
		err = bmp.Encode(w, img)
	default:
		err = ErrUnsupportedFormat
	}
	return err
}
開發者ID:maleck13,項目名稱:locals-api,代碼行數:34,代碼來源:helpers.go


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