Giter VIP home page Giter VIP logo

imageoptim-cli's Introduction

ImageOptim-CLI

Automates ImageOptim, ImageAlpha, and JPEGmini for Mac to make batch optimisation of images part of your automated build process.

NPM version NPM downloads Build Status Maintainability

Table of Contents

📣 Summary

While other image optimization tools are available from the command line, ImageOptim-CLI exists because the current benchmarks suggest that ImageOptim, ImageAlpha and JPEGmini currently outperform those alternatives over lossless and lossy optimizations.

ImageOptim-CLI is written in TypeScript and AppleScript but is distributed as a self-contained executable binary, you don't need Node.js installed to use ImageOptim-CLI.

Check out this short video demo of ImageOptim-CLI to see how it works.

🌩 Installation

npm install -g imageoptim-cli
brew update
brew install imageoptim-cli

Manual

Otherwise, you can install manually by downloading the latest release then adding ImageOptim-CLI to your \\$PATH.

# go to home directory
cd ~
# download the tarball (change 3.0.7 to latest version if available)
curl --output imageoptim-cli.tgz https://registry.npmjs.org/imageoptim-cli/-/imageoptim-cli-3.0.7.tgz
# extract the tarball
tar -xvzf ./imageoptim-cli.tgz
# delete the tarball
rm imageoptim-cli.tgz
# rename the directory extracted from the tarball
mv ./package ./imageoptim-cli
# make imageoptim command available in your terminal
export PATH=$PATH:imageoptim-cli/dist

Saving somewhere in your home directory such as ~/imageoptim-cli is recommended, but not essential. Saving to /Applications is not recommended, do not do this.

🕹 Usage

$ imageoptim --help

  Usage: imageoptim [options] [patterns...]

  Options:

    -V, --version           output the version number
    -a, --imagealpha        enable ImageAlpha
    -j, --jpegmini          enable JPEGmini
    -C, --no-color          output to the terminal without colors
    -I, --no-imageoptim     disable ImageOptim
    -Q, --no-quit           do not quit apps once finished
    -S, --no-stats          do not display file size savings and quality loss information
    --number-of-colors <n>  ImageAlpha palette size, defaults to 256
    --quality <min>-<max>   ImageAlpha quality range from 0-100, defaults to 65-80
    --speed <n>             ImageAlpha speed from 1 (brute-force) to 10 (fastest), defaults to 1
    -h, --help              output usage information

  Supported Apps:

    ImageAlpha: https://pngmini.com
    ImageOptim: https://imageoptim.com
    JPEGmini Lite: https://itunes.apple.com/us/app/jpegmini-lite/id525742250
    JPEGmini Pro: https://itunes.apple.com/us/app/jpegmini-pro/id887163276
    JPEGmini: https://itunes.apple.com/us/app/jpegmini/id498944723

  Examples:

    Run ImageOptim.app over every image in current directory
    imageoptim

    Run ImageAlpha.app and ImageOptim.app over every PNG in current directory
    imageoptim --imagealpha '**/*.png'

    Run JPEGmini.app and ImageOptim.app over every JPG in current directory
    imageoptim --jpegmini '**/*.jpg' '**/*.jpeg'

    Run ImageOptim.app over every image in a specific directory
    imageoptim '~/Desktop'

⚠️ JPEGmini and support for assistive devices

You may be presented with the following message the first time you run ImageOptim-CLI with the --jpegmini flag.

To automate JPEGmini we need to add Terminal.app (or iTerm.app etc) to the 'support for assistive devices' whitelist.

The JPEGmini OS X Apps don't include a command line API, so a real user is simulated by entering synthetic clicks and keyboard commands instead. This requires your permission and is easily set up in System Preferences as shown by these guides.

💡 Related Projects

Grunt Plugin

The ImageOptim-CLI Grunt plugin is grunt-imageoptim.

Comparison of image optimization tools

ImageOptim-CLI features in this comparison of the performance of image optimisation tools alongside Kraken.io, CodeKit, grunt-contrib-imagemin, Smush.it, and TinyPNG.

Article for Smashing Magazine

How Optimized Are Your Images? Meet ImageOptim-CLI, a Batch Compression Tool

Alfred Workflow

The ImageOptim-CLI Workflow for Alfred app is alfred-image-optim-workflow

❓ FAQs

General

Do ImageOptim, ImageAlpha, or JPEGmini come bundled with the ImageOptim-CLI installation?

You will need to install these applications separately.

Do I have to pay to use ImageOptim-CLI?

The CLI, ImageOptim and ImageAlpha are all free. JPEGmini is a paid-for product but you can use ImageOptim-CLI and choose not to run JPEGmini.

The WebP image format looks promising, can you get ImageOptim-CLI to convert images to it?

WebP looks great and may well overtake the formats handled by ImageOptim-CLI, but converting images to WebP is outside ImageOptim-CLI's chosen remit.

Can you get ImageOptim-CLI to skip images it has already processed, if they haven't changed?

JPEGmini does this today, but for ImageOptim and ImageAlpha I feel a feature like this belongs in those applications rather than this automator.

ImageOptim

ImageOptim makes the fans on my Mac run at full power.

Optimising images is a pretty intensive process, so instead of optimising one image at a time (which would take forever) — ImageOptim optimises many images at the same time until all of them are done.

A side effect of this is that the fans come on at full power to keep your machine cool while it's maxed out.

ImageAlpha

I don't think ImageAlpha is running, I can't see anything.

ImageOptim-CLI uses ImageAlpha's internal installation of pngquant so it's normal that nothing is shown on screen.

It's also possible that if you look in the Activity Monitor you will not see pngquant displayed but it is being run. In my experience it's only when you run ImageOptim-CLI on a very large number of PNGs that you have enough time to spot it. ensure that Activity Monitor's Update Frequency is set to Very Often (1 sec).

JPEGmini

Can I use ImageOptim-CLI with JPEGmini Lite, the free version of JPEGmini?

Yes.

I upgraded from JPEGmini Lite to JPEGmini but ImageOptim-CLI still says JPEGmini is not installed.

Performing the in-app upgrade leaves the app named as jpegmini-lite, so ImageOptim-CLI can't determine whether it's the free or full version. It is better to instead buy the full version of JPEGmini separately.

ImageOptim-CLI says “To automate JPEGmini we need to enable GUI Scripting”, how do I do that?

See this tutorial on how to manage Accessibility preferences and GUI Scripting. In the case of OS X Mavericks, you will want to add the Applications JPEGmini and Terminal (or equivalent such as iTerm).

Windows and Linux

Can I use ImageOptim-CLI on Windows or Linux?

ImageOptim-CLI is responsible for automating 3 OS X applications so is inherently bound to OS X for that reason.

Are there any plans for ImageOptim-CLI to support Windows or Linux?

It would first require ImageOptim, ImageAlpha, and JPEGmini to be available for those platforms.

I don't have OS X, can you recommend an alternative to ImageOptim-CLI?

@addyosmani wrote a really thorough article on tools for image optimization which discusses a wide range of options in great detail.

⚙️ Contributing

Have an idea? Found a bug? Please see the Contributing Guide for information on how to install the project and start writing code.

🙋🏿‍♀️ Getting Help

Get help with issues by creating a Bug Report or discuss ideas by opening a Feature Request.

👀 Other Projects

If you find my Open Source projects useful, please share them ❤️

🤓 Author

I'm Jamie Mason from Leeds in England, I began Web Design and Development in 1999 and have been Contracting and offering Consultancy as Fold Left Ltd since 2012. Who I've worked with includes Sky Sports, Sky Bet, Sky Poker, The Premier League, William Hill, Shell, Betfair, and Football Clubs including Leeds United, Spurs, West Ham, Arsenal, and more.

Follow JamieMason on GitHub      Follow fold_left on Twitter

imageoptim-cli's People

Contributors

angristan avatar galenhuntington avatar gitter-badger avatar jamesstout avatar jamiemason avatar kittysquee avatar msch avatar neikei avatar nwtn avatar oo12 avatar paleite avatar ramiroaraujo avatar regularlabs avatar stefancrain avatar tomchentw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

imageoptim-cli's Issues

Pre commit issue

Using this as a precommit hook git diff --cached --name-only --diff-filter=ACM | imageOptim -a It looks like the commit happens before the program exits

pngquant is super slow

Hi,

I'm wondering what imageAlpha steps does because its so slow. I've ran it on 19000 files for 8 hours and wasn't finished and with only a quarter processed. My mac is a 4,15 Ghz one (mackintosh)

It seems to me that if theres a lot of file, a lot of time is spent on cataloguing perhaps

Run pngquant on JPEGs

Mac build of pngquant uses Cocoa to read images, and therefore supports any bitmap image format that Cocoa does.

If you run:

pngquant jpeg_streepjes.jpg

you'll get jpeg_streepjes.jpg-fs8.png that is 369 bytes large, down from 179661 :)

JpegMini wants to quit, possible solutions

Hi,

Sometimes JpegMini complains it can'r be quit because it's processing files. That's because ImageOptim-CLI thinks Jpeg mini finished and wants tio quit it. Worse, it then launch the ImageOptim process on files not finished by JpegMini.

So it's critical to make sure ImageOptim correctly determine if jpegs mini is done.

Currently it seems ImageOptim_Cli monitors the CPU load of jpeg mini. But facts shows that this deosn't always works.

So I took a look.

  1. Improving the current CPU monitoring loop.

It samples the CPU load of Jpeg mini every 2 seconds, and if it's near zero it thinks it's done.
The problem I see with that approach is that the CPU percentage is only sampled once evey 12 seconds. If by bad luck, at the excat moment it samples it, Jpegmini use near zero cpu (maybe because the os starved it's ressources), then the loop is exited, even though in the very next moment the cpu use went up. So very short iddle time can lead to false done message.

To fix this I propose to put the 2 second loop inside a 5 repeats loop, and only exit the main loop if the sum of the 5 repeats cpu percentage is 0.

So we'll have 5 time less chances of bad reports.

But it's still prone to erors, though much less.

  1. Examining file usage.

Using opensnoop and iosnoop, you can actually measure file usage of a process.
So I think it's possible to loop till JpegMini file usage returns nothing.

The only problem with that is that it requires the zdmin password, which may be passed in parameter of optimimage_cli

ImageOptim is forced to be on foreground repeatedly

Hi,

I noticed thet when the ImageOptim is ran, it's forced by ImageOptim_CLI, to be on the foregound. Is that normal ?

I think ImageOptim_CLI sends each files top ImageOptim, rather than just the folder. Sometime it fails

LSOpenURLsWithRole() failed for the application /Applications/ImageOptim.app with error -1712 for the file /Volumes/clone FMS/imgs/Jpegs/Group_02/ZZZZZ0007/ZZZZZX0007_list_CZ_5.png.

If having ImageOptim on the foregrpound could be avoided that'd be nice because it can mess with other process on the machine

Automate setting of options in ImageAlpha

As mentioned by @joeldbirch in grunt-imageoptim issue 2;

It seems like it simply sets 256 colours, which is safe, but I'd love to be able to set the default options to 32 colours.

ImageAlpha currently doesn't persist overridden default options. Each time you run a file the default values you changed previously will be back in their original state.

Ideally if this were to be implemented, the best place for it would be the core ImageAlpha app's Preferences pane imo (/cc @pornel). It could be possible to do in this project via GUIScripting but that would be quite brittle and again the core app could benefit from this.

Maintain cache of processed files

Consider keep a record of processed images and their last modified dates in order to not reprocess unchanged images on subsequent runs.

JPEGmini: can’t make "4,2" into type real

Hi,

I'm using latest everything 190.8.5, Jpeg mini 1.8.1, ImageOptim 1.6.9

often jpeg mini will want to quit

but there's worse, the cli will output an error like can't transform 4,2 in real when jpeg mini runs. The worst part is that ImageOptim-CLI after encountering this error will immediately launch imageoptim, even though jepgmini is still processing files.

That's too bad because imageoptim is to be done after jpeg mini.

Moreover, I thing after a Jpegmini error "4,2", the CLI should stop and return an error so we are warned about it.

Processing 56 images...
ImageAlpha...
JPEGmini...
/Applications/ImageOptim-CLI-1.6.19/bin/imageOptimAppleScriptLib:3422:3459: execution error: System Events got an error: Can’t make "4,2" into type real. (-1700)
ImageOptim
✔ Finished in 21 seconds

As the error isn't stopping the cli, there's no way to be sure all was processed ok.

P.S : I'm using a french OS. In french language decimal number are separated with a comma rather than a dot. Maybe that's a localization issue. I tried to change language and number setting, but it didn't do anything better (I can't logout/restart for now, so I'm not sure it doesn't fixes it)

Automate JPEGmini

If -j or --jpeg-mini are provided, also optimise JPGs using JPEGmini

JPEGmini wants to quit

Dear developer,

first, thank you for this great tool! This tool was the only reason why I bought JPEGMini.

I found a bug:

Terminal:
/Users/madrian/image-optim/imageoptim-cli/bin/imageOptim --jpeg-mini --quit --directory /Users/madrian/Desktop/wat/

Immediately after imageOptim starts the JPEGMini I got this popup and error in the terminal:

http://d.pr/i/qWmQ+

Failed date conversion

On my machine

$ date
Wed May 22 13:41:09 BST 2013

On a colleague's machine

$ date
Wed 22 May 2013 13:41:40 BST

When running imageOptim-CLI on the latter machine, the following error is encountered as this format does not match our now method.

Failed conversion of ``Wed 22 May 2013 13:39:57 BST'' using format ``%a %b %d %T %Z %Y''
date: illegal time format
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... 
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

/cc @jamesstout

Prevent too-lossy PNG8 conversion

I've noticed you're calling pngquant without --quality option. For some images 256 colors may not be enough.

Consider adding --quality=75-100 to pngquant options. This will make it skip conversion if it's not possible to achieve at least "75%" (JPEG equivalent) quality.

Don't mark smallest lossy file as "winner"

Kraken.io's JPEG optimization isn't anything special, it achieves small files sizes by reducing quality heavily.

From the comparison:

JpegMini:
35028 bytes
80.84% saving
10% loss

Kraken.io:
35001 bytes
80.86% saving
68.59% loss

Kraken.io basically ruined the file to save 27 bytes.

You can mark winner only if both size and quality is better (that's clear Pareto improvement).

If quality and file sizes vary it's difficult and subjective. At very least you should use loss/saving ratio to pick the winner, and ideally either quality or file size should be close.

ImageOptim-CLI Opens System Prefs Pane When Run

When I run the following in the command line, ImageOptim is launched as expected, but the System Preferences pane in OS X is also launched. The install was done via npm with sudo and I used the -g flag.

Support processing small batches of files

A very good point made on Twitter about running ImageOptim-CLI in a pre-commit hook instead of as part of a deployment build against an entire folder, which is comparatively slow and inefficient.

To support this and #19 "Please consider making a plugin for Guard", we need to support running the CLI against short lists of files.

Publish RubyGem

ImageOptim-CLI has no dependency on Node but is currently only published to npm. Look into also releasing a RubyGem.

preferProgressive: true

Run some tests to see whether existing tools convert images to progressive or not, and whether this can be controlled.

Progressive images are likely to be slower/larger in actual terms but perceptually faster/smaller.

Aborts when directories are found

If there are subdirectories that have names ending in .jpg or .png then ImageOptim CLI tries to process them and then errors out.

http://pastebin.com/MBmXD4Fw

A better solution would be to add "-type f" to the find command wherever you are using it, so that directories don't get into the queue in the first place.

Feature request: Skip already processed images

I realize this is in direct conflict with this issue, but I'd love the capability to skip images that have already been processed. I use this as part of my build process, and it would save a ton of time to skip the files that have been processed before ImageOptim tries to re-optimize them. I would even be happy with this being an option that you would have to pass a flag (such as --skip-processed or -s) to activate.

Feature request: Running ImageOptim repeatedly until you encounter < 5% savings

Generally when I run ImageOptim, the Again button makes it easy to make sure that maximum savings have been achieved. For some images, it can take two or three hits of the Again button to achieve the optimum image.

Can the shell script run ImageOptim repeatedly? This may be slightly tricky because we don't want to run it repeatedly on all files but just the files that had big savings (say >5%) in the previous run. So we may have to keep track of which files had big savings, quit ImageOptim, and then reopen it and only add those files to the queue.

CLI can exit too early

It appears that the number of threads in use by ImageOptim.app can drop below our threshold periodically while processing an extremely large number of files. This results in the CLI exiting too early.

Possible cause may be either large queues of images, or multiple ImageOptim processes.

/cc @jamesstout

Combined/Multiple command line options

As mentioned by @joeldbirch in grunt-imageoptim issue 2, passing options in (the commonly expected format) of imageOptim -da 'assets/img' is not currently supported.

This will be addressed but in the meantime here is how to combine options.

Combining options

Run ImageOptim

  • imageOptim -d path/to/images
  • imageOptim --directory path/to/images

Run ImageOptim then quit it when finished

  • imageOptim -q -d path/to/images
  • imageOptim --quit --directory path/to/images

Run ImageAlpha then ImageOptim

  • imageOptim -a -d path/to/images
  • imageOptim --image-alpha --directory path/to/images

Run ImageAlpha then ImageOptim then quit ImageOptim when finished

  • imageOptim -q -a -d path/to/images
  • imageOptim --quit --image-alpha --directory path/to/images

Not yet supported

Until this issue is closed, the following formats are not supported;

Run ImageOptim then quit it when finished

  • imageOptim -qd path/to/images

Run ImageAlpha then ImageOptim

  • imageOptim -ad path/to/images

Run ImageAlpha then ImageOptim then Quit ImageOptim when finished

  • imageOptim -qad path/to/images

Publish Homebrew package

ImageOptim-CLI has no dependency on Node but is currently only published to npm. Look into also releasing a Homebrew package.

[gh-pages] highlight which optimisations were lossy

JPEGMini and TinyPNG are lossy optimizers, so comparing them with lossless optimizers isn't quite apples to apples.

e.g. if you included convert -quality 10 in the test it would probably win in all cases.

At very least I'd suggest highlighting this issue in the table, maybe with additional info about how much quality* was lost, so the reader can judge whether filesize gain was worth the loss.

*) "how much" is hard to quantify. Quality difference is usually measured in PSNR/dB/MSE or (D)SSIM, but those numbers are hard to interpret (e.g. people would think that SSIM 0.995 and 0.98 are almost the same, while in fact that's 4x difference).

To show something like "reduced file size by 20%, but quality by 30%" needs establishing what "100% quality lost" means:

  1. Create reference image for what is the "worst possible quality", e.g. compress image as JPEG in 10% quality (quality setting in compressors is also completely arbitrary, so whether that's 1%/5%/20% setting it doesn't matter, it just has to be quality at which you'd call image ruined and unusable).

    convert -quality 10 source.jpg worst.jpg
    

    or 16-color-or-less PNG:

    pngquant 16 --quality=0-10 - < source.png > worst.png
    
  2. Measure difference between "worst" image and the original: (compare is from ImageMagick)

    compare -metric MSE source.jpg worst.jpg /dev/null
    

    or better with dssim:

    dssim source.png worst.png
    
  3. Measure difference between optimized file and the original

    compare -metric MSE source.jpg optimized.jpg /dev/null
    

    or

    dssim source.png optimized.png
    
  4. Calculate: $optimized_difference / $worst_difference * 100 to get how much "% quality" was lost.

Add threading

Thanks for the project, Jamie. Unfortunately, it is much slower than the original ImageOptim app as it only uses a single core.

Simplify countProcesses()

countProcesses() probably doesn't need the fancy subscripting. Can probably be replaced with printf $(ps -x -o command | grep '[I]mage[Optim|Alpha]' | wc -l)

- @jamesstout

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.