コード例 #1
0
ファイル: image.go プロジェクト: rdwilliamson/snippets
// Writes an image to a file. The extension of the file is used to determine the
// encoding method.
func CreateImage(i image.Image, file string) error {
	ext := filepath.Ext(file)
	m := mime.TypeByExtension(ext)

	switch m {
	case "image/jpeg", "image/png", "image/tiff":
	default:
		return errors.Newf("unsupported extension/mime type: %s %s", ext, m)
	}

	f, err := os.Create(file)
	if err != nil {
		return errors.Wrap(err, "could not create file")
	}
	defer f.Close()

	switch m {
	case "image/jpeg":
		err = jpeg.Encode(f, i, &jpeg.Options{Quality: 98})
	case "image/png":
		err = png.Encode(f, i)
	case "image/tiff":
		err = tiff.Encode(f, i, &tiff.Options{Compression: tiff.Deflate, Predictor: true})
	default:
		panic("unreachable")
	}
	if err != nil {
		return errors.Wrap(err, "could not encode image")
	}

	return nil
}
コード例 #2
0
func (handler *ImageUtilsHandler) Save(img image.Image, format string) ([]byte, error) {
	// Based on github.com/disintegration/imaging/helpers.go Save()
	var err error
	writer := new(bytes.Buffer)
	switch format {
	case ".jpg", ".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(writer, rgba, &jpeg.Options{Quality: 95})
		} else {
			err = jpeg.Encode(writer, img, &jpeg.Options{Quality: 95})
		}

	case ".png":
		err = png.Encode(writer, img)
	case ".tif", ".tiff":
		err = tiff.Encode(writer, img, &tiff.Options{Compression: tiff.Deflate, Predictor: true})
	case ".bmp":
		err = bmp.Encode(writer, img)
	default:
		return nil, errors.New("Invalid image format")
	}

	return writer.Bytes(), err
}
コード例 #3
0
ファイル: vgimg.go プロジェクト: vron/plotinum
// WriteTo implements the io.WriterTo interface, writing a tiff image.
func (c TiffCanvas) WriteTo(w io.Writer) (int64, error) {
	wc := writerCounter{Writer: w}
	b := bufio.NewWriter(&wc)
	if err := tiff.Encode(b, c.img, nil); err != nil {
		return wc.n, err
	}
	err := b.Flush()
	return wc.n, err
}
コード例 #4
0
ファイル: kmpress.go プロジェクト: paul-nechifor/kmpress
func WriteImage(img image.Image, path string) error {
	out, err := os.Create(path)
	if err != nil {
		return err
	}
	defer out.Close()
	tiff.Encode(out, img, &tiff.Options{tiff.Deflate, true})
	return nil
}
コード例 #5
0
ファイル: helpers.go プロジェクト: hamstah/imaging
// Save saves the image to file with the specified filename.
// The format is determined from the filename extension: "jpg" (or "jpeg"), "png", "tif" (or "tiff") and "bmp" are supported.
func Save(img image.Image, filename string) (err error) {
	format := strings.ToLower(filepath.Ext(filename))
	okay := false
	for _, ext := range []string{".jpg", ".jpeg", ".png", ".tif", ".tiff", ".bmp"} {
		if format == ext {
			okay = true
			break
		}
	}
	if !okay {
		return fmt.Errorf(`imaging: unsupported image format: "%s"`, format)
	}

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

	switch format {
	case ".jpg", ".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(file, rgba, &jpeg.Options{Quality: 95})
		} else {
			err = jpeg.Encode(file, img, &jpeg.Options{Quality: 95})
		}

	case ".png":
		err = png.Encode(file, img)
	case ".tif", ".tiff":
		err = tiff.Encode(file, img, &tiff.Options{Compression: tiff.Deflate, Predictor: true})
	case ".bmp":
		err = bmp.Encode(file, img)
	}
	return
}
コード例 #6
0
ファイル: helpers.go プロジェクト: yaoshipu/imaging
// Save saves the image to file with the specified filename.
// The format is determined from the filename extension: "jpg" (or "jpeg"), "png", "tif" (or "tiff"), "bmp", and "gif" are supported.
func Save(img image.Image, filename string) (err error) {
	format := strings.ToLower(filepath.Ext(filename))
	m, err := regexp.MatchString(`^\.(jpg|jpeg|png|tif|tiff|bmp|gif)$`, format)
	if err != nil || !m {
		err = fmt.Errorf(`imaging: unsupported image format: "%s"`, format)
		return
	}

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

	switch format {
	case ".jpg", ".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(file, rgba, &jpeg.Options{Quality: 95})
		} else {
			err = jpeg.Encode(file, img, &jpeg.Options{Quality: 95})
		}

	case ".png":
		err = png.Encode(file, img)
	case ".tif", ".tiff":
		err = tiff.Encode(file, img, &tiff.Options{Compression: tiff.Deflate, Predictor: true})
	case ".bmp":
		err = bmp.Encode(file, img)
	case ".gif":
		err = gif.Encode(file, img, &gif.Options{NumColors: 256}))
	default:
		err = fmt.Errorf(`imaging: unsupported image format: "%s"`, format)
	}
	return
}