Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
xuyudong committed Sep 7, 2020
1 parent 7dd680d commit 95e9606
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
# resize
批量压缩图片

# 运行

```go
go build main.go

把main.exe放到待压缩的文件夹内,运行可执行文件,生成resize文件夹数据
```
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module go-pic-resize

go 1.14

require github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
76 changes: 76 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package main

import (
"fmt"
"github.com/nfnt/resize"
"image/jpeg"
"io/ioutil"
"log"
"os"
"strings"
)

func main() {
// open "test.jpg"
path := "./"
files, err := ioutil.ReadDir(path)
if err != nil {
log.Fatal(err)
}

pic := []string{".jpg", ".png"}
if os.Mkdir("resize", os.ModePerm); err != nil {
fmt.Println("resize"+"文件夹创建失败:", err.Error())
} else {
fmt.Println("resize" + "文件夹创建成功!")
}
for _, f := range files {
if f.IsDir() {
continue
}
for _, v := range pic {
if !strings.HasSuffix(f.Name(), v) {
continue
}
//todo 逻辑
fmt.Println(f.Name())

file, err := os.Open(f.Name())
if err != nil {
log.Fatal(err)
}

// decode jpeg into image.Image
img, err := jpeg.Decode(file)
if err != nil {
log.Fatal(err)
}
file.Close()

// resize to width 1000 using Lanczos resampling
// and preserve aspect ratio

kuan := img.Bounds().Dx() //
gao := img.Bounds().Dy() //
fmt.Println(kuan)
fmt.Println(gao)

m := resize.Resize(uint(kuan), 0, img, resize.Lanczos3)
//m := resize.Resize(1000, 0, img, resize.Lanczos3)

out, err := os.Create("resize/" + f.Name())
if err != nil {
log.Fatal(err)
}
defer out.Close()

// write new image to file
jpeg.Encode(out, m, nil)
fmt.Println("------------------")

}

}
return

}

0 comments on commit 95e9606

Please sign in to comment.