-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,10 @@ | ||
# resize | ||
批量压缩图片 | ||
|
||
# 运行 | ||
|
||
```go | ||
go build main.go | ||
|
||
把main.exe放到待压缩的文件夹内,运行可执行文件,生成resize文件夹数据 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
} |