Created
October 3, 2018 03:37
-
-
Save tmtk75/35f6a86c8c37aae47316d5bd18ecad9f to your computer and use it in GitHub Desktop.
Concatenate image files in .png vertically
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 ( | |
"flag" | |
"fmt" | |
"image" | |
"image/draw" | |
"image/png" | |
"log" | |
"math" | |
"os" | |
) | |
func main() { | |
outfn := flag.String("out", "out.png", "filename to output") | |
flag.Parse() | |
imgs := make([]image.Image, 0) | |
for _, e := range flag.Args() { | |
f, err := os.Open(e) | |
if err != nil { | |
log.Fatalf("%v", err) | |
} | |
defer f.Close() | |
i, _, err := image.Decode(f) | |
if err != nil { | |
log.Fatalf("%v", err) | |
} | |
imgs = append(imgs, i) | |
} | |
var w int | |
var h int | |
for _, e := range imgs { | |
fmt.Printf("%v\n", e.Bounds()) | |
w = int(math.Max(float64(w), float64(e.Bounds().Dx()))) | |
h += e.Bounds().Dy() | |
} | |
r := image.Rectangle{image.Pt(0, 0), image.Pt(w, h)} | |
out := image.NewRGBA(r) | |
fmt.Printf("%v\n", r) | |
var y int | |
for _, e := range imgs { | |
dr := image.Rectangle{image.Pt(0, y), image.Pt(w, y+e.Bounds().Dy())} | |
draw.Draw(out, dr, e, image.Pt(0, 0), draw.Src) | |
y += e.Bounds().Dy() | |
fmt.Printf("y: %v\n", y) | |
} | |
outf, _ := os.Create(*outfn) | |
defer outf.Close() | |
err := png.Encode(outf, out) | |
if err != nil { | |
log.Fatalf("%v", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment