Last active
July 20, 2016 18:52
-
-
Save aarti/ac5f7b3f9652368b8a7f0853fe68f78f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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