I came to know of yahoo!’s awesome lossless image compression service smush.it through this blog post. After playing around with smush.it for sometime I realised that there is no command line client that does it to images in local directories. So thought why not write one and this is the result – https://bitbucket.org/abhirama/smushit. You can download the binary from github.
Directions for use:
This is a command line tool which losslessly compresses all the images in the passed directory using yahoo!’s smush.it image compression utility – http://www.smushit.com/ysmush.it/, downloads the smushed images and replaces the original image with the smushed ones in their respective directories.
Usage:
java -jar smushit.jar [options]
Mandatory options:
-imageDir
Root directory of the images to be smushed. The program recursively traverses all sub directories of this directory smushing images.
Optional Options:
-verbose
Will print detailed messages on screen.
-dryRun
Will not download the smushed images.
-imgExtensions
Specify a list of comma separated image extensions. Only images with those extensions will be smushed.
Example usage:
java -jar smushit.jar -imageDir=/foo -verbose=true -dryRun=false -imgExtensions=gif,png,jpeg
The above will smush all images with extensions gif, png and jpeg in directory foo and it’s sub directories and will download the smushed images and replace the original with the smushed images.
Important:
Sometimes smushit converts gifs to pngs if the resulting png files are smaller. Take a look at this faq – http://developer.yahoo.com/yslow/smushit/faq.html#faq_giftopng . If this happens with your images, your image folder will have the original image as well as the newly converted png image.
Give it a spin and let me know if there are any bugs or any enhancements needed. It is in a fledgling state and I will keep on improving it in the coming days.
PS:If this interests you, take a look at this python script.
PS:If you want to run this as a batch file in windows, follow the instructions posted by here.
great work. Very nice and thank you. Due to a current bitbucket error on the smushit.zip I downloaded the tip zip file which worked.
Thanks for pointing it out Dan. Wrote a mail to Bitbucket regarding this and they said they will be deploying a fix very soon. Until then the tip file will have to do I guess.
Thanks so much for doing this! This made it very easy to incorporate smush.it into my build process. I really appreciate your releasing the code.
You are very much welcome 🙂
Well, I keep my thank you, but I’m running into an issue 🙂 The resulting smushed files don’t actually seem to be getting downloaded. I run
>java -jar smushit.jar -verbose=true -imageDir=c:\temp\pngs -dryRun=false
and see outputted
Smushing files in directory – c:\temp\pngs
Smushing files:
c:\temp\pngs\body_leftborder.png
Source Image:body_leftborder.png, Source image size:729, Smushed image size:327, Percentage saving:55.14
Downloaded smushed image – http://smushit.zenfs.com/results/67de70c4/smush/body_leftborder.png
Saved image – c:\temp\pngs\body_leftborder.png
————————————-
Total images uploaded – 1
Total images smushed – 1
Total uploaded images size – 729 bytes
Total smushed image size – 327 bytes
Percentage saving – 122.93578%
————————————-
But when I dir on c:\temp\pngs afterwards, I get
Directory of c:\temp\pngs
01/04/2011 11:03 AM 729 body_leftborder.png
Any ideas? Thanks.
Sorry for the late reply. I just checked the tool and see that it is working fine. If you can pass me the image in question, I can run the tool it on it and check.
I know this is going to seem completely newb…
Is this something that I run from my root directory in linux? or does this work in Windows…
If in windows, could you please take a minute to explain exactly how to run from a cmd prompt or is there a java program that I need to have installed first and run from?
Thanks in advance.
Sorry for the late reply. It does not matter which OS you use as long as you have JRE(Java Runtime Environment) installed. If you are in windows, give the directory in windows format, if you are in linux, give the directory in linux format.
The example usage mentioned has dryRun instead of -dryRun.
Fixed, Ms Keen eyes ;).
Wow, its running now but I feel compelled to say wow!
For most web developers I think using smush it is a marginal thing given you may only save a couple of a % and the fact that the interface precludes processing bulk images in multiple directories. But this makes so easy you cannot say no!
I had almost 5k images to do.
It did about half then I got this message ‘ Exception in thread “main” javax.imageio.IIOException: Can’t get input stream from URL!’.
Is this because SmushIt decided that I had been a little too busy?
Will
I guess I will give ita couple of hours and try again.
Maybe yahoo! is blocking the ip due to request bombardment, but I have never tested the tool with more than 500 images. So, please do let me know if you are able to do the conversion.
This is a great tool! Are the smushed images supposed to replace the original ones (desired)? When I run it I get the smushed one alongside the original with a mangled filename, for example “cd74f1e1%2Fsmush%2FsignUpButton.jpg” for the original “signUpButton.jpg”
Can you please confirm as to whether you see this behavior consistently?
This consistently happens to me as well, using the latest version.
Im having this issue aswell – latest build
This consistently happens to me as well, using the latest version.
Just happened to me as well. 5b6886d3%2Fsmush%2Fsubmit.png
The issue has been fixed. Grab the latest file here – https://bitbucket.org/abhirama/smushit/downloads/smushit-v0.2.zip
I love your superdog picture! You are a master of indoor light I’ve noticed! And you’ve inspired me to go hiking, which we shamefully have not done yet this summer. I have no exeu3csRs0;no baby over here!
Thanks for the fix!
Tested, and working fine!
Thx for the fix and keep up the good work
Hi, can you tell me if there is a way to run your wonderful tool behind a proxy authentication server?
To access the web, all connections go through a proxy, requiring a user/login.
This is my the error log I’m getting:
Adding file:X:\XXXXX\XXXX.jpg
Exception in thread “main” org.apache.http.conn.HttpHostConnectException: Connec
tion to http://www.smushit.com refused
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnect
ion(DefaultClientConnectionOperator.java:159)
at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.ja
va:149)
at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPool
edConnAdapter.java:108)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultReq
uestDirector.java:415)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpCl
ient.java:641)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpCl
ient.java:731)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpCl
ient.java:709)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpCl
ient.java:700)
at com.abhyrama.smushit.SmushIt.smushHelper(SmushIt.java:112)
at com.abhyrama.smushit.SmushIt.smush(SmushIt.java:69)
at com.abhyrama.smushit.SmushImages.smushHelper(SmushImages.java:82)
at com.abhyrama.smushit.SmushImages.smush(SmushImages.java:58)
at com.abhyrama.smushit.SmushImages.main(SmushImages.java:189)
Caused by: java.net.ConnectException: Connection timed out: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(Unknown Source)
at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSoc
ketFactory.java:123)
at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnect
ion(DefaultClientConnectionOperator.java:133)
… 12 more
Nope, this will not work behind a proxy.
Thanks for the command-line tool. This worked really well and reduced existing images by over 30%.
Thank you, very nice tool. For Window users I thought I’d share this .bat file. It allows you to drag a directory onto it to execute the script. It also will output a results.txt file. Just change the path to smushit.jar and results.txt.:
java -jar “C:\SmushIt\smushit.jar” -imageDir=%1 -verbose=true -dryRun=false -imgExtensions=gif,png,jpeg,jpg >”C:\SmushIt\results.txt” 2>&1
Put that in a file and save with a .bat extension (use notepad for example).
Awesome script, this makes me sit back and watch 🙂
so, apparently, this just uses yahoo’s hosted smush.it? Why not replicate the algorithm they use rather than having to depend on their web service?
I did an attempt here – https://abhirama.wordpress.com/2012/03/18/image-masher/, but much leaves to be desired in this. There is also another one here – https://github.com/thebeansgroup/smush.py/.
Hi, that’s really nice tool ,but i have a issue with it, when the name of image is not utf8 character (in my case danish) the common line stops execution, is there a way to skip such images without stoping the script.
Thanks in advance
This tool now works like a charm for me.. Thanks for posting this.
Amazing blog! Do you have any tips and hints for aspiring
writers? I’m hoping to start my own blog soon but I’m a little
lost on everything. Would you recommend starting with a
free platform like WordPress or go for a paid option?
There are so many choices out there that I’m completely overwhelmed .. Any ideas? Cheers!
Greetings, I think your blog may be having browser
compatibility problems. When I look at your site in Safari,
it looks fine however when opening in Internet Explorer, it has some overlapping issues.
I merely wanted to provide you with a quick heads up!
Besides that, great blog!
Thanks a lot for that pretty good tool which avoid time consuming use of smush.it.. 🙂
But, i encounter an issue, which is weird by the way… I have this issue with only one file and i’m asking myself if there is any size limit to treat images with your utility.. ?
The issue is simple, i put the image in the temp directory as i do usually. But when i ask to smush using the jar file, the program can’t see my image and close itself as soon as i open it.. This issue is only for 1 file which is 1,82Mo size.. All other images are smushed correctly.
I don’t understand.. Please help me out 🙂
I just like the helpful information you provide in your articles.
I’ll bookmark your weblog and take a look at once more right here regularly.
I’m somewhat sure I’ll learn many new stuff right here!
Good luck for the following!
Does it still work ? I see the project on Github is around 2 years old.
This tool has been awesome for me for years. Just went to smush a few images and got an error I’ve not seen before. The images weren’t smushed. Here’s the error:
Exception in thread “main” org.apache.http.client.HttpResponseException: Moved Permanently
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:67)
at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:55)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:735)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:709)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:700)
at com.abhyrama.smushit.SmushIt.smushHelper(SmushIt.java:112)
at com.abhyrama.smushit.SmushIt.smush(SmushIt.java:69)
at com.abhyrama.smushit.SmushImages.smushHelper(SmushImages.java:82)
at com.abhyrama.smushit.SmushImages.smush(SmushImages.java:58)
at com.abhyrama.smushit.SmushImages.main(SmushImages.java:189)
Yep, smush,it is dead, yahoo ended it..Thank you abhirama for your work
What’s up, this weekend is good in favor of me, since this moment i am reading this great educational article here at my
home.