-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPixFile.go
77 lines (63 loc) · 1.56 KB
/
PixFile.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package convert
/*
画像変換コマンド
次の仕様を満たすコマンドを作って下さい
ディレクトリを指定する
指定したディレクトリ以下のJPGファイルをPNGに変換(デフォルト)
ディレクトリ以下は再帰的に処理する
変換前と変換後の画像形式を指定できる(オプション)
以下を満たすように開発してください
mainパッケージと分離する
自作パッケージと標準パッケージと準標準パッケージのみ使う
準標準パッケージ:golang.org/x以下のパッケージ
ユーザ定義型を作ってみる
GoDocを生成してみる
*/
import (
"image"
"image/gif"
"image/jpeg"
"image/png"
"os"
)
type PixConv struct {
Path string
Src string
Dest string
}
// picture file 変換関数
func PixFile(filedata PixConv) error {
// open file
file, err := os.Open(filedata.Path)
if err != nil {
return err
}
defer file.Close()
// image reading.
img, format, err := image.Decode(file)
if err != nil {
// not image
return err
}
// 元ファイルが指定外ならスキップ
if format != filedata.Src {
return nil
}
// 出力先ファイル
savefile, err := os.Create(filedata.Path + "." + filedata.Dest)
if err != nil {
return err
}
defer savefile.Close()
switch filedata.Dest {
case "jpg", "jpeg":
opts := &jpeg.Options{}
jpeg.Encode(savefile, img, opts)
case "png":
png.Encode(savefile, img)
case "gif":
opts := &gif.Options{}
gif.Encode(savefile, img, opts)
}
return nil
}