Skip to content

Commit

Permalink
v0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
joway committed Jan 10, 2019
1 parent 3012ea7 commit 3bc6524
Show file tree
Hide file tree
Showing 447 changed files with 234,075 additions and 94 deletions.
22 changes: 19 additions & 3 deletions .circleci/config.yml
Expand Up @@ -6,8 +6,24 @@ jobs:
working_directory: /go/src/github.com/joway/imagic
steps:
- checkout
- run: env GO111MODULE=on go mod download
- run: env GO111MODULE=on go mod vendor
- run: env GO111MODULE=on go mod verify
- run: sudo apt update && sudo apt install -y gcc
- run: ./dep.sh
- run: env GO111MODULE=on go build
- run: env GO111MODULE=on go test -v ./...
install:
docker:
- image: circleci/golang:1.11
steps:
- run: mkdir -p ~/.ssh/ && echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
- run: go get github.com/joway/imagic
- run: imagic version
- run: imagic compress --quality 70 --parallel 10 --suffix .comp --output ./testdata/output ./testdata/images/**/*.png
- run: imagic compress --quality 70 --parallel 10 --suffix .comp --output ./testdata/output ./testdata/images/**/*.jpg
- run: imagic resize --width 320 --parallel 10 --suffix .res --output ./testdata/output ./testdata/images/**/*.png
- run: imagic resize --width 320 --parallel 10 --suffix .res --output ./testdata/output ./testdata/images/**/*.jpg
workflows:
version: 2
pipeline:
jobs:
- build
- install
5 changes: 4 additions & 1 deletion .gitignore
@@ -1,4 +1,7 @@
testdata/output
dist
*.output.*
*.comp.*
output
imagic

# Binaries for programs and plugins
Expand Down
44 changes: 31 additions & 13 deletions README.md
@@ -1,13 +1,6 @@
# Imagic

## Purpose

A easy and fast tool to process images.

### Support image format

- png
- jpeg
An easy and fast tool to process images.

## Install

Expand All @@ -17,15 +10,40 @@ go get github.com/joway/imagic

## Usage

### compress
### Supported format

- png
- jpeg

### CLI Options

```
imagic -h
```

### Compress

```shell
imagic compress --ratio 0.7 --parallel 10 --suffix .comp --output ./testdata/output ./testdata/*.png
imagic compress -q 70 -p 10 \
-s .comp -o ./output \
./testdata/**/*.png
```

### resize
### Resize

```shell
imagic resize --ratio 0.7 --parallel 10 --suffix .comp --output ./testdata/output ./testdata/*.png
imagic resize --width 320 *.png
imagic resize -w 320 -p 10 \
-s .comp -o ./output \
./testdata/**/*.jpg
```

## Acknowledgement

- [libimagequant-go](https://github.com/joway/libimagequant-go)
- [imaging](https://github.com/disintegration/imaging)






83 changes: 38 additions & 45 deletions cmd/compress.go
Expand Up @@ -4,66 +4,59 @@ import (
"fmt"
"github.com/joway/imagic/pkg/image"
"github.com/joway/imagic/pkg/util"
"log"
"os"
"path/filepath"
"sync"

"github.com/spf13/cobra"
)

var radio *float32
var output *string
var suffix *string
var compressParallelCh chan int
var compressQuality int

func init() {
radio = compressCmd.Flags().Float32P("radio", "r", 1.0, "Source directory to read from")
output = compressCmd.Flags().StringP("output", "o", "", "Source directory to read from")
suffix = compressCmd.Flags().StringP("suffix", "s", "", "Source directory to read from")
compressCmd.Flags().IntVarP(&compressQuality, "quality", "q", 70, "Quantization of image compression")
compressCmd.Flags().IntVarP(&parallel, "parallel", "p", 4, "Number of concurrent tasks")
compressCmd.Flags().StringVarP(&output, "output", "o", "", "Output directory to write precessed images")
compressCmd.Flags().StringVarP(&suffix, "suffix", "s", "", "Suffix of precessed image filename")

rootCmd.AddCommand(compressCmd)
}

var compressCmd = &cobra.Command{
Use: "compress",
Short: "Compress images",
Long: `Compress images`,
Args: cobra.MinimumNArgs(1),
Use: "compress",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
filesPattern := args[0]
files, err := filepath.Glob(filesPattern)
if err != nil {
fmt.Print(err)
os.Exit(1)
}
compressParallelCh = make(chan int, parallel)
wg := &sync.WaitGroup{}

files := getFilesFromPatterns(args)
for _, filename := range files {
var outputPath string
if *output != "" {
absOutput, _ := filepath.Abs(*output)
outputPath = absOutput
} else {
outputPath = filepath.Dir(filename)
}
baseFilename := filepath.Base(filename)
dotPos := util.IndexOf(baseFilename, ".", true)
if dotPos == -1 {
continue
}
outputFilename := outputPath + "/" + baseFilename[:dotPos] + *suffix + baseFilename[dotPos:]

img, err := image.NewImageFromPath(filename)
if err != nil {
log.Fatal(err)
}
outImg, err := img.Compress(0.5)
if err != nil {
log.Fatal(err)
}
err = outImg.Write(outputFilename)
if err != nil {
log.Fatal(err)
}
fmt.Println(outputFilename)
compressParallelCh <- 1
wg.Add(1)
go func() {
defer wg.Done()

outputFileName := getOutputFileName(filename, suffix, output)
img, err := image.NewImageFromPath(filename)
if err != nil {
util.LogError(err)
return
}
outImg, err := img.Compress(compressQuality)
if err != nil {
util.LogError(err)
return
}
if err := outImg.Write(outputFileName); err != nil {
util.LogError(err)
return
}

util.LogInfo(fmt.Sprintf("%s compressed", filepath.Base(outputFileName)))

<-compressParallelCh
}()
}
wg.Wait()
},
}
64 changes: 64 additions & 0 deletions cmd/resize.go
@@ -0,0 +1,64 @@
package cmd

import (
"fmt"
"github.com/joway/imagic/pkg/image"
"github.com/joway/imagic/pkg/util"
"path/filepath"
"sync"

"github.com/spf13/cobra"
)

var resizeParallelCh chan int
var resizeWidth int
var resizeHeight int

func init() {
resizeCmd.Flags().IntVarP(&resizeWidth, "width", "w", 1024, "number of concurrent tasks")
resizeCmd.Flags().IntVarP(&resizeHeight, "height", "l", 0, "number of concurrent tasks")

rootCmd.AddCommand(resizeCmd)
}

var resizeCmd = &cobra.Command{
Use: "resize",
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
resizeParallelCh = make(chan int, parallel)
wg := &sync.WaitGroup{}

files := getFilesFromPatterns(args)
for _, filename := range files {
resizeParallelCh <- 1
wg.Add(1)

go func() {
defer wg.Done()

outputFileName := getOutputFileName(filename, suffix, output)
img, err := image.NewImageFromPath(filename)
if err != nil {
util.LogError(err)
return
}
outImg, err := img.Resize(resizeWidth, resizeHeight)
if err != nil {
util.LogError(err)
return
}
if err := outImg.Write(outputFileName); err != nil {
util.LogError(err)
return
}

util.LogInfo(
fmt.Sprintf("%s resized", filepath.Base(outputFileName)),
)

<-resizeParallelCh
}()
}
wg.Wait()
},
}
27 changes: 20 additions & 7 deletions cmd/root.go
@@ -1,23 +1,36 @@
package cmd

import (
"fmt"
"github.com/joway/imagic/pkg/util"
"github.com/spf13/cobra"
"os"
)

var parallel int
var output string
var suffix string

func init() {
rootCmd.PersistentFlags().IntVarP(&parallel, "parallel", "p", 4, "number of concurrent tasks")
rootCmd.PersistentFlags().StringVarP(&output, "output", "o", "", "output directory to write precessed images")
rootCmd.PersistentFlags().StringVarP(&suffix, "suffix", "s", "", "suffix of precessed image filename")
}

var rootCmd = &cobra.Command{
Use: "imagic",
Short: "Imagic is a fast and easy image tool",
Long: `A Fast and Easy image tool`,
Short: "An easy and fast tool to process images.",
Long: `
Imagic is an easy and fast tool to process images.
Created by Joway Wang (https://joway.io).
Issues on https://github.com/joway/imagic/issues.
`,
Run: func(cmd *cobra.Command, args []string) {
// Do Stuff Here
_ = cmd.Help()
},
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
util.LogFatal(err)
}
}
39 changes: 39 additions & 0 deletions cmd/util.go
@@ -0,0 +1,39 @@
package cmd

import (
"github.com/joway/imagic/pkg/util"
"path/filepath"
)

func getFilesFromPatterns(patterns []string) []string {
result := make([]string, 0)
for _, pattern := range patterns {
files := getFilesFromPattern(pattern)
result = append(result, files...)
}
return result
}

func getFilesFromPattern(pattern string) []string {
files, err := filepath.Glob(pattern)
if err != nil {
util.LogFatal(err)
}
return files
}

func getOutputFileName(filename string, suffix string, outputDir string) string {
var outputPath string
if outputDir != "" {
absOutput, _ := filepath.Abs(outputDir)
outputPath = absOutput
} else {
outputPath = filepath.Dir(filename)
}
baseFilename := filepath.Base(filename)
dotPos := util.IndexOf(baseFilename, ".", true)
if dotPos == -1 {
return outputPath + "/" + baseFilename + suffix + baseFilename
}
return outputPath + "/" + baseFilename[:dotPos] + suffix + baseFilename[dotPos:]
}
5 changes: 5 additions & 0 deletions dep.sh
@@ -0,0 +1,5 @@
# /bin/bash

GO111MODULE=on go mod download
GO111MODULE=on go mod vendor
GO111MODULE=on go mod verify
8 changes: 7 additions & 1 deletion go.mod
@@ -1,10 +1,16 @@
module github.com/joway/imagic

require (
github.com/disintegration/imaging v1.5.0
github.com/fatih/color v1.7.0
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/joway/libimagequant-go v0.1.0
github.com/mattn/go-colorable v0.0.9 // indirect
github.com/mattn/go-isatty v0.0.4 // indirect
github.com/pkg/errors v0.8.1
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3 // indirect
github.com/stretchr/testify v1.3.0
github.com/ultimate-guitar/go-imagequant v0.0.0-20180623193211-b579eaf0f7d8
golang.org/x/image v0.0.0-20181116024801-cd38e8056d9b // indirect
golang.org/x/sys v0.0.0-20190109145017-48ac38b7c8cb // indirect
)

0 comments on commit 3bc6524

Please sign in to comment.