Esempio n. 1
0
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)

}
Esempio n. 2
0
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)
	}
}
Esempio n. 3
0
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
}
Esempio n. 4
0
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
}
Esempio n. 5
0
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)
}
Esempio n. 6
0
//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})
}
Esempio n. 7
0
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
}
Esempio n. 8
0
// 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})
}
Esempio n. 9
0
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)
}
Esempio n. 10
0
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
}
Esempio n. 11
0
// 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
}
Esempio n. 12
0
File: io.go Progetto: crhym3/imgdiff
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)
	}
}
Esempio n. 13
0
File: image.go Progetto: Kimau/GoCam
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()
}
Esempio n. 14
0
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)
	}
}
Esempio n. 15
0
// 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
}
Esempio n. 16
0
//MakeThumbnail makes thumbnail to suffix image format with thumbnailSize.
func MakeThumbnail(encoded []byte, suffix, thumbnailSize string) []byte {
	size := strings.Split(thumbnailSize, "x")
	if len(size) != 2 {
		return nil
	}
	x, err1 := strconv.Atoi(size[0])
	y, err2 := strconv.Atoi(size[1])
	if err1 != nil || err2 != nil {
		log.Println(thumbnailSize, "is illegal format")
		return nil
	}

	file := bytes.NewReader(encoded)
	img, _, err := image.Decode(file)
	if err != nil {
		log.Println(err)
		return nil
	}
	m := resize.Resize(uint(x), uint(y), img, resize.Lanczos3)
	var out bytes.Buffer
	switch suffix {
	case "jpg", "jpeg":
		err = jpeg.Encode(&out, m, nil)
	case "png":
		err = png.Encode(&out, m)
	case "gif":
		err = gif.Encode(&out, m, nil)
	default:
		log.Println("illegal format", suffix)
	}
	if err != nil {
		log.Println(err)
	}
	return out.Bytes()
}
Esempio n. 17
0
func Fuzz(data []byte) int {
	cfg, err := gif.DecodeConfig(bytes.NewReader(data))
	if err != nil {
		return 0
	}
	if cfg.Width*cfg.Height > 1e6 {
		return 0
	}
	img, err := gif.Decode(bytes.NewReader(data))
	if err != nil {
		return 0
	}
	for c := 1; c <= 256; c += 21 {
		var w bytes.Buffer
		err = gif.Encode(&w, img, &gif.Options{NumColors: c})
		if err != nil {
			panic(err)
		}
		img1, err := gif.Decode(&w)
		if err != nil {
			panic(err)
		}
		b0 := img.Bounds()
		b1 := img1.Bounds()
		if b0.Max.X-b0.Min.X != b1.Max.X-b1.Min.X || b0.Max.Y-b0.Min.Y != b1.Max.Y-b1.Min.Y {
			fmt.Printf("img0: %#v\n", img.Bounds())
			fmt.Printf("img1: %#v\n", img1.Bounds())
			panic("bounds changed")
		}
	}
	return 1
}
Esempio n. 18
0
func (fi *FileInfo) createThumb(buffer *bytes.Buffer, c context.Context) {
	if imageTypes.MatchString(fi.Type) {
		src, _, err := image.Decode(bytes.NewReader(buffer.Bytes()))
		check(err)
		filter := gift.New(gift.ResizeToFit(
			THUMB_MAX_WIDTH,
			THUMB_MAX_HEIGHT,
			gift.LanczosResampling,
		))
		dst := image.NewNRGBA(filter.Bounds(src.Bounds()))
		filter.Draw(dst, src)
		buffer.Reset()
		bWriter := bufio.NewWriter(buffer)
		switch fi.Type {
		case "image/jpeg", "image/pjpeg":
			err = jpeg.Encode(bWriter, dst, nil)
		case "image/gif":
			err = gif.Encode(bWriter, dst, nil)
		default:
			err = png.Encode(bWriter, dst)
		}
		check(err)
		bWriter.Flush()
		thumbnailKey := fi.Key + thumbSuffix + filepath.Ext(fi.Name)
		item := &memcache.Item{
			Key:   thumbnailKey,
			Value: buffer.Bytes(),
		}
		err = memcache.Set(c, item)
		check(err)
		fi.ThumbnailKey = thumbnailKey
	}
}
Esempio n. 19
0
func writeToJP() {

	imgRect := image.Rect(0, 0, gridSize*10, gridSize*10)
	img := image.NewGray(imgRect)
	draw.Draw(img, img.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src)
	for x := 0; x < gridSize; x++ {
		for y := 0; y < gridSize; y++ {
			fill := &image.Uniform{color.White}
			if Env[x][y] == -1 {
				fill = &image.Uniform{color.Black}
			} else if Env[x][y] > 1 {
				c := color.Gray{uint8(Env[x][y] * 20)}
				fill = &image.Uniform{c}
			}

			draw.Draw(img, image.Rect((x-1)*10, (y-1)*10, x*10, y*10), fill, image.ZP, draw.Src)
		}
	}
	buf := bytes.Buffer{}
	// ok, write out the data into the new JPEG file
	err := gif.Encode(&buf, img, nil)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	tmpimg, err := gif.Decode(&buf)
	if err != nil {
		log.Printf("Skipping frame due to weird error reading the temporary gif :%s", err)
	}
	frames = append(frames, tmpimg.(*image.Paletted))
}
Esempio n. 20
0
// Encode encodes the image with the given options.
func Encode(w io.Writer, i image.Image, o Options) error {

	o.Optimize()

	if o.Width > 0 {
		i = resize.Resize(uint(float64(o.Width)*o.Dpr), o.Height, i, resize.Bicubic)
	}

	// Now write the result.
	switch {
	case o.Mime == "image/jpeg":
		jpeg.Encode(w, i, &jpeg.Options{Quality: o.Quality})
	case o.Mime == "image/png":
		png.Encode(w, i)
	case o.Mime == "image/webp":
		webp.Encode(w, i, &webp.Options{Quality: float32(o.Quality)})
	case o.Mime == "image/gif":
		gif.Encode(w, i, nil)
	default:
		return fmt.Errorf("Format %s is not supported", o.Mime)
	}

	return nil

}
Esempio n. 21
0
func toGIF(in io.Reader, out io.Writer) error {
	img, kind, err := image.Decode(in)
	if err != nil {
		return err
	}
	fmt.Fprintln(os.Stderr, "Input format =", kind)
	return gif.Encode(out, img, &gif.Options{NumColors: 128})
}
Esempio n. 22
0
func gifize(in io.Reader, out io.Writer) error {
	img, _, err := image.Decode(in)
	if err != nil {
		return err
	}

	return gif.Encode(out, img, nil)
}
Esempio n. 23
0
func createTestGif(t *testing.T, width int, height int) []byte {
	var buffer bytes.Buffer

	if err := gif.Encode(&buffer, image.NewRGBA(image.Rect(0, 0, width, height)), nil); err != nil {
		t.Fatalf("failed to create gif: %v", err.Error())
	}

	return buffer.Bytes()
}
Esempio n. 24
0
func imageEncode(fileext string, file io.Writer, img image.Image) {
	switch fileext {
	case "jpg", "jpeg":
		jpeg.Encode(file, img, nil)
	case "png":
		png.Encode(file, img)
	case "gif":
		gif.Encode(file, img, nil)
	}
}
Esempio n. 25
0
File: main.go Progetto: junzzz/irt
func createEncodeImage(format string, file *os.File, image image.Image) {
	switch format {
	case JPG:
		jpeg.Encode(file, image, nil)
	case GIF:
		gif.Encode(file, image, nil)
	case PNG:
		png.Encode(file, image)
	}
}
Esempio n. 26
0
func encodeImage(writeInto io.Writer, image image.Image, mimeType string) error {
	switch mimeType {
	case "jpeg", "pjpeg":
		return jpeg.Encode(writeInto, image, nil)
	case "gif":
		return gif.Encode(writeInto, image, nil)
	default:
		return png.Encode(writeInto, image)
	}
}
Esempio n. 27
0
func ResizeGif(wb *bytes.Buffer, r io.Reader, g *gift.GIFT, width int, height int) error {
	src, err := gif.Decode(r)
	bounds := src.Bounds()
	dst := src
	if width < bounds.Dx() || height < bounds.Dy() {
		dst = ApplyFilter(src, g)
	}
	err = gif.Encode(wb, dst, nil)
	return err
}
Esempio n. 28
0
func TestRenderImage(t *testing.T) {
	r := image.Rect(0, 0, 600, 300)
	img := image.NewGray(r)
	code, _ := EAN13FromString12("590123412345")
	code.RenderImage(img, r, 20)

	f, _ := os.Create("test.gif")
	defer f.Close()
	gif.Encode(f, img, nil)
}
Esempio n. 29
0
func (ih *ImageHelper) Encode(w io.Writer, img image.Image, fileType string) error {
	if fileType == "png" {
		return png.Encode(w, img)
	} else if fileType == "jpeg" || fileType == "jpg" {
		return jpeg.Encode(w, img, nil)
	} else if fileType == "gif" {
		return gif.Encode(w, img, nil)
	}
	log.Println("Unsupported media for encoding ", fileType)
	return errors.New("Unsupported media type")
}
Esempio n. 30
0
// creates an io.Reader of image from entities.Image
func (self Image) Reader() io.Reader {
	output := new(bytes.Buffer)
	switch self.Format {
	case Jpg:
		jpeg.Encode(output, self.Img, &jpeg.Options{85})
	case Gif:
		gif.Encode(output, self.Img, &gif.Options{256, nil, nil})
	case Png:
		png.Encode(output, self.Img)
	}
	return output
}