Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 準備
// mkdir pokemon_karuta
// cd pokemon_karuta
// このファイル置く
// git clone https://github.com/fanzeyi/pokemon.json.git
// mkdir {output,compact_h,compact_v}
// 実行
// go run pokemon_karuta.go
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os/exec"
"strings"
)
type Pokemon struct {
Id int `json:"id"`
Name struct {
Jname string `json:"japanese"`
} `json:"name"`
}
func ParseJson(jsonFile string) []Pokemon {
bytes, err := ioutil.ReadFile(jsonFile)
if err != nil {
fmt.Println(err)
}
// JSONデコード
var pokemons []Pokemon
if err := json.Unmarshal(bytes, &pokemons); err != nil {
fmt.Println(err)
}
return pokemons
}
func main() {
// JSONからモンスター名を取り出し、画像に挿入する
jsonFile := "pokemon.json/pokedex.json"
pokemons := ParseJson(jsonFile)
for _, p := range pokemons {
if p.Id > 151 {
continue
}
fmt.Printf("%d : %s", p.Id, p.Name.Jname)
spaceArgsStr := fmt.Sprintf("pokemon.json/images/%03d.png -gravity center -extent 570x570 output/%03d.png", p.Id, p.Id)
spaceArgs := strings.Fields(spaceArgsStr)
err := exec.Command("convert", spaceArgs...).Run()
if err != nil {
fmt.Printf(" : 変換エラー\n")
fmt.Println(err)
continue
}
textArgsStr := fmt.Sprintf("-pointsize 80 -gravity south -font Takaoゴシック -annotate +0+10 %s output/%03d.png output/%03d.png", p.Name.Jname, p.Id, p.Id)
textArgs := strings.Fields(textArgsStr)
err = exec.Command("convert", textArgs...).Run()
if err != nil {
fmt.Printf(" : 変換エラー\n")
fmt.Println(err)
continue
}
fmt.Printf(" : 変換完了\n")
}
// ここから画像結合 まずは横
files, _ := ioutil.ReadDir("./output")
fileNames := []string{}
for _, f := range files {
//fmt.Println(f.Name())
fileNames = append(fileNames, "./output/"+f.Name())
}
compactH := [][]string{}
sliceSize := len(fileNames)
for i := 0; i < sliceSize; i += 3 {
end := i + 3
if sliceSize < end {
end = sliceSize
}
compactH = append(compactH, fileNames[i:end])
}
//fmt.Print(compactH)
for i := 0; i < len(compactH); i++ {
fmt.Printf("横に結合 : %d枚目", i+1)
ch := compactH[i]
chArgs := []string{}
chArgs = append(chArgs, "+append")
chArgs = append(chArgs, ch[:]...)
chArgs = append(chArgs, fmt.Sprintf("./compact_h/h%03d.png", i+1))
//fmt.Println(chArgs)
err := exec.Command("convert", chArgs...).Run()
if err != nil {
fmt.Printf(" : 結合エラー\n")
fmt.Println(err)
continue
}
fmt.Printf(" : 結合完了\n")
}
// 縦に結合
chfiles, _ := ioutil.ReadDir("./compact_h")
chfileNames := []string{}
for _, f := range chfiles {
chfileNames = append(chfileNames, "./compact_h/"+f.Name())
}
compactV := [][]string{}
sliceSizeV := len(chfileNames)
for i := 0; i < sliceSizeV; i += 4 {
end := i + 4
if sliceSizeV < end {
end = sliceSizeV
}
compactV = append(compactV, chfileNames[i:end])
}
for i := 0; i < len(compactV); i++ {
fmt.Printf("縦に結合 : %d枚目", i+1)
cv := compactV[i]
cvArgs := []string{}
cvArgs = append(cvArgs, "-append")
cvArgs = append(cvArgs, cv[:]...)
cvArgs = append(cvArgs, fmt.Sprintf("./compact_v/v%03d.png", i+1))
//fmt.Println(cvArgs)
err := exec.Command("convert", cvArgs...).Run()
if err != nil {
fmt.Printf(" : 結合エラー\n")
fmt.Println(err)
continue
}
fmt.Printf(" : 結合完了\n")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment