Skip to content

Instantly share code, notes, and snippets.

@aarti
Last active July 20, 2016 18:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aarti/ac5f7b3f9652368b8a7f0853fe68f78f to your computer and use it in GitHub Desktop.
Save aarti/ac5f7b3f9652368b8a7f0853fe68f78f to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"image"
"image/color"
"image/gif"
"image/jpeg"
"image/png"
"math/rand"
"os"
"strconv"
"github.com/chai2010/webp"
"golang.org/x/image/bmp"
"golang.org/x/image/tiff"
)
func main() {
args := os.Args
if len(args) < 4 {
fmt.Println("Usage: image-creator <width> <height> <format>")
return
}
width, _ := strconv.ParseInt(args[1], 10, 32)
height, _ := strconv.ParseInt(args[2], 10, 32)
format := args[3]
myimage := image.NewRGBA(image.Rectangle{image.Point{0, 0}, image.Point{int(width), int(height)}})
// This loop just fills the image with random data
for x := 0; x < int(width); x++ {
for y := 0; y < int(height); y++ {
c := color.RGBA{uint8(rand.Intn(255)), uint8(rand.Intn(255)), uint8(rand.Intn(255)), uint8(rand.Intn(255))}
myimage.Set(x, y, c)
}
}
switch format {
case "png":
genPNG(myimage, int(width), int(height))
case "jpeg":
genJPEG(myimage, int(width), int(height))
case "gif":
genGIF(myimage, int(width), int(height))
case "tiff":
genTIFF(myimage, int(width), int(height))
case "webp":
genWEBP(myimage, int(width), int(height))
case "bmp":
genBMP(myimage, int(width), int(height))
}
}
func genPNG(myimage image.Image, width, height int) {
mypngfile, _ := os.Create("test.png")
png.Encode(mypngfile, myimage)
fmt.Printf("Generated image test.png of size %dx%d\n", width, height)
}
func genJPEG(myimage image.Image, width, height int) {
myjpegfile, _ := os.Create("test.jpeg")
jpeg.Encode(myjpegfile, myimage, nil)
fmt.Printf("Generated image test.jpeg of size %dx%d\n", width, height)
}
func genGIF(myimage image.Image, width, height int) {
mygiffile, _ := os.Create("test.gif")
gif.Encode(mygiffile, myimage, nil)
fmt.Printf("Generated image test.gif of size %dx%d\n", width, height)
}
func genTIFF(myimage image.Image, width, height int) {
mytifffile, _ := os.Create("test.tiff")
tiff.Encode(mytifffile, myimage, nil)
fmt.Printf("Generated image test.tiff of size %dx%d\n", width, height)
}
func genWEBP(myimage image.Image, width, height int) {
mywebpfile, _ := os.Create("test.webp")
webp.Encode(mywebpfile, myimage, nil)
fmt.Printf("Generated image test.webp of size %dx%d\n", width, height)
}
func genBMP(myimage image.Image, width, height int) {
mybmpfile, _ := os.Create("test.bmp")
bmp.Encode(mybmpfile, myimage)
fmt.Printf("Generated image test.bmp of size %dx%d\n", width, height)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment