Jump to content

Optimise PNG files


blackosx
 Share

9 posts in this topic

Recommended Posts

For web work and in this case, boot manager imagery, 24-bit PNG files don’t have to be used and in most cases the same result can be achieved from an 8-bit PNG. Though be aware that some images, for example ones using fine gradients, can suffer from this process so you should check the results before your final use.

 

8-bit files are smaller than 24-bit files.

 

Enter: pngquant

 

If you just wish to simply compress a directory of .PNG files, run the following command to create new compressed versions of the files in the same directory whilst retaining the original files.



pngquant 256 *.png


There are other options available. Check pngquant’s help page for more info.

 


How does this refer to Clover?

 

Once upon a time, Clover’s theme files were uncompressed 24-bit PNG files saved inside an ICNS file. Clover would read each .ICNS file, extract the required size of .PNG and draw that icon to screen.

 

Clover’s repository contained a number of themes and all these 24-bit PNG files and ICNS files resulted in the theme files making up a large percentage of the overall repository size.

 

After discussion with Slice, Clover was then changed to be able to directly load individual PNG files rather then extract them from an ICNS file, though traditional .ICNS files can still be used.

 

To convert all the existing themes, I created a simple bash script to scan and compress all the Clover theme directories. This script was hosted on projectosx but since that’s no longer available I am adding it here for legacy purposes.

 

It’s uses a two step process:

 

1 - Find and compress all .PNG files.

2 - Find all .ICNS files, extract and compress each 128x128 .PNG image saving back with .ICNS extension to satifsy Clover.

 

-r option can be used to rebuild ICNS files after image optimisation.

 


Updated to v1.6 of the script which uses v2.5.2 of pngquant.

 


 

Usage is: (v1.5 upwards has an option to rebuild icons files after optimisation with additional -r at the end)



./optimiseTheme.sh /path/to/directory_of_images -r


Note: The script overwrites the files it finds with the optimised versions. If you wish to retain the original files then keep those separate.

 

This script could be improved but I never bothered as it did what I needed back then. If anybody does improve it then please post it back here.

 

Conclusion

 

All existing Clover themes that are in the Clover Theme Repository are now compressed (at least I think they are), so this script is not really needed any more. But, if you create a new theme using the old way of doing it (with .ICNS files), or if you do have a copy of an old theme that you want to compress then this script will still help.

 

As far as 'optimised' .PNG files go though, you should always use them for whatever work you do as smaller files load quicker and take less space. 

  • Like 7
Link to comment
Share on other sites

  • 10 months later...

Hi,
Yous script is awesome
very well written, it helps me understand how scripting works, and also saves a lot of time
 
I've been using (trying) to actually compress and optimize the size of 1200+ icons in the base Max os x system.
from 600+MB, I'd like to get to 350-400 MB.
on a few icons in a test folder so far so good, it search for it, open it into a folder, compress each and then re-encapsulate it into an .icns file
but your script doesn't seem to be handling spaces in names very well.
like any app with a space in the name like Quick Time.app ( actually Quick\ Time.app) will fail
 
would you have any idea on how to tweak your script in order to go through files and folders which contains spaces in ther name?

 

thanks

Link to comment
Share on other sites

Hi

 
Yeah. It was an old script and I think Clover themes didn’t use files containing spaces so I never took that in to account back then.
 
I’ve attached the latest version of the script (v1.5) to the opening post.
 
The changes from v1.2 are:
- an updated version of pngquant (v2.5.2).
- script runs from a symbolic link.
- works with files and directories containing spaces in their names.
- added -r option to rebuild ICNS files after optimising.
 
usage:
./optimiseTheme.sh /path/to/ICNS files [-r]
  • Like 1
Link to comment
Share on other sites

That is absolutely amazing, I never scripted or coded anything but with your script, that really the beginning of something very exciting.

That is out of the scope of your script, but since that you seem to be quite gifted with scripting, I hope you won't mind me asking the following...

 

Iconutil creates a set of 8 .PNG files even though the .icns file is only 1 .PNG file. so I end up with .ICNS being 100 times heavier than the original version

check that file: /System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/RightContainerArrowIcon.icns  it is 3Kb

after being processed by iconutil, it is modified into 8 different .PNG and PNGQUANT cannot do much about the weight of 8 files vs 1

the end file, is then 149 Kb.....

 

Would there be a way to implement in your script an "if", based on the size of the output .icns. not to delete the original file if the converted file is heavier

Like

If size of original .icns file is < than size of the resulting .icns file 

then remove the temp and don't delete original file

Else remove original file and copy new icon to original location

 

Because I added -size +100k into the search parameters, which seems to be fine, but as a matter of fact, if you have some time, I'd very much like to understand how you'd have scripted the "if" that would compare the size of two files each time.

I found several great sites that deal with that, but I'm not advanced enough to correctly write such complicated lines of script.

 

Thanks again, whatever you do. Your script is awesome, it solved my challenge, I gained 250Mb on the 650Mb of icons that the base Mac OS X system contains.

Link to comment
Share on other sites

I’ve updated the opening post with v1.6 which now retains the original ICNS file if smaller than the processed one.

 
Please note:
 
This script was orignally intended for Clover and strips all image sizes other than 128x128px from the ICNS file as at the time Clover did not use any other image size.
 
An original Apple system ICNS file will contain multiple image sizes, including large images for retina displays, to allow the system to present the best image experience to the user at any size. Using this script on system icons will be a detriment to the look of OS X finder as it will only have 128px icons to use. This may not be an issue for your own personal use but if you’re working on something that other people may use then bear this in mind.
 
Link to comment
Share on other sites

Hey man, thanks a lot !

I know that your script was not as first for the usage that I'm using it.

But as a matter of fact it does a really great job !!!

 

your a very talented scripter and I'll definitively learn from your knowledge.

Thanks a lot !

Link to comment
Share on other sites

 Share

×
×
  • Create New...