<commit_before>package main

import (
	"fmt"
	"github.com/RedHai/FormatImage/resize"
	"image"
	"image/png"
	"io/ioutil"
	"os"
	"path/filepath"
	"regexp"
	"strings"
)

func main() {

	if len(os.Args) < 2 {
		fmt.Println("Usage : FormatImage [image dir]")
		return
	}

	generatePairImageWithDir(os.Args[1])

}

func generatePairImageWithDir(dirName string) {

	fileInfoArr, err := ioutil.ReadDir(dirName)

	if err != nil {
		panic(err)
	}

	for i := 0; i != len(fileInfoArr); i++ {

		if fileInfoArr[i].IsDir() {
			dirNewName := filepath.Join(dirName, fileInfoArr[i].Name())
			generatePairImageWithDir(dirNewName)
		} else {
			r, _ := regexp.Compile(".*\\.png")
			if r.MatchString(fileInfoArr[i].Name()) {
				imgPath := filepath.Join(dirName, fileInfoArr[i].Name())
				generatePairImage(imgPath)
			}
		}
	}

}

func generatePairImage(imgName string) {

	f, err := os.Open(imgName)

	if err != nil {
		fmt.Println(err)
		panic(err)
	}
	defer f.Close()

	m, err := png.Decode(f)

	if err != nil {
		fmt.Println(err)
		panic(err)
	}

	newImg := resize.Resize(m, image.Rect(0, 0, m.Bounds().Max.X, m.Bounds().Max.Y),
		m.Bounds().Max.X/2, m.Bounds().Max.Y/2)

	stringArr := strings.Split(imgName, ".")

	var imgNewName string = ""

	if len(stringArr) == 2 {
		imgNewName = stringArr[0] + "@2x." + stringArr[1]
	}

	if imgNewName == "" {
		fmt.Println("inValid file name")
		return
	}

	os.Rename(imgName, imgNewName)

	outFile, fErr := os.Create(imgName)
	if fErr != nil {
		panic(fErr)
	}
	defer outFile.Close()

	err = png.Encode(outFile, newImg)

	if err != nil {
		panic(err)
	} else {
		fmt.Println(imgName, ",", imgNewName+"\n")
	}
}<commit_msg>Support upcase PNG, ignore file who has @2x already<commit_after>package main

import (
	"fmt"
	"github.com/RedHai/FormatImage/resize"
	"image"
	"image/png"
	"io/ioutil"
	"os"
	"path/filepath"
	"regexp"
	"strings"
)

func main() {

	if len(os.Args) < 2 {
		fmt.Println("Usage : FormatImage [image dir]")
		return
	}

	generatePairImageWithDir(os.Args[1])

}

func generatePairImageWithDir(dirName string) {

	fileInfoArr, err := ioutil.ReadDir(dirName)

	if err != nil {
		panic(err)
	}

	for i := 0; i != len(fileInfoArr); i++ {

		if fileInfoArr[i].IsDir() {
			dirNewName := filepath.Join(dirName, fileInfoArr[i].Name())
			generatePairImageWithDir(dirNewName)
		} else {
			rpng, _ := regexp.Compile("(.*\\.png)|(.*\\.PNG)")
			r2xpng, _ := regexp.Compile(".*[^@]@2x\\.(png|PNG)")
			if rpng.MatchString(fileInfoArr[i].Name()) {

				if r2xpng.MatchString(fileInfoArr[i].Name()) {
					continue
				}
				stringArr := strings.Split(fileInfoArr[i].Name(), ".")

				imgPath := filepath.Join(dirName, fileInfoArr[i].Name())
				img2xPath := filepath.Join(dirName, stringArr[0]+"@2x."+stringArr[1])

				_, err := os.Stat(img2xPath)
				if err == nil {
					continue
				}
				generatePairImage(imgPath)
			}
		}
	}

}

func generatePairImage(imgName string) {

	f, err := os.Open(imgName)

	if err != nil {
		fmt.Println(err)
		panic(err)
	}
	defer f.Close()

	m, err := png.Decode(f)

	if err != nil {
		fmt.Println(err)
		panic(err)
	}

	newImg := resize.Resize(m, image.Rect(0, 0, m.Bounds().Max.X, m.Bounds().Max.Y),
		m.Bounds().Max.X/2, m.Bounds().Max.Y/2)

	stringArr := strings.Split(imgName, ".")

	var imgNewName string = ""

	if len(stringArr) == 2 {
		imgNewName = stringArr[0] + "@2x." + stringArr[1]
	}

	if imgNewName == "" {
		fmt.Println("inValid file name")
		return
	}

	os.Rename(imgName, imgNewName)

	outFile, fErr := os.Create(imgName)
	if fErr != nil {
		panic(fErr)
	}
	defer outFile.Close()

	err = png.Encode(outFile, newImg)

	if err != nil {
		panic(err)
	} else {
		fmt.Println(imgName, ",", imgNewName)
	}
}
{'fraction_non_alphanumeric': 0.18174431202600216, 'fraction_numerical': 0.008396533044420368, 'mean_word_length': 3.509157509157509, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1404771', 'n_tokens_mistral': 1670, 'n_tokens_neox': 1513, 'n_words': 268}