-
Notifications
You must be signed in to change notification settings - Fork 17
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
Kadai2 miyahara #31
Open
hikaru7719
wants to merge
3
commits into
master
Choose a base branch
from
kadai2-miyahara
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Kadai2 miyahara #31
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea/ | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# 画像変換コマンド | ||
## 課題内容 | ||
- 次の仕様を満たすコマンドを作って下さい | ||
- ディレクトリを指定する | ||
- 指定したディレクトリ以下のJPGファイルをPNGに変換(デフォルト) | ||
- ディレクトリ以下は再帰的に処理する | ||
- 変換前と変換後の画像形式を指定できる(オプション) | ||
- 以下を満たすように開発してください | ||
- mainパッケージと分離する | ||
- 自作パッケージと標準パッケージと準標準パッケージのみ使う | ||
- 準標準パッケージ:golang.org/x以下のパッケージ | ||
- ユーザ定義型を作ってみる | ||
- GoDocを生成してみる | ||
|
||
## 実行方法 | ||
実行するために以下のコマンドを実行してください。 | ||
``` | ||
go build -o conv | ||
./conv -i=[input file type] -o=[output file type] [target directory] | ||
|
||
``` | ||
`input file type` と`output file type` はjpgかpngを選択してください。 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package converter | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"image" | ||
"image/jpeg" | ||
"image/png" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// Converter Convert jpg to png or png to jpg. | ||
// In represent a input file type. | ||
// Out represent a output file type. | ||
// Directory represent a target directory. | ||
type Converter struct { | ||
in string | ||
out string | ||
directory string | ||
} | ||
|
||
// FileSet is Set of input file name and output file name. | ||
type FileSet struct { | ||
inputFileName string | ||
outputFileName string | ||
} | ||
|
||
// New return a new Converter. | ||
func New(in, out, directory string) *Converter { | ||
return &Converter{in: in, out: out, directory: directory} | ||
} | ||
|
||
// Run execute image convert function. | ||
func (c Converter) Run() int { | ||
var err error | ||
fileSetSlice, err := c.dirWalk() | ||
if err != nil { | ||
fmt.Println(err) | ||
return 1 | ||
} | ||
err = c.convert(fileSetSlice) | ||
if err != nil { | ||
fmt.Println(err) | ||
return 1 | ||
} | ||
return 0 | ||
} | ||
|
||
// dirWalk returns file set of input file name and output file in target directory. | ||
func (c Converter) dirWalk() ([]FileSet, error) { | ||
fileSetSlice := make([]FileSet, 0, 50) | ||
err := filepath.Walk(c.directory, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if filepath.Ext(info.Name()) == ("." + c.in) { | ||
inputFileName := path | ||
outputFileName := c.outputFilePath(inputFileName) | ||
fileSet := FileSet{inputFileName: inputFileName, outputFileName: outputFileName} | ||
fmt.Println(inputFileName, outputFileName) | ||
fileSetSlice = append(fileSetSlice, fileSet) | ||
} | ||
return nil | ||
}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return fileSetSlice, nil | ||
} | ||
|
||
// convert calls execute function every file set. | ||
func (c Converter) convert(fileSetSlice []FileSet) error { | ||
var err error | ||
|
||
for _, fileSet := range fileSetSlice { | ||
err = c.execute(fileSet) | ||
if err != nil { | ||
break | ||
} | ||
} | ||
|
||
return err | ||
} | ||
|
||
// execute function open file and calls encode function. | ||
func (c Converter) execute(fileset FileSet) error { | ||
var err error | ||
|
||
inputFile, err := os.Open(fileset.inputFileName) | ||
defer inputFile.Close() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
outputFile, err := os.Create(fileset.outputFileName) | ||
defer outputFile.Close() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
img, _, err := image.Decode(inputFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = c.encode(outputFile, img) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// encode returns error by encode function. | ||
// if output type is not support, Encode returns error. | ||
func (c Converter) encode(file *os.File, image image.Image) error { | ||
switch c.out { | ||
case "jpg", "jpeg": | ||
err := jpeg.Encode(file, image, &jpeg.Options{Quality: 80}) | ||
return err | ||
case "png": | ||
err := png.Encode(file, image) | ||
return err | ||
default: | ||
return errors.New("invalid output type") | ||
} | ||
} | ||
|
||
// outputFilePath returns output file path to correspond input file path. | ||
func (c Converter) outputFilePath(inputFileName string) string { | ||
stringSlice := strings.Split(inputFileName, ".") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Mac だと |
||
outputFileName := stringSlice[0] + "." + c.out | ||
return outputFileName | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://play.golang.org/p/S9YecKjLVo8
例えば、すでにあるディレクトリに対して
os.Create
を実行します。当然エラーが返ってきますが、そのとき
outputFile
の中身はどうなっていますか?その場合に
outputFile.Close()
を呼ぶとどうなるか試してみてください。