Skip to content

Instantly share code, notes, and snippets.

@tsdko
Last active March 26, 2026 03:45
Show Gist options
  • Select an option

  • Save tsdko/a0e57874a5bbe9422bed1b4ee3e2aa1e to your computer and use it in GitHub Desktop.

Select an option

Save tsdko/a0e57874a5bbe9422bed1b4ee3e2aa1e to your computer and use it in GitHub Desktop.
package main
// This is free and unencumbered software released into the public domain.
//
// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.
//
// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
// For more information, please refer to <https://unlicense.org/>
import (
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"log"
"os"
"path/filepath"
"strings"
)
type CropTarget struct {
Src image.Rectangle
Holes []image.Rectangle
Name string
}
func xywh(x, y, w, h int) image.Rectangle {
return image.Rect(x, y, x+w, y+h)
}
var targets = []CropTarget{
{Src: xywh(0, 0, 32, 32), Name: "containerbg"},
{Src: xywh(32, 0, 32, 32), Holes: []image.Rectangle{xywh(40, 8, 16, 16)}, Name: "border"},
{Src: xywh(40, 8, 16, 8), Name: "arrowup"},
{Src: xywh(40, 16, 16, 8), Name: "arrowdown"},
{Src: xywh(0, 32, 16, 16), Name: "fontshadow"},
{Src: xywh(0, 48, 16, 16), Name: "font1"},
{Src: xywh(16, 48, 16, 16), Name: "font2"},
{Src: xywh(32, 48, 16, 16), Name: "font3"},
{Src: xywh(48, 48, 16, 16), Name: "font4"},
{Src: xywh(64, 48, 16, 16), Name: "font5"},
{Src: xywh(80, 48, 16, 16), Name: "font6"},
{Src: xywh(96, 48, 16, 16), Name: "font7"},
}
func loadImage(path string) (image.Image, error) {
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
img, _, err := image.Decode(f)
return img, err
}
func writeCrop(img image.Image, dir string, t CropTarget) error {
timg := image.NewNRGBA(t.Src)
draw.Draw(timg, t.Src, img, timg.Bounds().Min, draw.Src)
for _, h := range t.Holes {
draw.Draw(timg, h, image.Transparent, image.Point{}, draw.Src)
}
f, err := os.Create(filepath.Join(dir, t.Name) + ".png")
if err != nil {
return err
}
defer f.Close()
if err := png.Encode(f, timg); err != nil {
return err
}
return nil
}
func convert(imgpath string) error {
img, err := loadImage(imgpath)
if err != nil {
return err
}
dir := filepath.Base(imgpath)
if i := strings.LastIndexByte(dir, '.'); i >= 0 {
dir = dir[:i]
}
if p, ok := img.(*image.Paletted); ok {
p.Palette[0] = color.Transparent
}
if err := os.MkdirAll(dir, 0o755); err != nil {
return err
}
empty := image.NewNRGBA(image.Rect(0, 0, 1, 1))
empty.Set(0, 0, color.NRGBA{})
for _, t := range targets {
if err := writeCrop(img, dir, t); err != nil {
return fmt.Errorf("%s/%s: %w", dir, t.Name, err)
}
}
return nil
}
func main() {
for _, sys := range os.Args[1:] {
if err := convert(sys); err != nil {
log.Fatal(err)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment