Created
May 13, 2013 14:20
-
-
Save imjasonh/5568641 to your computer and use it in GitHub Desktop.
Examples for image copying and GIF exploding in Go
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 ( | |
"flag" | |
"io" | |
"log" | |
"os" | |
) | |
var ( | |
src = flag.String("src", "", "Source file") | |
dst = flag.String("dst", "", "Destination file") | |
) | |
func main() { | |
flag.Parse() | |
// Open the source file | |
s, err := os.Open(*src) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer s.Close() | |
// Create a destination file | |
d, err := os.Create(*dst) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer d.Close() | |
// Copy all the bytes from s into d | |
_, err = io.Copy(d, s) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
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
// Reads a PNG image and copies its pixels to a new PNG image. | |
package main | |
import ( | |
"flag" | |
"fmt" | |
"image" | |
"image/png" | |
"log" | |
"os" | |
) | |
var ( | |
src = flag.String("src", "", "Source file") | |
) | |
func main() { | |
flag.Parse() | |
// Open the source file | |
s, err := os.Open(*src) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer s.Close() | |
// Decode the source file as a PNG | |
simg, err := png.Decode(s) | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Create a new RGBA image with the same bounds as simg | |
dimg := image.NewRGBA(image.Rectangle{ | |
simg.Bounds().Min, | |
simg.Bounds().Max, | |
}) | |
// For each pixel in simg, set that pixel on dimg | |
for x := simg.Bounds().Min.X; x < simg.Bounds().Max.X; x++ { | |
for y := simg.Bounds().Min.Y; y < simg.Bounds().Max.Y; y++ { | |
dimg.Set(x, y, simg.At(x, y)) | |
} | |
} | |
// Create a destination file | |
d, err := os.Create(fmt.Sprintf("%s_copy.png", *src)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer d.Close() | |
// Write the new image to the destination file | |
err = png.Encode(d, dimg) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
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
// Reads the each frame of a GIF image and writes it to a new file as PNG. | |
package main | |
import ( | |
"flag" | |
"fmt" | |
"image" | |
"image/gif" | |
"image/png" | |
"log" | |
"os" | |
) | |
var ( | |
src = flag.String("src", "", "Source file") | |
) | |
func main() { | |
flag.Parse() | |
// Open the source file | |
s, err := os.Open(*src) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer s.Close() | |
// Decode the source file as a GIF | |
// DecodeAll reads all frames of the GIF | |
simg, err := gif.DecodeAll(s) | |
if err != nil { | |
log.Fatal(err) | |
} | |
for i, frame := range simg.Image { | |
// Create a new RGBA image with the same bounds as frame | |
dimg := image.NewRGBA(image.Rectangle{ | |
frame.Bounds().Min, | |
frame.Bounds().Max, | |
}) | |
// For each pixel in frame, set that pixel on dimg | |
for x := frame.Bounds().Min.X; x < frame.Bounds().Max.X; x++ { | |
for y := frame.Bounds().Min.Y; y < frame.Bounds().Max.Y; y++ { | |
dimg.Set(x, y, frame.At(x, y)) | |
} | |
} | |
// Create a destination file | |
d, err := os.Create(fmt.Sprintf("%s_%d.png", *src, i)) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer d.Close() | |
// Write the new image to the destination file | |
err = png.Encode(d, dimg) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment