本文整理匯總了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)
}
示例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)
}
}
示例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
}
示例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
}
示例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)
}
示例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})
}
示例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
}
示例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})
}
示例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)
}
示例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
}
示例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
}
示例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)
}
}
示例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()
}
示例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)
}
}
示例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
}