Esempio n. 1
0
func main() {
	dir, _ := ioutil.ReadDir(".")

	var imgs []*image.Paletted
	for _, file := range dir {
		f, _ := os.Open(file.Name())
		defer f.Close()

		if ext := path.Ext(file.Name()); ext != ".jpg" && ext != ".JPG" && ext != ".jpeg" && ext != ".png" {
			fmt.Printf("File not an image: %s\n", file.Name())
		} else {
			img, err := rs.ResizePixels(f, 400, 400)
			if err != nil {
				fmt.Println(err)
			}

			var opt gif.Options
			opt.NumColors = 256

			buf := new(bytes.Buffer)
			gif.Encode(buf, img, &opt)

			g, _ := gif.DecodeAll(buf)
			imgs = append(imgs, g.Image[0])
		}
	}

	g := new(gif.GIF)

	g.Image = imgs
	g.Delay = evenDelay(100, len(g.Image))
	g.LoopCount = 1000

	var opt gif.Options
	opt.NumColors = 256

	out, err := os.Create("./output.gif")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	err = gif.EncodeAll(out, g)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	fmt.Println("Generated image to output.gif \n")
}
Esempio n. 2
0
func main() {

	out, err := os.Create("./output.gif")
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}

	dir, _ := ioutil.ReadDir(".")

	var imgs []*image.Paletted

	for _, file := range dir {
		switch ext := path.Ext(file.Name()); ext {
		case ".jpg":

			f, _ := os.Open(file.Name())
			img, _ := jpeg.Decode(f)
			var opt gif.Options
			opt.NumColors = 256

			buf := new(bytes.Buffer)
			gif.Encode(buf, img, &opt)

			g, _ := gif.DecodeAll(buf)

			imgs = append(imgs, g.Image[0])

		default:
			fmt.Println(ext)
		}
	}

	g := new(gif.GIF)

	g.Image = imgs

	var opt gif.Options
	opt.NumColors = 256
	g.Delay = []int{100, 100, 100}
	g.LoopCount = 1000

	err = gif.EncodeAll(out, g)
	if err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	fmt.Println("Generated image to output.gif \n")
}
Esempio n. 3
0
func InvertImage(input string, outfile string) {
	reader, err := os.Open(input)
	if err != nil {
		panic(err)
	}
	defer reader.Close()
	img, _, err := image.Decode(reader)
	if err != nil {
		panic(err)
	}
	bounds := img.Bounds()
	inverted := image.NewNRGBA(bounds)
	for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
		for x := bounds.Min.X; x < bounds.Max.X; x++ {
			r, g, b, a := img.At(x, y).RGBA()
			r = 255 - r>>8
			g = 255 - g>>8
			b = 255 - b>>8
			inverted.SetNRGBA(x, y, color.NRGBA{uint8(r), uint8(g), uint8(b), uint8(a)})
		}
	}
	pwd, _ := os.Getwd()
	fileName := path.Join(pwd, outfile)
	fp, err := os.Create(fileName)
	if err != nil {
		panic(err)
	}
	idx := strings.LastIndex(fileName, ".")
	if idx == -1 {
		panic(errors.New("you dont have a supported output file"))
	}
	ext := fileName[idx+1:]
	if ext == "png" {
		err = png.Encode(fp, inverted)
		if err != nil {
			panic(err)
		}
	} else if ext == "jpeg" || ext == "jpg" {
		var opt jpeg.Options
		opt.Quality = 100
		err = jpeg.Encode(fp, inverted, &opt)
		if err != nil {
			panic(err)
		}
	} else if ext == "gif" {
		var opt gif.Options
		opt.NumColors = 256
		err = gif.Encode(fp, inverted, &opt)
	} else {
		panic(errors.New("you dont have a supported output file"))
	}
}
Esempio n. 4
0
func WriteImage(img image.Image, ext string, w io.Writer) error {
	var err error
	if ext == "png" {
		err = png.Encode(w, img)
		if err != nil {
			return err
		}
	} else if ext == "jpeg" || ext == "jpg" {
		var opt jpeg.Options
		opt.Quality = 100
		err = jpeg.Encode(w, img, &opt)
		if err != nil {
			return err
		}
	} else if ext == "gif" {
		var opt gif.Options
		opt.NumColors = 256
		err = gif.Encode(w, img, &opt)
	} else {
		return errors.New("you dont have a supported output file")
	}
	return nil
}
Esempio n. 5
0
// generate one thumbnail
func generateThumbnail(image_file string, overwrite bool) error {
	var err error
	if IsDirectory(thumbs_path) {
		source_image := folder_path + "/" + image_file

		mime, err := getContentType(source_image)
		if err != nil {
			return err
		}

		for thumb_folder, thumb_size := range thumb_sizes {
			thumb_folder_path := thumbs_path + "/" + thumb_folder
			if !Exists(thumb_folder_path) {
				log.Println("Creating folder" + thumb_folder_path)
				err := os.Mkdir(thumb_folder_path, 0755)
				if err != nil {
					return err
				}
			}

			if IsDirectory(thumb_folder_path) {
				thumb_file_path := thumb_folder_path + "/" + image_file

				width, height, exact_size, err := parseSize(thumb_size)
				if err != nil {
					return err
				}

				if Exists(thumb_file_path) && overwrite == false {
					log.Printf("Nothing to do, thumb %s already exists\n", thumb_file_path)
				} else {
					var img image.Image

					file, err := os.Open(source_image)
					if err != nil {
						return err
					}

					if mime == "image/jpeg" {
						img, err = jpeg.Decode(file)
						if err != nil {
							return err
						}
					} else if mime == "image/gif" {
						img, err = gif.Decode(file)
						if err != nil {
							return err
						}
					} else if mime == "image/png" {
						img, err = png.Decode(file)
						if err != nil {
							return err
						}
					}

					file.Close()

					var resized_image image.Image

					if exact_size {
						img_width, img_height, err := getImageDimensions(source_image)
						if err != nil {
							return err
						}

						img_ratio := float64(img_width) / float64(img_height)

						thumb_ratio := float64(width) / float64(height)

						resize_width := uint(width)
						resize_height := uint(height)

						if img_ratio > thumb_ratio {
							resize_width = uint(img_width * 5)
						} else {
							resize_height = uint(img_height * 5)
						}

						image := resize.Thumbnail(resize_width, resize_height, img, resize.Lanczos3)

						resized_image, err = cutter.Crop(image, cutter.Config{
							Width:  width,
							Height: height,
						})
						if err != nil {
							return err
						}

					} else {
						resized_image = resize.Thumbnail(uint(width), uint(height), img, resize.Lanczos3)
					}

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

					defer out.Close()

					if mime == "image/jpeg" {
						var jpeg_opt jpeg.Options
						jpeg_opt.Quality = viper.GetInt("jpeg_quality")
						jpeg.Encode(out, resized_image, &jpeg_opt)
					} else if mime == "image/gif" {
						var gif_opt gif.Options
						gif_opt.NumColors = 256

						gif.Encode(out, resized_image, &gif_opt)
					} else if mime == "image/png" {
						png.Encode(out, resized_image)
					}
				}
			} else {
				return errors.New("Can't create thumbnails. " + thumb_folder_path + " must be a directory")
			}
		}
	} else {
		return errors.New("Thumbs folder doesn't exist or is not a Folder")
	}

	return err
}