Skip to content

Instantly share code, notes, and snippets.

@tungbt94
Created May 5, 2018 12:05
Show Gist options
  • Save tungbt94/187a25dbfe57898d4170135a35ace317 to your computer and use it in GitHub Desktop.
Save tungbt94/187a25dbfe57898d4170135a35ace317 to your computer and use it in GitHub Desktop.
Convert image in folder to resize them
// Note: You have to install imagemagick before
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strconv"
"strings"
)
type FileInfo struct {
name string
extension string
size int64
}
func (fi FileInfo) String() string {
formatedName := rightPad(fi.name, 50)
formatedExtension := rightPad(fi.extension, 10)
formatedSize := rightPad(strconv.FormatInt(fi.size, 10), 20)
return fmt.Sprintf("%s %s %s", formatedName, formatedExtension, formatedSize)
}
func rightPad(s string, length int) string {
sLen := len(s)
if sLen >= length {
return s
}
for i := 0; i < length-sLen; i++ {
s += " "
}
return s
}
func main() {
rootDir := "your_dir"
listImageFileInDir(rootDir)
}
func listImageFileInDir(rootDir string) {
var files []FileInfo
// Scan file
err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
fileNameIndex := strings.LastIndex(path, "/") + 1
fileName := path[fileNameIndex:]
fileTypeIndex := strings.LastIndex(fileName, ".")
if fileTypeIndex > 0 {
fileType := fileName[fileTypeIndex:]
matchImageType, _ := regexp.MatchString("(png|jpg)", fileType)
if matchImageType {
fileInfo := FileInfo{
name: fileName,
extension: fileType,
size: info.Size(),
}
files = append(files, fileInfo)
}
}
return nil
})
if err != nil {
panic(err)
}
// sort by size
sort.Slice(files[:], func(i, j int) bool {
return files[i].size > files[j].size
})
for _, file := range files {
fmt.Println(file)
}
for _, file := range files {
convertImg(file.name, file.extension)
}
}
func getFileSize(file string) int64 {
fi, err := os.Stat(file)
if err != nil {
log.Fatal(err)
}
return fi.Size()
}
func convertImg(fileName string, fileType string) {
convertedFileName := "conveted_" + fileName
// open file to append
file, err := os.OpenFile("convert_result.txt", os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
log.Fatal(err)
}
defer file.Close()
w := bufio.NewWriter(file)
// convert file
fileSizeBeforeConvert := getFileSize(fileName)
fmt.Fprintf(w, "File size of %s before convert: %d\n", fileName, fileSizeBeforeConvert)
if fileType == ".png" {
_, err := exec.Command("convert", fileName, "-strip", convertedFileName).Output()
if err != nil {
log.Fatal(err)
}
fileSizeAfterConvert := getFileSize(convertedFileName)
fmt.Fprintf(w, "File size of %s after convert: %d\n", fileName, fileSizeAfterConvert)
fmt.Fprintln(w, "======================================================================")
} else if fileType == ".jpg" {
_, err := exec.Command("convert", fileName, "-sampling-factor", "4:2:0", "-strip", "-quality", "100", "-interlace", "JPEG", "-colorspace", "sRGB", convertedFileName).Output()
if err != nil {
log.Fatal(err)
}
fileSizeAfterConvert := getFileSize(convertedFileName)
fmt.Fprintf(w, "File size of %s after convert: %d\n", fileName, fileSizeAfterConvert)
fmt.Fprintln(w, "======================================================================")
}
w.Flush()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment