generated from gopherdojo/template
-
Notifications
You must be signed in to change notification settings - Fork 181
Kadai1 s-shiraki #76
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
highpon
wants to merge
10
commits into
gopherdojo:master
Choose a base branch
from
highpon:kadai1-HighPon
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
Kadai1 s-shiraki #76
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9e2d665
add flag
highpon 6bf7af4
search recursively directory
highpon fe705a9
split main package
highpon 8994ab6
selected extension path
highpon 78b2d4b
implement image convert
highpon 4c3b6e6
add README
highpon 3225d04
fix bag
highpon 751b323
delete png
highpon bab2656
delete args.go
highpon 15ac369
modified pointed out part
highpon 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 hidden or 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 @@ | ||
kadai1/image-convert |
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 hidden or 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,29 @@ | ||
# 課題 1:画像変換コマンドを作ろう | ||
|
||
画像を指定の形式に変換する | ||
変換後の画像ファイルは元の画像と同じディレクトリに配置される | ||
|
||
## コマンドラインオプション | ||
|
||
| オプション | 値の説明 | デフォルト値 | | ||
| ---------- | -------------------------------------- | ------------ | | ||
| -from | 変換前の画像形式(jpg,jpeg,pngから選択) | jpg | | ||
| -to | 変換後の画像形式(jpg,jpeg,pngから選択) | png | | ||
| -dir | ディレクトリの指定 | ./ | | ||
|
||
## 使い方 | ||
|
||
- ビルド | ||
|
||
```bash | ||
go build -o image-convert | ||
``` | ||
|
||
- ディレクトリを指定して実行 | ||
|
||
```bash | ||
./image-convert [...options] [directory] | ||
``` | ||
```example | ||
./image-convert -from png -to jpg -dir d1 | ||
``` |
This file contains hidden or 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,67 @@ | ||
package convert | ||
|
||
import ( | ||
"image" | ||
"image/jpeg" | ||
"image/png" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
func GetSelectedExtensionPath(fileType string, directory string) ([]string, error) { | ||
var retval []string | ||
err := filepath.Walk(directory, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if filepath.Ext(path) == "."+fileType { | ||
if f, err := os.Stat(path); !(os.IsNotExist(err) || f.IsDir()) { | ||
retval = append(retval, path) | ||
} | ||
} | ||
|
||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return retval, nil | ||
} | ||
|
||
func ConvertImage(fileName string, to string) error { | ||
f, err := os.Open(fileName) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
img, _, err := image.Decode(f) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
out, err := os.Create(fileName[:len(fileName)-len(filepath.Ext(fileName))+1] + to) | ||
if err != nil { | ||
return err | ||
} | ||
defer out.Close() | ||
|
||
switch to { | ||
case "jpg", "jpeg": | ||
if err := jpeg.Encode(out, img, nil); err != nil { | ||
return err | ||
} | ||
case "png": | ||
if err := png.Encode(out, img); err != nil { | ||
return err | ||
} | ||
default: | ||
|
||
} | ||
|
||
if err := os.Remove(fileName); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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 hidden or 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,12 @@ | ||
module image-convert | ||
|
||
go 1.17 | ||
|
||
require ( | ||
github.com/yuin/goldmark v1.3.5 // indirect | ||
golang.org/x/mod v0.4.2 // indirect | ||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect | ||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect | ||
golang.org/x/tools v0.1.5 // indirect | ||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect | ||
) |
This file contains hidden or 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,28 @@ | ||
github.com/yuin/goldmark v1.3.5 h1:dPmz1Snjq0kmkz159iL7S6WzdahUTHnHB5M56WFVifs= | ||
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= | ||
golang.org/x/mod v0.4.2 h1:Gz96sIWK3OalVv/I/qNygP42zyoKp3xptRVCWRFEBvo= | ||
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= | ||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= | ||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= | ||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0= | ||
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= | ||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE= | ||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= | ||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= | ||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= | ||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= | ||
golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA= | ||
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= | ||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= | ||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= | ||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= |
This file contains hidden or 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,36 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"image-convert/convert" | ||
"log" | ||
) | ||
|
||
type cmdArgs struct { | ||
from string | ||
to string | ||
dir string | ||
} | ||
|
||
func parseArgs() *cmdArgs { | ||
var cmd cmdArgs | ||
flag.StringVar(&cmd.from, "from", "jpg", "from") | ||
flag.StringVar(&cmd.to, "to", "png", "to") | ||
flag.StringVar(&cmd.dir, "dir", "./", "directory") | ||
flag.Parse() | ||
return &cmd | ||
} | ||
|
||
func main() { | ||
var args = parseArgs() | ||
var convertList, err = convert.GetSelectedExtensionPath(args.from, args.dir) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
for _, v := range convertList { | ||
err := convert.ConvertImage(v, args.to) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
} |
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.
パスはディレクトリも含んでいるため、ディレクトリではないかのチェックが必要です。
例えばxxx.jpgという名前のディレクトリがあると誤動作します。