就经验性来说,人强烈追求什么的时候,那东西基本上是不来的,而当你极力回避它的时候,它却自然找上头来。——《海边的卡夫卡》
分享一个在mac
上递归压缩某目录png
和jpg
图片的脚本
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
这两个pngquant
和jpegoptim
然后执行脚本:
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 {} +