Before this, I participated in the study room of ** Gopher Dojo ** and searched for the Gopher Dojo issues that I had been interested in for a long time on github. , (Is it okay to do it ...?) I practiced.
The first issue was ** changing the extension of the image **. I want to output it for the time being, so I will write various things here. Also, from the package used, I will explain a little about flag
, ʻos,
path / filepath, ʻimage
.
# go version
go version go1.15.2 linux/amd64
# tree
.
|- conversion
| |- conversion.go
|- go.mod
|- main.go
For the time being, the repository is here
main.go
package main
import (
"cvs/conversion"
"flag"
"fmt"
"os"
"path/filepath"
)
var (
extension string
imagepath string
dirpath string
)
func main() {
flag.StringVar(&extension, "e", "jpeg", "Specifying the extension")
flag.StringVar(&imagepath, "f", "", "Specify the path of the file to be converted")
flag.StringVar(&dirpath, "d", "", "Specifying the file name after conversion")
flag.Parse()
err := conversion.ExtensionCheck(extension)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
err = conversion.FilepathCheck(imagepath)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
err = conversion.DirpathCheck(dirpath)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
f := filepath.Ext(imagepath)
err = conversion.FileExtCheck(f)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
fmt.Println("Converting ...")
err = conversion.FileExtension(extension, imagepath, dirpath)
if err != nil {
fmt.Println("Error:", err)
os.Exit(1)
}
}
conversion.go
/*
Conversion is a package for changing the extension of an image.
*/
package conversion
import (
"errors"
"fmt"
"image"
"image/gif"
"image/jpeg"
"image/png"
"os"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
)
const (
JPEG = "jpeg"
JPG = "jpg"
GIF = "gif"
PNG = "png"
)
// -Determines if the extension specified by e is supported.
func ExtensionCheck(ext string) error {
switch ext {
case JPEG, JPG, GIF, PNG:
return nil
default:
return errors.New("Extension that cannot be specified" + ext)
}
}
// -Determines if the file specified by f exists.
func FilepathCheck(imagepath string) error {
switch imagepath {
case "":
return errors.New("File is not specified")
default:
if f, err := os.Stat(imagepath); os.IsNotExist(err) || f.IsDir() {
return errors.New("File does not exist" + imagepath)
} else {
return nil
}
}
}
func DirpathCheck(dirpath string) error {
switch dirpath {
case "":
return errors.New("The converted file name is not specified")
default:
return nil
}
}
func FileExtCheck(imagepath string) error {
switch imagepath {
case "." + JPEG, "." + JPG, "." + GIF, "." + PNG:
return nil
default:
return errors.New("The specified file is not supported. :" + imagepath)
}
}
func FileExtension(extension string, imagepath string, dirpath string) error {
exFile, err := os.Open(imagepath)
defer exFile.Close()
if err != nil {
return errors.New("os.Create failed")
}
output, err := os.Create(dirpath)
defer output.Close()
if err != nil {
return errors.New("output failure")
}
img, _, Err := image.Decode(exFile)
if Err != nil {
return errors.New("Decode failure")
}
switch extension {
case JPEG, JPG:
err = jpeg.Encode(output, img, nil)
if err != nil {
return errors.New("Encode failure")
}
fmt.Println("Successful conversion")
return nil
case GIF:
err = gif.Encode(output, img, nil)
if err != nil {
return errors.New("Encode failure")
}
fmt.Println("Successful conversion")
return nil
case PNG:
err = png.Encode(output, img)
if err != nil {
return errors.New("Encode failure")
}
fmt.Println("Successful conversion")
return nil
}
return nil
}
go.mod
module cvs
go 1.15
For the time being, I made it work properly while making error handling appropriate.
As the theme of this code,
--Use go mod
--Use go doc
--Separate packages
I decided that. For the time being, the reason is
go mod
makes package splitting easier to usego doc
There are these two. But this time it's a pass.I will write about the package used this time.
If you raise the package used for the CLI command to change the extension of the image,
flag
os
path/filepath
image
there is. I will explain each of them.
flag Documentation Used to get arguments as an option from the command line
Implemented code(main.go)
var (
extension string
imagepath string
dirpath string
)
func main() {
flag.StringVar(&extension, "e", "jpeg", "Specifying the extension")
// -e "String" で、Stringを取得する/By default"jpeg"Is specified
flag.StringVar(&imagepath, "f", "", "Specify the path of the file to be converted")
flag.StringVar(&dirpath, "d", "", "Specifying the file name after conversion")
flag.Parse()
//Without Parse, it will not be parsed into a variable
~~~ omitted
How to write
flag.StringVar(&Variable name, "Optional symbol", "Default value","Description of this option")
You can get other than getting the character string (should)
os Documentation This time, I used it to save the image to see if the specified image really exists.
Implemented code(conversion.go)
func FilepathCheck(imagepath string) error {
switch imagepath {
case "":
return errors.New("File is not specified")
default:
if f, err := os.Stat(imagepath); os.IsNotExist(err) || f.IsDir() {
return errors.New("File does not exist" + imagepath)
} else {
return nil
}
}
}
//-It is a function that determines whether the file specified by f exists.
This guy can do pretty much anything.
path/filepath Documentation I'm getting the arguments of the function I introduced earlier. I'm getting the file path from string.
Implemented code(main.go)
f := filepath.Ext(imagepath)
err = conversion.FileExtCheck(f)
//Check if the imagepath really exists
image Documentation The image processing system can be done with this guy.
Implemented code(conversion.go)
img, _, Err := image.Decode(exFile)
if Err != nil {
return errors.New("Decode failure")
}
//Image decoding process
switch extension {
case JPEG, JPG:
err = jpeg.Encode(output, img, nil)
if err != nil {
return errors.New("Encode failure")
}
fmt.Println("Successful conversion")
return nil
case GIF:
err = gif.Encode(output, img, nil)
if err != nil {
return errors.New("Encode failure")
}
fmt.Println("Successful conversion")
return nil
case PNG:
err = png.Encode(output, img)
if err != nil {
return errors.New("Encode failure")
}
fmt.Println("Successful conversion")
return nil
}
//Encode according to the converted extension
It seems that image processing can be done almost only with this guy
When I actually run it, ...
# go build -o cvs
# ./cvs -e png -f sample.jpeg -d sample.png
Converting ...
Successful conversion
You can convert it like that.
By the way, even if you write it appropriately instead of .png after -d, the extension is converted properly.
If you convert it with a browser, there is no such thing as a strange script, and it is safe.
It was challenging and fun, so I wonder if I should make another command.
Then.