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

課題1提出 #64

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
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# #1 Gopher道場

#1 Gopher道場用のリポジトリです。

connpass: https://mercari.connpass.com/event/83139/
Expand All @@ -7,9 +8,9 @@ connpass: https://mercari.connpass.com/event/83139/

1回目の課題を提出する場合は次のようにコードを書いて下さい。

* ブランチ名を`kadai1-tenntenn`のようにする
* `kadai1/tenntenn`のようにディレクトリを作る
* READMEに説明や文章による課題の回答を書く
* PRを送る

- ブランチ名をkadai1-tenntennのようにする
- kadai1/tenntennのようにディレクトリを作る
- READMEに説明や文章による課題の回答を書く
- PRを送る
※FBには時間がかかる可能性があります。
Empty file added kadai1/go.sum
Empty file.
13 changes: 13 additions & 0 deletions kadai1/supermarine1377/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# はじめに

これは、Gopher道場 課題1の回答です。

# 使い方

`./main {jpgファイルを格納したディレクトリ名}`

とすると、指定したディレクトリ上のjpgファイルを再帰的に検索し、pngファイルに変換します。

`./main {jpgファイルを格納したディレクトリ名} {画像拡張子名}`

とすると、jpgファイルを指定した拡張子ファイルに変換します。
3 changes: 3 additions & 0 deletions kadai1/supermarine1377/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module supermarine1377

go 1.17
Binary file added kadai1/supermarine1377/images/.DS_Store
Binary file not shown.
Binary file added kadai1/supermarine1377/images/Cat03.jpg
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.
Binary file added kadai1/supermarine1377/images/inside/Cat04.jpg
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.
Binary file added kadai1/supermarine1377/main
Binary file not shown.
56 changes: 56 additions & 0 deletions kadai1/supermarine1377/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* This command convert jpg image to png, gif, tif, etc.

Command options

- Directory name

You must place jpg images in this directory. All jpg image in this direcotory is to be converted.

- Extenstion

A extension name you want images to converted.

Example

./main images png

*/
package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"supermarine1377/processes"
)

func main() {
var dirName string
var extention string
args := os.Args
if len(args) <= 1 {
fmt.Println("no argument passed, exiting...")
os.Exit(1)
}
dirName = args[1]
if len(args) >= 3 {
extention = args[2]
} else {
extention = "png"
}
if _, err := ioutil.ReadDir(dirName); err != nil {
log.Printf("no dir %s found, exiting...", dirName)
os.Exit(1)
}
images, err := processes.GetImages(dirName)
if err != nil {
log.Println("error occured during reading images", err)
}
for _, image := range images {
if err := processes.Convert(image, extention); err != nil {
log.Println(err)
}
log.Printf("finished converting %s", image.FileName)
}
}
83 changes: 83 additions & 0 deletions kadai1/supermarine1377/processes/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// Package processes implements converting JPG image.
package processes

import (
"errors"
"image"
"image/jpeg"
"log"
"os"
"strings"
"supermarine1377/types"
)

// convert a Myimage (see types package) passed as a argumaent.
func Convert(arg types.Myimage, extension string) error {
log.Printf("started to convert %s to %s", arg.FileName, extension)

if arg.Extention != "jpg" {
err := errors.New(arg.FileName + "is not .jpg")
return err
}
reader, err := myopen(arg.FileName, arg.Path)
if err != nil {
return err
}

m, err := mydecode(arg.FileName, reader)
if err != nil {
return err
}

if err := myencode(m, arg.FileName, extension); err != nil {
return err
}

return nil
}

func myopen(name string, path string) (*os.File, error) {
log.Printf("opening %s", name)
reader, err := os.Open(path)
if err != nil {
return nil, err
}
return reader, err
}

func mydecode(name string, reader *os.File) (image.Image, error) {
log.Printf("decoding %s", name)
m, _, err := image.Decode(reader)
if err != nil {
return nil, err
}
return m, nil
}

func myencode(m image.Image, name string, extension string) error {
log.Printf("encoding %s to %s", name, extension)
new, err := newname(name, extension)
if err != nil {
return err
}
c, err := os.Create(new)
defer c.Close()
if err != nil {
return err
}
jpeg.Encode(c, m, &jpeg.Options{Quality: 100})

return nil
}

func newname(old string, extenstion string) (string, error) {
log.Printf("renaming %s", old)
var result string
var position = strings.LastIndex(old, ".")
if position == -1 {
err := errors.New("This name has no dot")
return result, err
}
result = old[0:position] + "." + extenstion
return result, nil
}
40 changes: 40 additions & 0 deletions kadai1/supermarine1377/processes/get-images.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package processes

import (
"io/fs"
"path/filepath"
"regexp"
"supermarine1377/types"
)

// implements getting image as a slice of Myimage (see types package).
func GetImages(dirName string) ([]types.Myimage, error) {
var images []types.Myimage
err := filepath.WalkDir(dirName, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
var image types.Myimage
name := d.Name()

match, err := regexp.MatchString("..+jpg", name)
if err != nil {
return err
}
if !match {
return nil
}
image.FileName = name
image.Path = path
image.Extention = "jpg"

images = append(images, image)

return nil
})

return images, err
}
10 changes: 10 additions & 0 deletions kadai1/supermarine1377/types/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
Package types implements Myimage type, which is used by other packages to represent a image file.
*/
package types

type Myimage struct {
FileName string
Path string
Extention string
}
17 changes: 0 additions & 17 deletions kadai1/toshi0607/extchanger/README.md

This file was deleted.

135 changes: 0 additions & 135 deletions kadai1/toshi0607/extchanger/converter/converter.go

This file was deleted.

Binary file removed kadai1/toshi0607/extchanger/extchanger
Binary file not shown.
Loading