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/gkoki #63

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

import (
"fmt"
"image"
"image/gif"
_ "image/jpeg"
"image/png"
"io/ioutil"
"os"
"path/filepath"
)

//再帰的にファイルを読み込む
func GetAllFile(pathname string, ipt []string) ([]string, error) {
rd, err := ioutil.ReadDir(pathname)
if err != nil {
fmt.Println("Failed to read dir:", err)
return ipt, err
}
for _, fi := range rd {
if fi.IsDir() {
fullDir := pathname + "/" + fi.Name()
ipt, err = GetAllFile(fullDir, ipt)
if err != nil {
fmt.Println("Failed to read dir:", err)
return ipt, err
}
} else {
fullName := pathname + "/" + fi.Name()
ipt = append(ipt, fullName)
}
}
return ipt, nil
}

//画像の形式を変換する
func Conv(ipt, opt string) (s string, err error) {
s1 := "right"
file, err := os.Open(ipt)
if err != nil {
return s1, err
}
//assert(err, "Invalid image file path ")
defer file.Close()

img, _, err := image.Decode(file)
if err != nil {
return s1, err
}
//assert(err, "Failed to convert file to image.")

out, err := os.Create(opt)
if err != nil {
return s1, err
}
defer out.Close()

switch filepath.Ext(opt) {
case ".png":
err = png.Encode(out, img)
case ".gif":
err = gif.Encode(out, img, nil)
}
if err != nil {
return s1, err
}
return s1, nil
}

//func assert(err error, msg string) {
// if err != nil {
// panic(err.Error() + ":" + msg)
// }
//}
47 changes: 47 additions & 0 deletions kadai1/convert/conver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package convert

import (
"convert"
"path/filepath"
"testing"
)

func TestAllfile(t *testing.T) {
t.Helper()
var s []string
output, _ := GetAllFile("./img", s)
if len(output) != 15 {
t.Errorf("Image file does not complete readed,expected 15,but %v ", len(output)) //画像の枚数が違う
}
}

func TestTableAllfile(t *testing.T) {
t.Helper()
var s []string
var tests = []struct {
pathinput string
expect int
}{
{"./img/img_test1", 3},
{"./img/img_test2", 4},
{"./img/img_test3", 5},
}
for _, te := range tests {
output, _ := GetAllFile(te.pathinput, s)
if len(output) != te.expect {
t.Errorf("Image file does not complete readed,expected %v,but %v ", tests[1], len(output)) //画像の枚数が違う
}
}
}

func TestConv(t *testing.T) {
t.Helper()
err := convert.Conv("./img/1.0.jpg", "./img/1.0.png")
if err != nil {
t.Errorf("error happened %v", err)
}
matches, _ := filepath.Glob("./img/*.png")
if matches == nil {
t.Errorf("Image file does not converted %v", matches)
}
}
Binary file added kadai1/convert/img/1.0.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/convert/img/1.1.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/convert/img/1.2.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/convert/img/img_test1/1.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/convert/img/img_test1/2.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/convert/img/img_test1/3.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/convert/img/img_test2/1.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/convert/img/img_test2/2.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/convert/img/img_test2/3.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/convert/img/img_test2/4.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/convert/img/img_test3/1.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/convert/img/img_test3/2.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/convert/img/img_test3/3.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/convert/img/img_test3/4.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/convert/img/img_test3/5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions kadai1/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module image.go

go 1.16
Binary file added kadai1/img/1.0.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/img/1.1.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/img/1.2.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/img/img_test/1.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/img/img_test/2.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/img/img_test/3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 39 additions & 0 deletions kadai1/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*This is a training code for learning Go language.
And this code can change the format of images as you like*/
package main

import (
"bufio"
"convert"
"fmt"
"os"
"strings"
)

type oriPath struct {
inputPA string
outputPA string
}

func main() {
var a oriPath
a.inputPA = "./img"
a.outputPA = "./img"
scanner := bufio.NewScanner(os.Stdin)
fmt.Print("Input the image format you want(like png or gif):")
if !scanner.Scan() {
fmt.Println("Please input the format.")
return
}
srcPath := scanner.Text()

var s []string
s, _ = convert.GetAllFile(a.inputPA, s)
for _, i := range s {
j := strings.Replace(i, "jpg", srcPath, -1) //jpgから任意の形式へ変換する
err := convert.Conv(i, j)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
}
}
}
75 changes: 75 additions & 0 deletions kadai2/convert/conver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package convert

import (
"fmt"
"image"
"image/gif"
_ "image/jpeg"
"image/png"
"io/ioutil"
"os"
"path/filepath"
)

//再帰的にファイルを読み込む
func GetAllFile(pathname string, ipt []string) ([]string, error) {
rd, err := ioutil.ReadDir(pathname)
if err != nil {
fmt.Println("Failed to read dir:", err)
return ipt, err
}
for _, fi := range rd {
if fi.IsDir() {
fullDir := pathname + "/" + fi.Name()
ipt, err = GetAllFile(fullDir, ipt)
if err != nil {
fmt.Println("Failed to read dir:", err)
return ipt, err
}
} else {
fullName := pathname + "/" + fi.Name()
ipt = append(ipt, fullName)
}
}
return ipt, nil
}

//画像の形式を変換する
func Conv(ipt, opt string) (s string, err error) {
s1 := "right"
file, err := os.Open(ipt)
if err != nil {
return s1, err
}
//assert(err, "Invalid image file path ")
defer file.Close()

img, _, err := image.Decode(file)
if err != nil {
return s1, err
}
//assert(err, "Failed to convert file to image.")

out, err := os.Create(opt)
if err != nil {
return s1, err
}
defer out.Close()

switch filepath.Ext(opt) {
case ".png":
err = png.Encode(out, img)
case ".gif":
err = gif.Encode(out, img, nil)
}
if err != nil {
return s1, err
}
return s1, nil
}

//func assert(err error, msg string) {
// if err != nil {
// panic(err.Error() + ":" + msg)
// }
//}
47 changes: 47 additions & 0 deletions kadai2/convert/conver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package convert

import (
"convert"
"path/filepath"
"testing"
)

func TestAllfile(t *testing.T) {
t.Helper()
var s []string
output, _ := GetAllFile("./img", s)
if len(output) != 15 {
t.Errorf("Image file does not complete readed,expected 15,but %v ", len(output)) //画像の枚数が違う
}
}

func TestTableAllfile(t *testing.T) {
t.Helper()
var s []string
var tests = []struct {
pathinput string
expect int
}{
{"./img/img_test1", 3},
{"./img/img_test2", 4},
{"./img/img_test3", 5},
}
for _, te := range tests {
output, _ := GetAllFile(te.pathinput, s)
if len(output) != te.expect {
t.Errorf("Image file does not complete readed,expected %v,but %v ", tests[1], len(output)) //画像の枚数が違う
}
}
}

func TestConv(t *testing.T) {
t.Helper()
err := convert.Conv("./img/1.0.jpg", "./img/1.0.png")
if err != nil {
t.Errorf("error happened %v", err)
}
matches, _ := filepath.Glob("./img/*.png")
if matches == nil {
t.Errorf("Image file does not converted %v", matches)
}
}
Binary file added kadai2/convert/img/1.0.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/convert/img/1.1.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/convert/img/1.2.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/convert/img/img_test1/1.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/convert/img/img_test1/2.jpg
Binary file added kadai2/convert/img/img_test1/3.jpg
Binary file added kadai2/convert/img/img_test2/1.jpg
Binary file added kadai2/convert/img/img_test2/2.jpg
Binary file added kadai2/convert/img/img_test2/3.jpg
Binary file added kadai2/convert/img/img_test2/4.jpg
Binary file added kadai2/convert/img/img_test3/1.jpg
Binary file added kadai2/convert/img/img_test3/2.jpg
Binary file added kadai2/convert/img/img_test3/3.jpg
Binary file added kadai2/convert/img/img_test3/4.jpg
Binary file added kadai2/convert/img/img_test3/5.jpg
3 changes: 3 additions & 0 deletions kadai2/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module image.go

go 1.16
Binary file added kadai2/img/1.0.jpg
Binary file added kadai2/img/1.1.jpg
Binary file added kadai2/img/1.2.jpg
Binary file added kadai2/img/img_test/1.jpg
Binary file added kadai2/img/img_test/2.jpg
Binary file added kadai2/img/img_test/3.jpg
39 changes: 39 additions & 0 deletions kadai2/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*This is a training code for learning Go language.
And this code can change the format of images as you like*/
package main

import (
"bufio"
"convert"
"fmt"
"os"
"strings"
)

type oriPath struct {
inputPA string
outputPA string
}

func main() {
var a oriPath
a.inputPA = "./img"
a.outputPA = "./img"
scanner := bufio.NewScanner(os.Stdin)
fmt.Print("Input the image format you want(like png or gif):")
if !scanner.Scan() {
fmt.Println("Please input the format.")
return
}
srcPath := scanner.Text()

var s []string
s, _ = convert.GetAllFile(a.inputPA, s)
for _, i := range s {
j := strings.Replace(i, "jpg", srcPath, -1) //jpgから任意の形式へ変換する
err := convert.Conv(i, j)
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err.Error())
}
}
}