-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (60 loc) · 2.25 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
FOR J.A.R.V.I.S. project
original project https://github.com/Priler/jarvis
made by cyberuser0x33 https://github.com/cyberuser0x33
*/
package main
import (
"github.com/disintegration/imaging"
"log"
"os"
"path/filepath"
"strings"
)
func main() {
// Путь к папке c файлами
// важно!!! путь к папке в Windows нужно указывать чере двойной слэш "\\"
// как пример D:\\folder\\files
filesDir := "Путь к папке с файлами"
files, err := os.ReadDir(filesDir)
if err != nil {
log.Fatalf("Error reading directory: %v", err)
}
// Создаем папку "output" внутри папки с файлами
outputDir := filepath.Join(filesDir, "output")
err = os.MkdirAll(outputDir, 0755)
if err != nil {
log.Fatalf("Error creating output directory: %v", err)
}
for _, file := range files {
// Проверяем, является ли файл графическим файлом поддерживаемого формата
if !file.IsDir() && isValidImageFile(file.Name()) {
inputPath := filepath.Join(filesDir, file.Name())
outputPath := filepath.Join(outputDir, file.Name()[:len(file.Name())-len(filepath.Ext(file.Name()))]+".jpg")
convertFile(inputPath, outputPath)
}
}
}
func isValidImageFile(filename string) bool {
ext := strings.ToLower(filepath.Ext(filename))
return ext == ".jpg" || ext == ".png" || ext == ".jfif"
}
func convertFile(inputPath, outputPath string) {
inputImage, err := imaging.Open(inputPath)
if err != nil {
log.Printf("Error opening file %s: %v", inputPath, err)
return
}
outputFile, err := os.Create(outputPath)
if err != nil {
log.Printf("Error creating file %s: %v", outputPath, err)
return
}
defer outputFile.Close()
err = imaging.Encode(outputFile, inputImage, imaging.JPEG, imaging.JPEGQuality(100))
if err != nil {
log.Printf("Error encoding file %s: %v", outputPath, err)
return
}
log.Printf("Converted %s to %s", inputPath, outputPath)
}