Пример #1
0
// convert a src ppm image to a dst png image, resizing to a largest dimension
// max if max != 0
func ppmToPng(src, dst string, max int) error {
	in, err := os.Open(src)
	if err != nil {
		return err
	}

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

	// resize the image if necessary
	if max != 0 {
		img = resize.Thumbnail(uint(max), uint(max), img, resize.NearestNeighbor)
	}

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

	err = png.Encode(out, img)
	if err != nil {
		return err
	}

	return nil
}
Пример #2
0
// convert a src ppm image to a dst png image, resizing to a largest dimension
// max if max != 0
func ppmToPng(src []byte, max int) ([]byte, error) {
	in := bytes.NewReader(src)

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

	// resize the image if necessary
	if max != 0 {
		img = resize.Thumbnail(uint(max), uint(max), img, resize.NearestNeighbor)
	}

	out := new(bytes.Buffer)

	err = png.Encode(out, img)
	if err != nil {
		return nil, err
	}

	return out.Bytes(), nil
}