Last active
July 18, 2018 00:56
-
-
Save Battleroid/24bdd8b67c48b2207d9f49bef1b3ed7f to your computer and use it in GitHub Desktop.
This file contains 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/palette" | |
"image/draw" | |
"image/gif" | |
"log" | |
"math/rand" | |
"os" | |
"github.com/aaparella/carve" | |
"github.com/nfnt/resize" | |
) | |
func main() { | |
var resultFilename string | |
switch { | |
case len(os.Args) < 2: | |
resultFilename = "morphed.gif" | |
case len(os.Args) >= 2: | |
resultFilename = os.Args[2] | |
} | |
//if len(os.Args) < 2 { | |
// resultFilename = "morphed.gif" | |
//} else { | |
// resultFilename = os.Args[2] | |
//} | |
inFile, err := os.Open(os.Args[1]) | |
if err != nil { | |
log.Fatal(err) | |
} | |
img, _, err := image.Decode(inFile) | |
if err != nil { | |
log.Fatal(err) | |
} | |
inFile.Close() | |
img = resize.Resize(300, 300, img, resize.Lanczos3) | |
orgImages := []image.Image{} | |
for i := 0; i < 100; i++ { | |
fmt.Printf("%d/100 carving\n", i+1) | |
//filename := fmt.Sprintf("morphed-%d-%s", i, os.Args[1]) | |
//outFile, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0755) | |
//if err != nil { | |
// log.Fatal(err) | |
//} | |
//defer outFile.Close() | |
rHeight := rand.Intn(10) | |
rWidth := rand.Intn(10) | |
carved, err := carve.ReduceHeight(img, rHeight) // used to be 3 | |
carved, err = carve.ReduceWidth(carved, rWidth) | |
carved = resize.Resize(300, 300, carved, resize.NearestNeighbor) | |
//png.Encode(outFile, carved) | |
if err != nil { | |
log.Fatal(err) | |
} | |
img = carved | |
orgImages = append(orgImages, img) | |
} | |
fmt.Println("Done morphing, building GIF") | |
images := []*image.Paletted{} | |
delays := []int{} | |
colorPalette := palette.Plan9 | |
for i := 0; i < 100; i++ { | |
fmt.Printf("%d/100 framing\n", i+1) | |
//filename := fmt.Sprintf("morphed-%d-%s", i, os.Args[1]) | |
//inFile, err := os.Open(filename) | |
//if err != nil { | |
// log.Fatal(err) | |
//} | |
//img, _, err := image.Decode(inFile) | |
//if err != nil { | |
// log.Fatal(err) | |
//} | |
//inFile.Close() | |
img := orgImages[i] | |
bounds := img.Bounds() | |
plImg := image.NewPaletted(bounds, colorPalette) | |
draw.Draw(plImg, bounds, img, image.ZP, draw.Src) | |
images = append(images, plImg) | |
delays = append(delays, 0) | |
} | |
gifOutFile, err := os.OpenFile(resultFilename, os.O_CREATE|os.O_WRONLY, 0755) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer gifOutFile.Close() | |
gif.EncodeAll(gifOutFile, &gif.GIF{ | |
Image: images, | |
Delay: delays, | |
}) | |
fmt.Printf("Done, saved to %s", resultFilename) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment