Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【課題1】画像変換コマンドを作ろう #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kadai1/imgconv/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
4 changes: 4 additions & 0 deletions kadai1/imgconv/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.PHONY: fmt
fmt: ## Format source code
@which goimports > /dev/null || GO111MODULE=off go get -u golang.org/x/tools/cmd/goimports
@goimports -d -w $(shell find . -type f -name '*.go')
34 changes: 34 additions & 0 deletions kadai1/imgconv/cmd/imgconv/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"flag"
"log"
"os"

"github.com/gopherdojo/dojo7/kadai1/sabe/pkg/imgconv"
)

func main() {
if err := exec(); err != nil {
log.Println(err)
os.Exit(1)
}
os.Exit(0)
}

func exec() error {
var (
dir string
fromExt string
toExt string
)
flag.StringVar(&dir, "d", ".", "target directory flag")
flag.StringVar(&fromExt, "f", "jpg", "before convert ext flag")
flag.StringVar(&toExt, "t", "png", "after convert ext flag")
flag.Parse()
imageConfList, err := imgconv.NewImageConfList(dir, fromExt, toExt)
if err != nil {
return err
}
return imageConfList.Convert()
}
3 changes: 3 additions & 0 deletions kadai1/imgconv/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/gopherdojo/dojo7/kadai1/sabe

go 1.12
1 change: 1 addition & 0 deletions kadai1/imgconv/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
github.com/gopherdojo/dojo7 v0.0.0-20190903074013-69385d40c994 h1:UbdhtSFDdFiSYOb/TkEmlRIUFQRrBlKOzVDY3+SXaSw=
Binary file added kadai1/imgconv/imgconv
Binary file not shown.
32 changes: 32 additions & 0 deletions kadai1/imgconv/pkg/imgconv/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package imgconv

import (
"image/gif"
_ "image/gif"
"image/jpeg"
_ "image/jpeg"
"image/png"
_ "image/png"
)

func (l ImageConfList) Convert() error {
for _, v := range l {
err := v.Convert()
if err != nil {
return err
}
}
return nil
}

func (imgCf ImageConf) Convert() error {
switch imgCf.ToExt {
case "png":
return png.Encode(imgCf.ToFile, imgCf.DecodedFile)
case "gif":
return gif.Encode(imgCf.ToFile, imgCf.DecodedFile, nil)
case "jpg", "jpeg":
return jpeg.Encode(imgCf.ToFile, imgCf.DecodedFile, nil)
}
return nil
}
115 changes: 115 additions & 0 deletions kadai1/imgconv/pkg/imgconv/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package imgconv

import (
"fmt"
"image"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
)

type ImageConf struct {
FromExt string
ToExt string
DecodedFile image.Image
ToFile io.Writer
}

type ImageConfList []ImageConf

// create ImageConfList
func NewImageConfList(dir, fromExt, toExt string) (ImageConfList, error) {
// read dir
paths, err := readDir(dir, strings.ToLower(fromExt))
if err != nil {
return nil, err
}
imgCfList := make(ImageConfList, len(paths))
for i, path := range paths {
imgCf := ImageConf{
FromExt: strings.ToLower(fromExt),
ToExt: strings.ToLower(toExt),
}

// set decode file
err = imgCf.SetDecodedFile(path)
if err != nil {
return nil, err
}

// set output file
err = imgCf.SetToFile(getConvertedPath(path, strings.ToLower(toExt)))
if err != nil {
return nil, err
}

imgCfList[i] = imgCf
}
return imgCfList, nil
}

// set ImageConf.DecodedFile
func (imgCf *ImageConf) SetDecodedFile(path string) (err error) {
// open file
file, err := os.Open(path)
if err != nil {
return
}
defer func() {
err = file.Close()
}()

// decode file
img, _, err := image.Decode(file)
if err != nil {
return
}
imgCf.DecodedFile = img
return
}

// set ImageConf.ToFile
func (imgCf *ImageConf) SetToFile(path string) error {
// create output file
toFile, err := os.Create(path)
if err != nil {
return err
}
imgCf.ToFile = toFile
return nil
}

// recursively read directory
func readDir(dir, fromExt string) ([]string, error) {
files, err := ioutil.ReadDir(dir)
if err != nil {
return nil, err
}

var paths []string
for _, file := range files {
if file.IsDir() {
paths2, err := readDir(filepath.Join(dir, file.Name()), fromExt)
if err != nil {
return nil, err
}
paths = append(paths, paths2...)
continue
}
extStart := strings.LastIndex(file.Name(), ".")
if extStart > 0 && strings.ToLower(file.Name()[extStart+1:]) == fromExt {
paths = append(paths, filepath.Join(dir, file.Name()))
} else {
continue
}
}
return paths, nil
}

// get converted file path
func getConvertedPath(path, toExt string) string {
extStart := strings.LastIndex(path, ".")
return fmt.Sprintf("%s.%s", path[:extStart], toExt)
}
Binary file added kadai1/imgconv/testutils/testdata/test.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added kadai1/imgconv/testutils/testdata/test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.