Skip to content

Instantly share code, notes, and snippets.

@ajstarks
Created May 1, 2018 05:00
Show Gist options
  • Save ajstarks/78e55b92f9a62200ef5ab7211549589b to your computer and use it in GitHub Desktop.
Save ajstarks/78e55b92f9a62200ef5ab7211549589b to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"image"
"image/color"
"image/png"
"os"
"github.com/srwiley/oksvg"
"github.com/srwiley/rasterx"
)
func DrawIcon(iconPath string) image.Image {
icon, errSvg := oksvg.ReadIcon(iconPath, oksvg.IgnoreErrorMode)
if errSvg != nil {
return nil
}
w, h := int(icon.ViewBox.W), int(icon.ViewBox.H)
img := image.NewRGBA(image.Rect(0, 0, w, h))
source := image.NewUniform(color.NRGBA{0, 0, 0, 255})
scannerGV := rasterx.NewScannerGV(w, h, img, img.Bounds(), source, image.Point{0, 0})
raster := rasterx.NewDasher(w, h, scannerGV)
icon.Draw(raster, 1.0)
return img
}
func SaveIcon(iconPath string) error {
var err error
img := DrawIcon(iconPath)
if img != nil {
err = SaveToPngFile(fmt.Sprintf("%s.png", iconPath), img)
if err != nil {
return err
}
}
return err
}
func SaveToPngFile(filePath string, m image.Image) error {
// Create the file
f, err := os.Create(filePath)
if err != nil {
return err
}
defer f.Close()
// Create Writer from file
b := bufio.NewWriter(f)
// Write the image into the buffer
err = png.Encode(b, m)
if err != nil {
return err
}
err = b.Flush()
if err != nil {
return err
}
return nil
}
func main() {
for _, f := range os.Args[1:] {
err := SaveIcon(f)
if err != nil {
fmt.Printf("%v\n", err)
continue
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment