How to use OptiPNG recursively

Few hours ago I wrote about image optimization with Jpegoptim. Now I’ll write few words about Optipng, a PNG and GIF optimizer. 

OptiPNG is a PNG optimizer that re-compresses image files to a smaller size, without losing any information. This program also converts external formats (BMP, GIF, PNM and TIFF) to optimized PNG, and performs PNG integrity checks and corrections. If you wish to learn how PNG optimization works, or to know about other similar tools, see the PNG-Tech article A guide to PNG optimization.

[adsense:336×280:6928840684]

The only drawback of this piece of software is that it saves optimized images only in PNG format, using the same filename. This means you have to replace all references to gif files with png ones (mostly in CSS stylesheets and template files). Another disadvantaje is that sometimes, small gif files are actually smaller in size than the optimized png’s, so it’s up to you which files you’re going to replace.

To parse a whole structure of directories in Linux, you can use the following commands:

Find png files and optimize them:

find -type f -name "*.png" -exec optipng {} ;

Find gif files, optimize and convert them to png:

find -type f -name "*.gif" -exec optipng {} ;

You can use -o(1-7) parameter before brackets, but there’s almost no difference between the default value (-o2) and -o7 (slow and hardware intensive).

If you know better tips on how to use OptiPNG please leave a comment below.

[adsense:336×280:5172905888]

Articolul precedent
Articolul următor

3 COMENTARII

  1. You can use such parameters:
    optipng -nc -nb -o7
    The arguments -nc -nb tell optipng not to alter any colour information (or we risk changing the appearance of the optimized image) and -o7 tells it to use the best compression.

    If you want compress faster with (little more filesize) than use lower -o value, for example -o5

    • „…or we risk changing the appearance of the optimized image”

      Optipng is a losless png optimizer, it never changes the image.
      You misunderstud what that those parameters means.

      As we know, a 24bit image can contain a maximum 16777216 different colours.
      A 16bit image can contain 65536 different colours.
      An 8bit image can contain 256 different colours.

      Let’s say you have a 24bit image. In almost all cases, your 24bit image really contains less colours than that 16 million.
      So, if your 24bit image doesn’t contain more than 65536 different colours, it can be converted to a 16bit image, without losing anything. Maybe this 24bit image doesn’t even contain more than 256 different colors, than it can be converted to an 8bit, also without changing anything in it.
      And this is what exactly OptiPNG do with bit depth reduction, it reduces the bit depth ONLY if it it possible; only if the unique numbers of colours does not exceed the numbers that can be saved in a given bith depth.

      Now, some info about the colour palette reduction.
      Again, we have a 24bit image, and let’s say has only 38000 colours in it’s palette.
      Immediately we know that this image can be converted into 16bit image.
      But, if we further examin the image, and we look at the pallette, we may find out, that the palette contains redundant entries. Like duplicate entries that indicate the same RGB value, or entries that do not have a correspondent in the raw pixel data. So these can be removed from the palette without changing anything in the image.
      In this example let’s say that most of those 38000 colours in the palette duplicates and we remove those duplicates. After removing them, we can end up having really only 200 different clours in the image, so it doesn’t even need to be a 16bit image it can be reduced to a 8bit image.
      This is what OptiPNG do with the colour palette reduction, also ONLY if these modifications are possible.

      Those two flags (-nc, -nb) only thells OptiPNG to not change the bit-depth and do not reduce the palette even if it is possible.
      These flags are there, because in some cases, you must have your image to be exactly 24bit,16bit or 8bit for some reason. Same for the palette reduction; you migt need all the palette entries, even if they are duplicates or not used.

      Also the -o flag desn’t mean the compression level!
      It means the optimization level; it tells OptiPNG how many times to try different combinations of optimizations. (Run OptiPNG without parameter to see what they are.)
      Higher numbers after the -o flag, while it can give you smaller filesize, they are aslo significantly incrase the otimization time, especially if you have a large resolution image and/or optimizing a lot of files at once!

      Also there is another really useful flag, that can further optimize the result.
      This flag removes metadatas from the image.
      If you will using those informations, for example when you will put the images into some kind of a catalogue software, and you label them, etc. you don’t want them to be removed.
      But if you don’t need them, use this flag:
      -strip all
      This will remove everything, every metadata, reducing even further the image size.

      So, this is the full command that will reduce the image size as much as possible:

      optipng -full -o7 -strip all

LĂSAȚI UN MESAJ

Vă rugăm să introduceți comentariul dvs.!
Introduceți aici numele dvs.
Captcha verification failed!
Scorul utilizatorului captcha a eșuat. va rog sa ne contactati!

Acest site folosește Akismet pentru a reduce spamul. Află cum sunt procesate datele comentariilor tale.

- Advertisement -spot_img
Noutăți

Articole similare

- Advertisement -spot_img