squoosh-cli压缩图片

最近使用squoosh, 遇到一些问题,下面记录下过程。
官网地址

  1. 压缩卡住
    # squoosh-cli --resize {"enabled":true,"width":240.5,"height":320.5,"method":"lanczos3","fitMethod":"stretch","premultiply":true,"linearRGB":true} --mozjpeg {"quality":50,"baseline":false,"arithmetic":false,"progressive":true,"optimize_coding":true,"smoothing":0,"color_space":3,"quant_table":3,"trellis_multipass":false,"trellis_opt_zero":false,"trellis_opt_table":false,"trellis_loops":1,"auto_subsample":true,"chroma_subsample":2,"separate_chroma_quality":false,"chroma_quality":75} otqilzdy.jpg -d ../dest/
    0/1 ⠙ ▐╌╌╌╌╌╌╌╌╌╌▌ Decoding...
    0/1 ⠴ ▐╌╌╌╌╌╌╌╌╌╌▌ Decoding...

卡住不动了,是不是图片损坏了???, 所以检测下是否能打开图片,我这里发现图片能正常打开,所以不是此问题。

使用magick命令查看图片的属性, 官网地址

# magick identify otqilzdy.jpg
otqilzdy.jpg JPEG 481x641 481x641+0+0 8-bit CMYK 670898B 0.010u 0:00.076

这里也看不出啥问题, 所以找个能够正常压缩的图片,查看属性跟无法压缩的图片对比下

# squoosh-cli --resize {"enabled":true,"width":240.5,"height":320.5,"method":"lanczos3","fitMethod":"stretch","premultiply":true,"linearRGB":true} --mozjpeg {"quality":50,"baseline":false,"arithmetic":false,"progressive":true,"optimize_coding":true,"smoothing":0,"color_space":3,"quant_table":3,"trellis_multipass":false,"trellis_opt_zero":false,"trellis_opt_table":false,"trellis_loops":1,"auto_subsample":true,"chroma_subsample":2,"separate_chroma_quality":false,"chroma_quality":75} jmcnewjb.jpeg  -d ../dest/
1/1 ✔ Squoosh results:
 jmcnewjb.jpeg: 1.02MB
  └ ../dest/jmcnewjb.jpg → 8.71KB (0.834%)

// 此[jmcnewjb.jpeg]图片能够正常压缩, 查看图片属性
# magick identify jmcnewjb.jpeg
jmcnewjb.jpeg JPEG 5120x2880 5120x2880+0+0 8-bit sRGB 1.01994MiB 0.000u 0:00.005

对比下两个图片的属性

otqilzdy.jpg JPEG 481x641 481x641+0+0 8-bit CMYK 670898B 0.010u 0:00.076
jmcnewjb.jpeg JPEG 5120x2880 5120x2880+0+0 8-bit sRGB 1.01994MiB 0.000u 0:00.005

// 从属性中可以看出来,CMYK, sRGB 这两个是不同的, 当然文件大小,尺寸等不同跟问题无关。

所以,我们先暂时确定CMYKsRGB的问题,能否把CMYK转成sRGB再压缩呢???, 下面我们测试下

转换

// 转换,magick和convert 都可以使用
 # convert -colorspace sRGB otqilzdy.jpg  otqilzdy_1.jpg
 // 查看属性是否已经转成功
 # magick identify otqilzdy_1.jpg
otqilzdy_1.jpg JPEG 481x641 481x641+0+0 8-bit sRGB 33128B 0.000u 0:00.004

下面测试下把转换后的文件压缩

# squoosh-cli --resize {"enabled":true,"width":240.5,"height":320.5,"method":"lanczos3","fitMethod":"stretch","premultiply":true,"linearRGB":true} --mozjpeg {"quality":50,"baseline":false,"arithmetic":false,"progressive":true,"optimize_coding":true,"smoothing":0,"color_space":3,"quant_table":3,"trellis_multipass":false,"trellis_opt_zero":false,"trellis_opt_table":false,"trellis_loops":1,"auto_subsample":true,"chroma_subsample":2,"separate_chroma_quality":false,"chroma_quality":75} otqilzdy_1.jpg -d ../dest/
1/1 ✔ Squoosh results:
 otqilzdy_1.jpg: 4.68KB
  └ ../dest/otqilzdy_1.jpg → 4.59KB (98.0%)

果然压缩是没问题的。虽然压缩比比较低,应该是magick转换时压缩过了。

项目中使用

func main() {
    filePath := "./test.jpg"
    // 检测图片是否是CMYK
    colorSpace, err := checkColor(filePath)
    if err != nil {
        fmt.Errorf(err)
    }

    // 转sRGB
    if colorSpace == "CMYK" {
        err = Exec("/usr/local/bin/convert", "-colorspace", "sRGB", filePath, filePath)
        if err != nil {
            fmt.Errorf(err)
        }
    }

    // 压缩
    ...
}


    // 检测图片属性colorspace
func checkColor(source string) (string, error) {
  cmdStr := "/usr/local/bin/magick identify -format %[colorspace] " + source
  cmd := exec.Command("/usr/local/bin/magick", "identify", "-format", "%[colorspace]", source)
  // 此处是windows版本
  // c := exec.Command("cmd", "/C", cmd)
  output, err := cmd.CombinedOutput()
  return string(output), err
}
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!
未填写
文章
39
粉丝
9
喜欢
71
收藏
102
排名:461
访问:1.9 万
私信
所有博文
社区赞助商