Add support for png and gif without alpha channels

This commit is contained in:
Your Name 2022-11-20 15:48:11 -05:00
parent 626ba8d0ef
commit 92f16b2d37
1 changed files with 16 additions and 4 deletions

16
main.go
View File

@ -3,7 +3,9 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"image/gif"
"image/jpeg" "image/jpeg"
"image/png"
"io/fs" "io/fs"
"os" "os"
"path/filepath" "path/filepath"
@ -18,6 +20,8 @@ type config struct {
Quality uint Quality uint
Replace bool Replace bool
StartingPath string StartingPath string
PNG bool
GIF bool
Quiet bool Quiet bool
AtLeast uint AtLeast uint
MaxWidth uint MaxWidth uint
@ -40,6 +44,8 @@ func main() {
flag.UintVar(&c.Diff, "diff", 25, "Percent difference required to replace original image.") flag.UintVar(&c.Diff, "diff", 25, "Percent difference required to replace original image.")
flag.UintVar(&c.Quality, "quality", 85, "JPG compression quality level.") flag.UintVar(&c.Quality, "quality", 85, "JPG compression quality level.")
flag.BoolVar(&c.Quiet, "quiet", false, "Less output - don't print per-file detail") flag.BoolVar(&c.Quiet, "quiet", false, "Less output - don't print per-file detail")
flag.BoolVar(&c.PNG, "png", false, "Convert PNG files without transparency to JPG if the other criteria are met")
flag.BoolVar(&c.GIF, "gif", false, "Convert GIF files without transparency to JPG if the other criteria are met")
flag.UintVar(&c.AtLeast, "atleast", 0, "Ignore images that aren't at least this many kilobytes") flag.UintVar(&c.AtLeast, "atleast", 0, "Ignore images that aren't at least this many kilobytes")
flag.BoolVar(&c.IgnoreSuffix, "ignoresuffix", false, "Ignore suffix on filenames and attempt to treat everything as a JPG") flag.BoolVar(&c.IgnoreSuffix, "ignoresuffix", false, "Ignore suffix on filenames and attempt to treat everything as a JPG")
flag.UintVar(&c.MaxWidth, "maxwidth", 0, "Maximum width, scale images bigger to fit within this width. 0 ignores. Won't replace unless it meets the diff threashold.") flag.UintVar(&c.MaxWidth, "maxwidth", 0, "Maximum width, scale images bigger to fit within this width. 0 ignores. Won't replace unless it meets the diff threashold.")
@ -77,7 +83,7 @@ func main() {
for _, f := range d { for _, f := range d {
if !f.IsDir() { if !f.IsDir() {
ext := strings.ToLower(filepath.Ext(f.Name())) ext := strings.ToLower(filepath.Ext(f.Name()))
if c.IgnoreSuffix || ext == ".jpg" || ext == ".jpeg" { if c.IgnoreSuffix || ext == ".jpg" || ext == ".jpeg" || ext==".png" || ext==".gif" {
filelist = append(filelist, f.Name()) filelist = append(filelist, f.Name())
} }
@ -86,7 +92,7 @@ func main() {
} else { } else {
filepath.WalkDir(c.StartingPath, func(path string, d fs.DirEntry, err error) error { filepath.WalkDir(c.StartingPath, func(path string, d fs.DirEntry, err error) error {
ext := strings.ToLower(filepath.Ext(path)) ext := strings.ToLower(filepath.Ext(path))
if !d.IsDir() && (c.IgnoreSuffix || ext == ".jpg" || ext == ".jpeg") { if !d.IsDir() && (c.IgnoreSuffix || ext == ".jpg" || ext == ".jpeg" || ext==".png" || ext==".gif" ) {
filelist = append(filelist, path) filelist = append(filelist, path)
} }
return nil return nil
@ -126,10 +132,16 @@ func doSingleImage(f string, c config, s stats) stats {
} }
defer in.Close() defer in.Close()
orig, err := jpeg.Decode(in) orig, err := jpeg.Decode(in)
if err != nil {
orig,err=png.Decode(in)
if err!=nil {
orig,err=gif.Decode(in)
if err != nil { if err != nil {
fmt.Printf("Couldn't decode source image %s: %s\n", f, err.Error()) fmt.Printf("Couldn't decode source image %s: %s\n", f, err.Error())
return s return s
} }
}
}
in.Close() in.Close()
// Check bounds and resize if necessary // Check bounds and resize if necessary
ores:=orig.Bounds() ores:=orig.Bounds()