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

Kadai2 itsumura #24

Open
wants to merge 5 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
40 changes: 40 additions & 0 deletions kadai1/itsumura/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
imgChange
====

jpgファイルをpngに変換したり、pngファイルをjpgに変換するプログラムです。

## Requirement 依存性
多分無し

## Usage 使い方
1. imgChangeファイルを画像があるフォルダと同じディレクトリに置きます。

2. コマンドからプログラムを起動します
```
$ ./imgChange
```
3. カレントディレクトリのファイル一覧が表示されるので、その中から画像ファイルが入っているフォルダ正しく入力します。
```
[changefunc imgChange.go pic]
pic ←入力する
```

4. 変換先のファイル形式を入力します
```
変換先のファイル形式を選んで下さい
jpg or png
png ←入力する
```

5. 変換が実行されます。

## Install
imgChangeの実行ファイルを置くだけ。Linuxなら.shの拡張子を、Windowsなら.exeの拡張子をつけてください。

## Licence

[MIT](https://github.com/tcnksm/tool/blob/master/LICENCE)

## Author

[dumblepy](https://github.com/dumblepy)
Binary file added kadai1/itsumura/bin/imgChange
Binary file not shown.
Binary file added kadai1/itsumura/pic/mysql.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/itsumura/pic/mysql.png
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/itsumura/pic/php.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/itsumura/pic/php.png
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/itsumura/pic/pic2/docker.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/itsumura/pic/pic2/docker.png
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/itsumura/pic/pic2/pic3/gopher.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/itsumura/pic/pic2/pic3/gopher.png
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/itsumura/pic/pic2/pic3/ruby.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/itsumura/pic/pic2/pic3/ruby.png
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/itsumura/pic/pic2/python.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/itsumura/pic/pic2/python.png
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/itsumura/pic/tux.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/itsumura/pic/tux.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions kadai1/itsumura/src/changefunc/jpgToPng.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
Package changefunc :Encode jpg or png data to other type of image.

jpgまたはpngファイルを、他方のファイル形式に変換します。

jpg => png

png => jpg

*/
package changefunc

import (
"fmt"
"image"
"image/png"
"os"
"strings"
)

//If error happen, fire panic.
//エラーが起きた時にpanicを起こす関数
func ec(err *error) {
if *err != nil {
fmt.Println(*err)
panic(*err)
}
}

// JpgToPng :Encode Jpg file to Png file and return number of files which are successed to encode.JpgをPngに変換する関数
func JpgToPng(paths []string, filetype string) int {
success := 0
fmt.Println("jpgToPng実行")

for _, inputPath := range paths {
newFileNameSlice := strings.Split(inputPath, ".")

var err error

//try
if newFileNameSlice[len(newFileNameSlice)-1] != "jpg" &&
newFileNameSlice[len(newFileNameSlice)-1] != "jpeg" {
fmt.Println(inputPath + "はjpgファイルでないためスキップ")
continue
}

//ファイル名を変更
outputPath := ""
for i := 0; i < len(newFileNameSlice)-1; i++ {
outputPath += newFileNameSlice[i]
}
outputPath = outputPath + ".png"
fmt.Println(inputPath + "から" + outputPath + "を作成しています")

//出力ファイル生成
out, err := os.Create(outputPath)

//入力ファイルを開く
file, err := os.Open(inputPath)

//画像のバイナリデータ
img, _, err := image.Decode(file)

if err == nil {
err := png.Encode(out, img)
if err == nil {
success++
}
}
ec(&err)
}
return success
}
58 changes: 58 additions & 0 deletions kadai1/itsumura/src/changefunc/pngToJpg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package changefunc

import (
"fmt"
"image"
"image/jpeg"
"os"
"strings"
)

//PngToJpg :Encode Png file to Jpg file and return number of files which are successed to encode.
func PngToJpg(paths []string, filetype string) int {
success := 0
fmt.Println("PngToJpg実行")

for _, inputPath := range paths {
newFileNameSlice := strings.Split(inputPath, ".")

var err error

//try
if newFileNameSlice[len(newFileNameSlice)-1] != "png" {
fmt.Println(inputPath + "はpngファイルでないためスキップ")
continue
}

//ファイル名を変更
outputPath := ""
for i := 0; i < len(newFileNameSlice)-1; i++ {
outputPath += newFileNameSlice[i]
}
outputPath = outputPath + ".jpg"
fmt.Println(inputPath + "から" + outputPath + "を作成しています")

//出力ファイル生成
out, err := os.Create(outputPath)

//入力ファイルを開く
file, err := os.Open(inputPath)

//画像のバイナリデータ
img, _, err := image.Decode(file)

//変換のオプション 初期値nil
opts := &jpeg.Options{Quality: 100}

if err == nil {
err := jpeg.Encode(out, img, opts)
if err == nil {
success++
}
}
if err != nil {
continue
}
}
return success
}
139 changes: 139 additions & 0 deletions kadai1/itsumura/src/imgChange.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
ディレクトリを指定する
指定したディレクトリ以下のJPGファイルをPNGに変換
ディレクトリ以下は再帰的に処理する
変換前と変換後の画像形式を指定できる

mainパッケージと分離する
自作パッケージと標準パッケージと準標準パッケージのみ使う
準標準パッケージ:golang.org/x以下のパッケージ
ユーザ定義型を作ってみる
GoDocを生成してみる
*/

package main

import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"

"./changefunc"
)

//エラーハンドリング
func ec(err *error) {
if *err != nil {
fmt.Println(*err)
panic(*err)
}
}

//ユーザー定義型
type fileName string

//拡張子を表示
func (fileName fileName) Extension() string {
return strings.Split(string(fileName), ".")[1]
}

func main() {

for {
fmt.Println("以下の中から画像を変換するディレクトリを選んでください")

var fileNameSlice []string
files, _ := ioutil.ReadDir("./") //カレントディレクトリをls
for _, file := range files {
fileNameSlice = append(fileNameSlice, file.Name())
}
fmt.Println(fileNameSlice) //カレントディレクトリのファイル一覧を表示

var input string
fmt.Scan(&input) //標準入力から文字列取得

//検索
var err error
results, err := searchDir(input)
//エラーが起きたらもう一度
if err != nil {
fmt.Println("正しく入力してください")
continue
}

fmt.Println(results) //選択したディレクトリ以下のファイルを全表示
check := true

for _, result := range results {
filename := fileName(result)

if filename.Extension() != "jpg" &&
filename.Extension() != "jpeg" &&
filename.Extension() != "png" {
check = false
}
/*newFileNameSlice := strings.Split(result, ".")

if newFileNameSlice[len(newFileNameSlice)-1] != "jpg" &&
newFileNameSlice[len(newFileNameSlice)-1] != "jpeg" &&
newFileNameSlice[len(newFileNameSlice)-1] != "png" {
check = false
}
*/
}
if check == false {
fmt.Println("変換可能な画像ファイルがありません")
continue
}

//resultに入ってるパスの画像を変換
input = ""
for {
fmt.Println("変換先のファイル形式を選んで下さい")
fmt.Println("jpg or png")

fmt.Scan(&input) //標準入力から文字列取得

if input != "jpg" &&
input != "png" {
fmt.Println("正しく入力してください")
continue
} else {
break
}
}
fmt.Println(input + "を選択") //変換ファイル形式

//画像変換処理
success := 0
if input == "jpg" {
success = changefunc.PngToJpg(results, input)
} else if input == "png" {
success = changefunc.JpgToPng(results, input)
}

if success > 0 {
fmt.Println("完了")
break
} else {
continue
}
}
}

func searchDir(dir string) ([]string, error) {
files, err := ioutil.ReadDir(dir)

var paths []string
for _, file := range files {
if file.IsDir() {
var path []string
path, err = searchDir(filepath.Join(dir, file.Name()))
paths = append(paths, path...)
continue
}
paths = append(paths, filepath.Join(dir, file.Name()))
}
return paths, err
}
19 changes: 19 additions & 0 deletions kadai2/itsumura/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# テスト

実行コマンド
```
go test -run ""
```

テストカバレッジ表示
```
go test -cover ""
```

# io.Readerとio.Writer
## 標準パッケージでの使われ方
- 標準パッケージでの関数で、引数として使われていた
- 引数として渡された値は、Readerではbufio.NewReader()やbufio.NewScanner()で読み込まれていた

## メリット
- 成功したか否かを取得し、エラーハンドリングを用意にするため?
Binary file added kadai2/itsumura/bin/imgChange
Binary file not shown.
Binary file added kadai2/itsumura/pic/mysql.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 kadai2/itsumura/pic/mysql.png
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 kadai2/itsumura/pic/php.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 kadai2/itsumura/pic/php.png
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 kadai2/itsumura/pic/pic2/docker.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 kadai2/itsumura/pic/pic2/docker.png
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 kadai2/itsumura/pic/pic2/pic3/gopher.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 kadai2/itsumura/pic/pic2/pic3/gopher.png
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 kadai2/itsumura/pic/pic2/pic3/ruby.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 kadai2/itsumura/pic/pic2/pic3/ruby.png
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 kadai2/itsumura/pic/pic2/python.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 kadai2/itsumura/pic/pic2/python.png
Binary file added kadai2/itsumura/pic/tux.jpg
Binary file added kadai2/itsumura/pic/tux.png
Loading