前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >mac进行图片压缩

mac进行图片压缩

作者头像
阿超
发布2024-08-23 19:56:56
740
发布2024-08-23 19:56:56
举报
文章被收录于专栏:快乐阿超

就经验性来说,人强烈追求什么的时候,那东西基本上是不来的,而当你极力回避它的时候,它却自然找上头来。——《海边的卡夫卡》

分享一个在mac上递归压缩某目录pngjpg图片的脚本

代码语言:javascript
复制
Github-Id-VampireAchao:Downloads achao$ brew install pngquant
HOMEBREW_BREW_GIT_REMOTE set: using https://mirrors.ustc.edu.cn/brew.git as the Homebrew/brew Git remote.
remote: Enumerating objects: 174, done.
remote: Counting objects: 100% (26/26), done.
remote: Total 174 (delta 26), reused 26 (delta 26), pack-reused 148
Receiving objects: 100% (174/174), 96.85 KiB | 1.56 MiB/s, done.
Resolving deltas: 100% (85/85), completed with 6 local objects.
From https://mirrors.ustc.edu.cn/brew
   21e3055eb5..f17ee4303c  master     -> origin/master
 * [new tag]               4.3.16     -> 4.3.16
==> Auto-updating Homebrew...
Adjust how often this is run with HOMEBREW_AUTO_UPDATE_SECS or disable with
HOMEBREW_NO_AUTO_UPDATE. Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
==> Auto-updated Homebrew!
Updated 2 taps (homebrew/core and homebrew/cask).
==> New Formulae
asm6809      boost@1.85   go@1.22      gql          tmpmail      ufbt
==> New Casks
geekbench-ai                             nrf-connect

You have 28 outdated formulae and 2 outdated casks installed.
Github-Id-VampireAchao:blog-img achao$ brew install jpegoptim
==> Fetching jpegoptim
==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/bottles/jpegoptim-1
######################################################################### 100.0%
==> Pouring jpegoptim-1.5.5.arm64_sonoma.bottle.tar.gz
🍺  /opt/homebrew/Cellar/jpegoptim/1.5.5: 8 files, 146.7KB
==> Running `brew cleanup jpegoptim`...
Disable this behaviour by setting HOMEBREW_NO_INSTALL_CLEANUP.
Hide these hints with HOMEBREW_NO_ENV_HINTS (see `man brew`).
Github-Id-VampireAchao:blog-img achao$ find /Users/achao/Documents/blog/themes/gal/source/imgs/oss -type f \( -name "*.png" -o -name "*.jpg" \) -size +500k -exec sh -c '
>   for img; do
>     while [ $(stat -f%z "$img") -gt 512000 ]; do
>       magick "$img" -resize 90% -quality 70 "$img"
>       if [[ "$img" == *.png ]]; then
>         pngquant --force --quality=50-70 "$img" --output "${img%.png}-compressed.png"
>         mv "${img%.png}-compressed.png" "$img"
>       else
>         jpegoptim --max=70 --strip-all "$img"
>       fi
>     done
>   done
> ' sh {} +
/Users/achao/Documents/blog/themes/gal/source/imgs/oss/2020-06-01/background4.jpg 1728x972 24bit N JFIF [OK] 339636 --> 280503 bytes (17.41%), optimized.
/Users/achao/Documents/blog/themes/gal/source/imgs/oss/2020-06-01/background5.jpg 1728x1080 24bit N JFIF [OK] 311400 --> 240412 bytes (22.80%), optimized.
/Users/achao/Documents/blog/themes/gal/source/imgs/oss/2020-06-01/background7.jpg 1728x972 24bit N JFIF [OK] 167138 --> 136059 bytes (18.59%), optimized.
/Users/achao/Documents/blog/themes/gal/source/imgs/oss/2020-06-01/background6.jpg 1728x972 24bit N JFIF [OK] 180822 --> 143980 bytes (20.37%), optimized.
/Users/achao/Documents/blog/themes/gal/source/imgs/oss/2020-06-01/background2.jpg 1728x1080 24bit N JFIF [OK] 249454 --> 200443 bytes (19.65%), optimized.
/Users/achao/Documents/blog/themes/gal/source/imgs/oss/2020-06-01/head.jpg 1800x1800 24bit N JFIF [OK] 194731 --> 161802 bytes (16.91%), optimized.
/Users/achao/Documents/blog/themes/gal/source/imgs/oss/2020-06-01/cover1.jpg 1512x945 24bit N JFIF [OK] 165473 --> 131972 bytes (20.25%), optimized.
/Users/achao/Documents/blog/themes/gal/source/imgs/oss/2021-06-21/6b2a6c42-dcd4-427b-8a5b-2fde0990cbf6_head.jpg 1800x1800 24bit N JFIF,IPTC,Exif,XMP [OK] 229552 --> 161802 bytes (29.51%), optimized.
/Users/achao/Documents/blog/themes/gal/source/imgs/oss/picGo/QQ图片20210715230053.jpg 972x2106 24bit N JFIF,ICC [OK] 237743 --> 237077 bytes (0.28%), optimized.
/Users/achao/Documents/blog/themes/gal/source/imgs/oss/2021-06-19/d61c65ec-b3c9-4f3f-8f49-36d7c5fa12b5_head.jpg 1800x1800 24bit N JFIF,IPTC,Exif,XMP [OK] 229552 --> 161802 bytes (29.51%), optimized.
/Users/achao/Documents/blog/themes/gal/source/imgs/oss/2021-06-19/2d4bc9e4-01da-4690-8a32-063105b958a6_head.jpg 1800x1800 24bit N JFIF,IPTC,Exif,XMP [OK] 229552 --> 161802 bytes (29.51%), optimized.
/Users/achao/Documents/blog/themes/gal/source/imgs/oss/2021-06-20/b5b330e0-da3f-4126-8a5f-87279a3945c5_head.jpg 1800x1800 24bit N JFIF,IPTC,Exif,XMP [OK] 229552 --> 161802 bytes (29.51%), optimized.
/Users/achao/Documents/blog/themes/gal/source/imgs/oss/2021-06-20/1f8c2c9d-d4b9-492e-8f1a-a3d56e599825_head.jpg 1800x1800 24bit N JFIF,IPTC,Exif,XMP [OK] 229552 --> 161802 bytes (29.51%), optimized.
/Users/achao/Documents/blog/themes/gal/source/imgs/oss/2021-06-20/a5f4d7fd-24b5-4348-8af2-7a4364b69d62_head.jpg 1800x1800 24bit N JFIF,IPTC,Exif,XMP [OK] 229552 --> 161802 bytes (29.51%), optimized.
/Users/achao/Documents/blog/themes/gal/source/imgs/oss/2021-06-20/548c2884-c76d-452a-80bb-54bfcb309111_head.jpg 1800x1800 24bit N JFIF,IPTC,Exif,XMP [OK] 229552 --> 161802 bytes (29.51%), optimized.
/Users/achao/Documents/blog/themes/gal/source/imgs/oss/2021-06-20/c3b845ac-3650-428b-8fec-907f4c776148_head.jpg 1800x1800 24bit N JFIF,IPTC,Exif,XMP [OK] 229552 --> 161802 bytes (29.51%), optimized.
Github-Id-VampireAchao:blog-img achao$ 

其实主要是通过brew install这两个pngquantjpegoptim

然后执行脚本:

代码语言:javascript
复制
find /Users/achao/Documents/blog/themes/gal/source/imgs/oss -type f \( -name "*.png" -o -name "*.jpg" \) -size +500k -exec sh -c '
  for img; do
    while [ $(stat -f%z "$img") -gt 512000 ]; do
      magick "$img" -resize 90% -quality 70 "$img"
      if [[ "$img" == *.png ]]; then
        pngquant --force --quality=50-70 "$img" --output "${img%.png}-compressed.png"
        mv "${img%.png}-compressed.png" "$img"
      else
        jpegoptim --max=70 --strip-all "$img"
      fi
    done
  done
' sh {} +
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-08-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档