2019年4月2日

Macでmozjpegを使う

Share

mozjpeg

https://github.com/mozilla/mozjpeg

説明不要かと思いますが、mozillaプロジェクトのjpegエンコードです

ちなみに、オンラインツールでも提供されてます。

https://mozjpeg.codelove.de/

インストール

macへのインストールはbrewコマンドでできます。

$ brew install mozjpeg

使い方

インストールしてもmozjpegというコマンドが使えるようになるわけではなく、

画像圧縮にはcjpegというコマンドを使います。

pathが通ってないですが、下記にインストールされてます。

$ /usr/local/Cellar/mozjpeg/3.3.1_1/bin/cjpeg

画像を圧縮する

# 圧縮前サイズ

$ ls -l sample.jpg | cut -d' ' -f7
52611

# 圧縮品質80を指定


$ /usr/local/Cellar/mozjpeg/3.3.1_1/bin/cjpeg -optimize -quality 80 sample.jpg > sample80.jpg

# 圧縮後サイズ
$ ls -l sample80.jpg | cut -d' ' -f8
32311

一括圧縮編(xargs)

事前にresizedフォルダを作成しておく

$ find . -name '*.jpg' -type f | sed 's!^.*/!!' | xargs -I% /usr/local/Cellar/mozjpeg/3.3.1_1/bin/cjpeg -optimize -quality 80 -outfile "resized/%" "%"

ヘルプ

$ /usr/local/Cellar/mozjpeg/3.3.1_1/bin/cjpeg -help
/usr/local/Cellar/mozjpeg/3.3.1_1/bin/cjpeg: unknown option 'help'
usage: /usr/local/Cellar/mozjpeg/3.3.1_1/bin/cjpeg [switches] [inputfile]
Switches (names may be abbreviated):
  -quality N[,...]   Compression quality (0..100; 5-95 is most useful range,
                     default is 75)
  -grayscale     Create monochrome JPEG file
  -rgb           Create RGB JPEG file
  -optimize      Optimize Huffman table (smaller file, but slow compression, enabled by default)
  -progressive   Create progressive JPEG file (enabled by default)
  -baseline      Create baseline JPEG file (disable progressive coding)
  -targa         Input file is Targa format (usually not needed)
  -revert        Revert to standard defaults (instead of mozjpeg defaults)
  -fastcrush     Disable progressive scan optimization
  -dc-scan-opt   DC scan optimization mode
                 - 0 One scan for all components
                 - 1 One scan per component (default)
                 - 2 Optimize between one scan for all components and one scan for 1st component
                     plus one scan for remaining components
  -notrellis     Disable trellis optimization
  -trellis-dc    Enable trellis optimization of DC coefficients (default)
  -notrellis-dc  Disable trellis optimization of DC coefficients
  -tune-psnr     Tune trellis optimization for PSNR
  -tune-hvs-psnr Tune trellis optimization for PSNR-HVS (default)
  -tune-ssim     Tune trellis optimization for SSIM
  -tune-ms-ssim  Tune trellis optimization for MS-SSIM
Switches for advanced users:
  -noovershoot   Disable black-on-white deringing via overshoot
  -arithmetic    Use arithmetic coding
  -dct int       Use integer DCT method (default)
  -dct fast      Use fast integer DCT (less accurate)
  -dct float     Use floating-point DCT method
  -quant-baseline Use 8-bit quantization table entries for baseline JPEG compatibility
  -quant-table N Use predefined quantization table N:
                 - 0 JPEG Annex K
                 - 1 Flat
                 - 2 Custom, tuned for MS-SSIM
                 - 3 ImageMagick table by N. Robidoux
                 - 4 Custom, tuned for PSNR-HVS
                 - 5 Table from paper by Klein, Silverstein and Carney
  -restart N     Set restart interval in rows, or in blocks with B
  -smooth N      Smooth dithered input (N=1..100 is strength)
  -maxmemory N   Maximum memory to use (in kbytes)
  -outfile name  Specify name for output file
  -memdst        Compress to memory instead of file (useful for benchmarking)
  -verbose  or  -debug   Emit debug output
  -version       Print version information and exit
Switches for wizards:
  -qtables FILE  Use quantization tables given in FILE
  -qslots N[,...]    Set component quantization tables
  -sample HxV[,...]  Set component sampling factors
  -scans FILE    Create multi-scan JPEG per script FILE