Skip to content

Kadai1-uetsu #14

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

Open
wants to merge 4 commits 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
50 changes: 50 additions & 0 deletions kadai1/uetsu/imgConv/convert/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package convert

import (
"image"
"image/jpeg"
"image/png"
"os"
"path/filepath"
"strings"
)

// InputExt 拡張子の変換前と変換後を格納する。
type InputExt struct {
Src string
Dst string
}

// ImgConv ファイルをjpgからpngに変換する。
// TODO: 他の拡張子に対応
func ImgConv(filePath string, inputExt InputExt) (convertedFilePath string, err error) {
file, err := os.Open(filePath)
if err != nil {
return
}
defer file.Close()

var decodeImage image.Image
switch inputExt.Src {
case "jpg", "jpeg":
decodeImage, err = jpeg.Decode(file)
}
if err != nil {
return
}

convertedFilePath = strings.Join([]string{filePath[:len(filePath)-len(filepath.Ext(filePath))], inputExt.Dst}, ".")

dstfile, err := os.Create(convertedFilePath)
if err != nil {
return
}
defer dstfile.Close()

err = png.Encode(dstfile, decodeImage)
if err != nil {
return
}

return
}
19 changes: 19 additions & 0 deletions kadai1/uetsu/imgConv/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2019 masashi.uetsu. All rights reserved.

/*
指定したディレクトリ配下のjpegファイルを再帰的に探索してpngファイルに変換します。ディレクトリは並べて複数指定可能です。

Usage

imgconv dirPath dirPath

func searchfile.RecursionFile

RecursionFile(dirPath string) (filePathList []string)

func convert.ImgConv

ImgConv(filePath string) (convertedFilePath string, err error)

*/
package main
33 changes: 33 additions & 0 deletions kadai1/uetsu/imgConv/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"flag"
"fmt"

"dojo6/kadai1/uetsu/ImgConv/convert"
"dojo6/kadai1/uetsu/imgConv/searchfile"
)

func main() {
flag.Parse()
args := flag.Args()

inputExt := convert.InputExt{
"jpg",
"png",
}

for _, dirPath := range args {
filePathList := searchfile.RecursionFile(dirPath)

for _, filePath := range filePathList {
convertedFilePath, err := convert.ImgConv(filePath, inputExt)
if err != nil {
fmt.Printf("%s convert fail\n", filePath)
continue
}
fmt.Printf("%s convert succeed => %s\n", filePath, convertedFilePath)
}

}
}
28 changes: 28 additions & 0 deletions kadai1/uetsu/imgConv/searchfile/searchfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package searchfile

import (
"io/ioutil"
"path/filepath"
)

// RecursionFile 再帰的にファイル名の一覧を取得する
func RecursionFile(dirPath string) (filePathList []string) {

files, err := ioutil.ReadDir(dirPath)
if err != nil {
panic(err)
}

for _, file := range files {
if file.IsDir() {
filePathList = append(filePathList, RecursionFile(filepath.Join(dirPath, file.Name()))...)
continue
}
if filepath.Ext(file.Name()) != ".jpg" {
continue
}
filePathList = append(filePathList, filepath.Join(dirPath, file.Name()))
}

return
}
44 changes: 44 additions & 0 deletions kadai1/uetsu/imgConv/searchfile/searchfile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package searchfile_test

import (
"dojo6/kadai1/uetsu/ImgConv/searchfile"
"sort"
"testing"
)

var TestCase = []struct {
in string
out []string
}{
{"sample", []string{"sample/hoge.jpg"}},
{"sample2", []string{"sample2/hoge.jpg", "sample2/hogo/hoge2.jpg"}},
}

func Test_RecursionFile(t *testing.T) {
for _, tt := range TestCase {
expect := tt.out
actual := searchfile.RecursionFile(tt.in)
sort.Strings(expect)
sort.Strings(actual)
if len(actual) != len(expect) {
t.Errorf(`expect="%s" actual="%s"`, expect, actual)
break
}
for i := range expect {
if actual[i] != expect[i] {
t.Errorf(`expect="%s" actual="%s"`, expect, actual)
break
}
}
}
}
func Test_FileNotExists(t *testing.T) {
t.Helper()
defer func() {
err := recover()
if err == nil {
t.Errorf("Unexpected File Exists")
}
}()
searchfile.RecursionFile("test")
}