- Notifications
You must be signed in to change notification settings - Fork107
Optimize images using multiple utilities
License
toy/image_optim
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Command line tool and ruby interface to optimize (lossless compress, optionally lossy) jpeg, png, gif and svg images using external utilities:
- advpng fromAdvanceCOMP(will usezopfli on default/maximum level 4)
- gifsicle
- jhead
- jpegoptim
- jpeg-recompress
- jpegtran fromIndependent JPEG Group's JPEG library
- optipng
- oxipng
- pngcrush
- pngout
- pngquant
- svgo
Based onImageOptim.app.
Documentation forlatest gem version andmaster branch.
A test application with latestimage_optim andimage_optim_pack is available on render:https://iopack.onrender.com/.
gem install image_optim
You may also want to installimage_optim_pack (seeBinaries pack).
gem install image_optim_pack
Add to yourGemfile:
gem'image_optim'
Withimage_optim_pack:
gem'image_optim'gem'image_optim_pack'
With version:
gem'image_optim','~> 0.31'
If you want to check latest changes:
gem'image_optim',:git=>'git://github.com/toy/image_optim.git'
This gem is also be available asdocker image containing most binaries:
docker run --rm ghcr.io/toy/image_optim --version# image_optim versiondocker run --rm ghcr.io/toy/image_optim --info# image_optim info including bin versionsdocker run --rm -v"$PWD":/here -w /here ghcr.io/toy/image_optim image-in-this-folder.jpg
Seeimage_optim_pack repository forDockerfile andinstructions.
Simplest way forimage_optim to locate binaries is to install them in common location present inPATH (seeBinaries installation).
If you cannot install to common location, then install to custom one and add it toPATH.
Specify custom bin location usingXXX_BIN environment variable (JPEGOPTIM_BIN,OPTIPNG_BIN,JPEG_RECOMPRESS_BIN, …).
Besides permanently setting environment variables in~/.profile,~/.bash_profile,~/.bashrc,~/.zshrc, … they can be set:
before command:
PATH="/custom/location:$PATH" image_optim *.jpgfor example:
PATH="/Applications/ImageOptim.app/Contents/MacOS:$PATH" image_optim *.jpginside script:
ENV['PATH'] = "/custom/location:#{ENV['PATH']}"; ImageOptim.optimize_images([…])for example:
ENV['PATH'] = "/Applications/ImageOptim.app/Contents/MacOS:#{ENV['PATH']}"; ImageOptim.optimize_images([…])
Easiest way to get latest versions of most binaries forimage_optim for Linux and Mac OS X is by installingimage_optim_pack gem.
Check installation instructions inGem installation section.
Pack doesn't includepngout andsvgo binaries, their installation instructions are provided below.
sudo apt-get install -y advancecomp gifsicle jhead jpegoptim libjpeg-progs optipng pngcrush pngquant
If you get an old version ofpngquant, please check how to install up-to-date version or compile from source athttp://pngquant.org/.
sudo yum install -y advancecomp gifsicle jhead libjpeg optipng pngquant
You may also need to installlibjpeg-turbo-utils instead oflibjpeg:
sudo yum install -y libjpeg-turbo-utils
You will also need to installjpegoptim andpngcrush from source:
ReplaceX.Y.Z with latest version number fromhttp://www.kokkonen.net/tjko/projects.html#jpegoptim.
JPEGOPTIM_VERSION=X.Y.Zcd /tmpcurl -O http://www.kokkonen.net/tjko/src/jpegoptim-$JPEGOPTIM_VERSION.tar.gztar zxf jpegoptim-$JPEGOPTIM_VERSION.tar.gzcd jpegoptim-$JPEGOPTIM_VERSION./configure&& make&& make install
ReplaceX.Y.Z with latest version number fromhttp://sourceforge.net/projects/pmt/files/pngcrush/.
PNGCRUSH_VERSION=X.Y.Zcd /tmpcurl -O http://iweb.dl.sourceforge.net/project/pmt/pngcrush/$PNGCRUSH_VERSION/pngcrush-$PNGCRUSH_VERSION.tar.gztar zxf pngcrush-$PNGCRUSH_VERSION.tar.gzcd pngcrush-$PNGCRUSH_VERSIONmake&& cp -f pngcrush /usr/local/bin
sudo port install advancecomp gifsicle jhead jpegoptim jpeg optipng pngcrush pngquant
brew install advancecomp gifsicle jhead jpegoptim jpeg optipng oxipng pngcrush pngquant jonof/kenutils/pngout
Unless it is available in your chosen package manager, can be installed using cargo:
cargo install oxipng
If you installed the dependencies via brew, pngout should be installed already. Otherwise, you can installpngout by downloading and installing thebinary versions.
Note: pngout is free to use even in commercial soft, but you can not redistribute, repackage or reuse it without consent and agreement of creator.license
svgo is available from NPM.
npm install -g svgo
If you prefer to installsvgo to your project directory, use one of the following commands instead:
npm install svgoyarn add svgo
When installingsvgo to the project directory, you must add the following to your environment:
SVGO_BIN='node_modules/svgo/bin/svgo'Download and install thejpeg-recompress binary from theJPEG-Archive Releases page,or follow the instructions tobuild from source.
image_optim*.{jpg,png,gif,svg}image_optim -r.image_optim -h
Initialize optimizer (or you can call optimization methods directly onImageOptim):
image_optim=ImageOptim.newimage_optim=ImageOptim.new(:pngout=>false)image_optim=ImageOptim.new(:nice=>20)
Optimize image getting temp path:
image_optim.optimize_image('a.png')
Optimize image in place:
image_optim.optimize_image!('b.jpg')
Optimize image data:
image_optim.optimize_image_data(data)
Multiple images:
image_optim.optimize_images(Dir['*.png'])do |unoptimized,optimized|ifoptimizedputs"#{unoptimized} =>#{optimized}"endendimage_optim.optimize_images!(Dir['*.*'])image_optim.optimize_images_data(datas)
Rails image assets optimization is extracted intoimage_optim_rails gem.
Configuration in YAML format will be read and prepended to options from two paths:
$XDG_CONFIG_HOME/image_optim.yml(by default~/.config/image_optim.yml).image_optim.ymlin current working directory
Paths can be changed using:config_paths option and--config-paths argument.
Example configuration:
nice:20pngout:false# disableoptipng:level:5
image_optim uses standard ruby library for creating temporary files. Temporary directory can be changed using one ofTMPDIR,TMP orTEMP environment variables.
Run with--benchmark to compare the performance of each individual tool on your images:
image_optim --benchmark=isolated -r /tmp/corpus/
benchmarking: 100.0% (elapsed: 3.9m)BENCHMARK RESULTSname files elapsed kb saved kb/s-------- ----- ------- -------- -------oxipng 50 8.906 1867.253 209.664pngquant 50 1.980 214.597 108.386pngcrush 50 22.529 1753.704 77.841optipng 50 142.940 1641.101 11.481advpng 50 137.753 962.549 6.987pngout 50 426.706 444.679 1.042:nice— Nice level, priority of all used tools with higher value meaning lower priority, in range-20..19, negative values can be set only if run by root user(defaults to10):threads— Number of threads or disable(defaults to number of processors):verbose— Verbose output(defaults tofalse):pack— Require image_optim_pack or disable it, by default image_optim_pack will be used if available, will turn on:skip-missing-workersunless explicitly disabled(defaults tonil):skip_missing_workers— Skip workers with missing or problematic binaries(defaults tofalse):allow_lossy— Allow lossy workers and optimizations(defaults tofalse):cache_dir— Configure cache directory:cache_worker_digests- Also cache worker digests along with original file digest and worker options: updating workers invalidates cache:timeout— Maximum time in seconds to spend on one image, note multithreading and cache(defaults to unlimited)
Worker can be disabled by passingfalse instead of options hash or by setting option:disable totrue.
:level— Compression level:0- don't compress,1- fast,2- normal,3- extra,4- extreme(defaults to4)
:interlace— Interlace:true- interlace on,false- interlace off,nil- as is in original image (defaults to running two instances, one with interlace off and one with on):level— Compression level:1- light and fast,2- normal,3- heavy (slower)(defaults to3):careful— Avoid bugs with some software(defaults tofalse)
Worker has no options
:allow_lossy— Allow limiting maximum quality(defaults tofalse):strip— List of markers to strip::com,:exif,:iptc,:icc,:xmp,:noneor:all(defaults to:all):max_quality— Maximum image quality factor0..100, ignored in default/lossless mode(defaults to100)
:allow_lossy— Allow worker, it is always lossy(defaults tofalse):quality— JPEG quality preset:0- low,1- medium,2- high,3- veryhigh(defaults to3):method— Comparison Metric:mpe- Mean pixel error,ssim- Structural similarity,ms-ssim- Multi-scale structural similarity (slow!),smallfry- Linear-weighted BBCQ-like (may be patented)(defaults to ssim)
:copy_chunks— Copy all chunks(defaults tofalse):progressive— Create progressive JPEG file(defaults totrue):jpegrescan— Use jpegtran through jpegrescan, ignore progressive option(defaults totrue)
:level— Optimization level preset:0is least,7is best(defaults to6):interlace— Interlace:true- interlace on,false- interlace off,nil- as is in original image(defaults tofalse):strip— Remove all auxiliary chunks(defaults totrue)
:level— Optimization level preset:0is least,6is best(defaults to3):interlace— Interlace:true- interlace on,false- interlace off,nil- as is in original image(defaults tofalse):strip— Remove all auxiliary chunks(defaults totrue)
:chunks— List of chunks to remove or:alla- all except tRNS/transparency or:allb- all except tRNS and gAMA/gamma(defaults to:alla):fix— Fix otherwise fatal conditions such as bad CRCs(defaults tofalse):brute— Brute force try all methods, very time-consuming and generally not worthwhile(defaults tofalse):blacken— Blacken fully transparent pixels(defaults totrue)
:copy_chunks— Copy optional chunks(defaults tofalse):strategy— Strategy:0- xtreme,1- intense,2- longest Match,3- huffman Only,4- uncompressed(defaults to0)
:allow_lossy— Allow quality option(defaults tofalse):max_colors— Maximum number of colors to use(defaults to256):quality— min..max - don't save below min, use less colors below max (both in range0..100; in yaml -!ruby/range 0..100), ignored in default/lossless mode(defaults to100..100,0..100in lossy mode):speed— speed/quality trade-off:1- slow,3- default,11- fast & rough(defaults to3)
:disable_plugins— List of plugins to disable(defaults to[]):enable_plugins— List of plugins to enable(defaults to[]):allow_lossy— Allow precision option(defaults tofalse):precision— Number of digits in the fractional part0..20, ignored in default/lossless mode(defaults to3)
List of contributors toimage_optim.
If you would like to contribute - that is great and you are very welcome. Please check few notes in fileCONTRIBUTING.markdown.
In separate fileCHANGELOG.markdown.
Copyright (c) 2012-2024 Ivan Kuchin. SeeLICENSE.txt for details.
About
Optimize images using multiple utilities
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.