Skip to content

Commit d2d50c1

Browse files
authored
Add jpeg file output quality setting parameter (#7)
1 parent eebe47b commit d2d50c1

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ Flags:
2424
-l, --len int edge length of a cube face (default 1024)
2525
-o, --out string out file dir path (default ".")
2626
-s, --sides array list of sides splited by "," (optional)
27+
-q, --quality int jpeg file output quality ranges from 1 to 100 inclusive, higher is better (optional, default 75)
2728
```
2829
2930
``` sh
3031
# example
31-
./panorama --in ./sample_image.jpg --out ./dist --len 512 --sides left,right,top,buttom,front,back
32+
./panorama --in ./sample_image.jpg --out ./dist --len 512 --sides left,right,top,bottom,front,back
3233
```
3334
3435
### Installation

cmd/root.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
const (
2020
defaultEdgeLen = 1024
2121
maxConcurrentFiles = 10 // Adjust this number based on your system's file descriptor limit
22+
defaultJpegQuality = 75
2223
)
2324

2425
var (
@@ -27,6 +28,7 @@ var (
2728
inDirPath string
2829
edgeLen int
2930
sides []string
31+
quality int
3032

3133
validSides = []string{"front", "back", "left", "right", "top", "bottom"}
3234
semaphore = make(chan struct{}, maxConcurrentFiles)
@@ -75,6 +77,7 @@ func init() {
7577
rootCmd.Flags().StringVarP(&outFileDir, "out", "o", ".", "output file directory path")
7678
rootCmd.Flags().IntVarP(&edgeLen, "len", "l", defaultEdgeLen, "edge length of a cube face")
7779
rootCmd.Flags().StringSliceVarP(&sides, "sides", "s", []string{}, "array of sides [front,back,left,right,top,bottom] (default: all sides)")
80+
rootCmd.Flags().IntVarP(&quality, "quality", "q", defaultJpegQuality, "jpeg file output quality ranges from 1 to 100 inclusive, higher is better")
7881
}
7982

8083
func processSingleImage(inPath, outDir string, needSubdir bool) {
@@ -111,7 +114,7 @@ func processSingleImage(inPath, outDir string, needSubdir bool) {
111114
if needSubdir {
112115
outDir = filepath.Join(outDir, strings.TrimSuffix(filepath.Base(inPath), filepath.Ext(inPath)))
113116
}
114-
if err := conv.WriteImage(canvases, outDir, ext, sides); err != nil {
117+
if err := conv.WriteImage(canvases, outDir, ext, sides, quality); err != nil {
115118
progress.Lock()
116119
progress.errors = append(progress.errors, fmt.Sprintf("Error writing images for %s: %v", inPath, err))
117120
progress.Unlock()

conv/img.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func ReadImage(imagePath string) (image.Image, string, error) {
5353
return nil, "", errors.New("unsupported image format: " + ext)
5454
}
5555

56-
func WriteImage(canvases []*image.RGBA, writeDirPath, imgExt string, sides []string) error {
56+
func WriteImage(canvases []*image.RGBA, writeDirPath, imgExt string, sides []string, quality int) error {
5757
if len(canvases) != len(sides) {
5858
return errors.New("mismatched face size and sides length")
5959
}
@@ -76,7 +76,8 @@ func WriteImage(canvases []*image.RGBA, writeDirPath, imgExt string, sides []str
7676

7777
switch imgExt {
7878
case "jpeg":
79-
if err := jpeg.Encode(newFile, canvases[i], nil); err != nil {
79+
opt := jpeg.Options{Quality: quality}
80+
if err := jpeg.Encode(newFile, canvases[i], &opt); err != nil {
8081
return err
8182
}
8283
case "png":

0 commit comments

Comments
 (0)