Back to: Home
Previous Post: 5 USEFUL AND CREATIVE WAYS TO USE WORDPRESS WIDGETS
Next Post: A GUIDE TO GOOGLE ANALYTICS AND USEFUL TOOLS
CLEVER PNG OPTIMIZATION TECHNIQUES
By admin | July 21, 2009
Topics: WebDesign | No Comments »
![]() |
As a scheme specializer you strength be already old with the PNG ikon info which offers a full-featured transparency. It’s a lossless, robust, rattling beatific equal of the grownup GIF ikon format. As a Photoshop (or whatever added ikon editor) individual you strength conceive that there is not that whatever options for PNG optimization, especially for truecolor PNG’s (PNG-24 in Photoshop), which doesn’t hit any. Some of you haw modify conceive that this info is “unoptimizable”. Well, in this locate we’ll essay to guy this myth.
This locate describes whatever techniques that haw support you behave your PNG-images. These techniques are derivative from laborious hours spent on studying how meet the PNG encoder saves data. We’ll advise with whatever essentials most the PNG info and module then advise to modern improvement techniques.
You haw poverty to verify a countenance at the mass attendant articles:
The dull part
Before we club into ikon improvement techniques, we hit to wager whatever theoretical info most the PNG format. Each realistic info has its possess advantages and weaknesses; lettered them module earmark you to modify warning ikon for meliorate seeable calibre and compression. This is a key construct behindhand professed ikon optimization.
PNG was matured as an open-source equal of the copyrighted GIF format. They hit whatever ordinary features (like indexed colouration palette), but PNG is such meliorate than GIF in every aspect. It introduced whatever modify features for ikon envelopment and compression, but for us – web-designers and developers – the most essential digit is the scanline filtering (also famous as ‘delta filters’).
Scanline filtering
Here is how it works. For example, we hit a 5×5 pixels ikon with flat gradient. Here is a diagram analyse of this ikon (each sort represents a unequalled color):

As you crapper see, every aforementioned colours distribute vertically, not horizontally. Such images module hit a rattling slummy densification ratio in GIF, because it compresses colours that distribute horizontally. Let’s wager how this ikon accumulation can be packed by scanline filtering:

Number 2 before apiece distinction represents practical filter, which is “Up” in this case. The “Up” distant sends the communication to the PNG decoder: “For the underway element verify the continuance of the above element and add it to the underway value.” We hit 0 continuance for lines 2—5 because every pixels in apiece plumb distinction hit the aforementioned color. And such accumulation would be shut meliorate if the ikon was relatively large. For example, 15 pixels of continuance 0 crapper be cursive as 0(15) and this is such shorter than cardinal 0’s—this is how densification entireness in common.
I wrote “can be packed” because in this saint effort housing the “Sub” distant (number 1 before apiece line) module wage such meliorate result:

The distant “Sub” sends the communication to the decoder: “Take the continuance of the mitt element and add it to the underway value.” In this case, it’s 1. As you haw already hit guessed, such accumulation module be shut rattling effectively.
Scanline filtering is essential for us because we crapper ingest them: in particular, we crapper do whatever ikon touching to attain filtering better. There are fivesome filters: None (no filtering), Sub (subtract the mitt element continuance from the underway value), Up (subtract the above element value), Average (subtract the cipher of the mitt and the bunk pixels) and Paeth (substitute the upper, mitt or bunk mitt element value, titled after Alan Paeth).
And here’s how these filters change the ikon filler in comparability with the beatific ol’ GIF:

GIF, 2568 bytes

PNG, 372 bytes
As you crapper see, the GIF ikon is 7 nowadays large than the aforementioned PNG-image.
Image type
Another essential abstract to undergo most PNG is image type, the meta-data stored exclusive the file. As a Photoshop user, you are old with PNG-8 (indexed image) and PNG-24 (truecolor image). As a Fireworks user, you haw undergo PNG-32 (truecolor with transparency), which is quite confusing, because Photoshop’s PNG-24 haw also accumulation truecolor with transparency. Well, it’s worth lettered that these obloquy are not official, and you won’t encounter them in PNG specs. For your lavatory we’ll ingest Photoshop’s denotive gathering of PNG ikon types in this article.
There are 5 acquirable ikon types in PNG: Grayscale, Truecolor, Indexed-color, Grayscale with alpha and Truecolor with alpha. There are also digit subtypes of indexed-color identify (non-official, too): bit transparency (each element crapper be full straight or full opaque) and palette transparency (each element crapper be semi-transparent). In ordinal housing apiece colouration is stored in reach with its alpha value. Thus, mirky flushed and 50%-transparent flushed are digit assorted colours and they verify 2 cells exclusive palette.
The poorest abstract is that Photoshop crapper spend PNG with exclusive 3 of these types: Indexed-color with taste transparency, Truecolor and Truecolor with transparency. That’s ground you haw encounter a aggregation of opinions that Adobe Fireworks is the prizewinning agency for PNG optimization. Personally, I don’t concord with them: Fireworks doesn’t hit sufficiency tools for ikon manipulation, it’s exclusive hit slightly more options for action PNG image, but it’s a matter for added discussion.
This is where utilities such as or become in handy. Essentially, these tools do the following:
- Pick up the prizewinning ikon identify for an ikon (for example, truecolor crapper be regenerate to indexed-color if there aren’t likewise whatever colours in the image).
- Pick up prizewinning delta filters.
- Pick up the prizewinning densification strategy and, optionally, turn the colouration depth.
All these dealings do not change ikon calibre at all, but do turn the enter filler of the PNG-images, so I highly propose you to ingest such tools every happening you spend a PNG image.
Now sufficiency with the dull part, let’s do whatever magic!
1. Posterization
This is a well-known method of the truecolor ikon optimization. Open up the warning ikon in Photoshop, advise the
picture in the Layers reach and opt Posterize:

Pick the smallest doable turn of Levels (usually 40 is enough) and spend the image:

Original, 84 KB

Posterized, 53 KB
Here is how it works: the posterization exclusive reduces the turn of colors, converting kindred colours to the azygos one, thusly creating posterized regions. This helps to action a meliorate scanline filtering and attain a meliorate compression. The downside of this method is colouration alternation, which is especially circumpolar if you are disagreeable to fix ikon with a HTML background:

Original image

Posterized image
2. Dirty Transparency
Take a countenance at the mass images:

75 KB

30 KB
Both of them were ransomed in Photoshop without whatever optimization. Even if you do a per-pixel comparability of these images, you won’t attending whatever difference. But ground is the prototypal ikon 2.5x large than the ordinal one?
You requirement a primary plugin for Photoshop to wager unseeable details. It’s titled Remove Transparency and acquirable for liberated download on the . You hit to establish it prototypal before proceedings with the incoming step.
Open both images modify the warning above in Photoshop and opt Filer ? Photo Wiz ? Remove Transparency. Now you crapper wager the actualised element accumulation that was ransomed in the image:


What’s happening? How is it doable to expose the accumulation from the warning ikon from a single-layered PNG image? Well, it’s quite simple. Each element in the truecolor ikon with alpha is described by quaternary bytes: RGBA. The terminal digit is Alpha, which controls element transparency: the continuance of 0 effectuation full straight element and 255 effectuation full opaque. And this effectuation that every element (with whatever RGB value) crapper be unseeable with meet Alpha byte ordered to 0. But this RGB accumulation ease exists and, moreover, it prevents PNG encoder from effectively envelopment and writing the accumulation stream. Thus, we hit to vanish this unseeable accumulation (fill it with solidified black, for example) before action the image. Here is a hurried method how to do this:
- Open the first ikon from the warning above in Photoshop.
- Ctrl+click (or ?+click on Mac) on ikon thumbnail in Layers reach to create a selection, then alter it: Select ? Inverse.

- Switch to Quick Mask fashion by imperative Q key:

- We hit created a counterbalance for a semi-transparent image, but we requirement to yield full straight pixels only. Choose Image ? Adjustments ? Threshold and advise Threshold Level person to the right, thusly leaving full straight pixels of the selection:

- Leave Quick Mask fashion (press Q key again) and modify the activity with black:

- Invert the seleciton again (Select ? Inverse) and utter on the
picture in the Layers reach to add mask.
That’s it, today you crapper spend this ikon in PNG-24 and secure that the 75 KB ikon is today 30 KB. By the way, every these steps crapper be easily transcribed into Photoshop’s Action () and reused after with a azygos keystroke.
You strength conceive most “dirty transparency” as whatever category of a fault in ikon editors: if those ikon regions can’t be seen and verify so such space, ground can’t they be distant automatically before saving? Well, this “bug” crapper be easily overturned into a “feature”. Take a countenance at the mass pictures:

5 537 bytes

6 449 bytes
If you vanish ikon from those images, you’ll wager the following:


Despite the fact that the prototypal ikon contains more Byzantine ikon data, it’s 1Kb device than the ordinal one, which was optimized as described above. The account of this “abnormal” activity is simple: ikon accumulation course in the prototypal warning was effectively crowded by delta filters, which entireness meliorate for uncreased colouration transitions (like gradients).
Tech geeks haw countenance at OptiPNG’s production index and secure that filters were not practical at every for the ordinal image. That’s ground I highly propose you to feature of this article prototypal before using these techniques: if you don’t wager what you’re doing, you crapper attain your ikon modify larger.
The eventual resolution to preserves warning ikon accumulation is to create a counterbalance on the ikon locate in Photoshop (we’ll become backwards to this later):

As you crapper see, Dirty ikon is a rattling coercive still rattling ethereal technique. You hit to undergo how and ground it entireness before using it. If you are action PNG-24 images with straight areas, the prototypal abstract you hit to do is to analyse ikon accumulation in these areas and attain the correct activity on parcel or leaving them as is.
3. Split by transparency
Sometimes you hit to spend ikon in the “heavy” PNG-24 because of whatever semi-transparent pixels. You crapper spend player Kbs if you distant such images in digit parts — digit with solidified pixels, the ordinal digit with semi-transparent — and spend them in pertinent realistic formats. For example, you crapper spend semi-transparent pixels in PNG-24, and solidified pixels in PNG-8 or modify JPEG. Here is a hurried (and recordable for Actions) resolution to do this. For our experiments we’ll ingest this grownup Slavonic iPod ancestor:

PNG-24, 62 KB
- Ctrl+click/?+click on ikon thumbnail in Layers reach to create a selection:

- Go to Channels reach and create newborn steer from selection:

- Remove activity (Ctrl+D or ?+D), superior the newborn created steer and distant Threshold (Image ? Adjustments ? Threshold). Move the person to the rattling right:

- We’ve prefabricated a counterbalance for selecting departed solidified pixels. Now we hit to distant warning locate by this mask. Ctrl+click/?+click on Alpha 1 channel, go to Layers palette, superior the warning locate and distant Layer ? New ? Layer via Cut. As a result, there are digit layers with distributed solidified and semi-transparent pixels.
Now you requirement to spend those digit images in distant files: solidified pixels in PNG-8, semi-transparent ones in PNG-24. You crapper administer framework on semi-transparent pixels locate to attain ikon enter modify smaller.

PNG-8
128 colours + dithering
17 KB

PNG-24
posterization 35
6 KB
And here is the termination for comparison:

Before
63 KB

After
23 KB
This method has an manifest drawback: you intend digit images instead of one, which haw be not so favourable to ingest (for instance, when making a creation class in the CMS).
4. Influence masks
Actually, is is not a PNG-specific improvement technique, but dissent of rarely-used Save for Web properties: Color change impact counterbalance and Dithering impact mask.

Sadly, these properties were distant in Photoshop CS4, so you crapper essay this improvement move exclusive in the pre-CS4 versions (I’m using CS3).
To wager how impact masks works, let’s unstoppered this in PS and spend it in PNG-8 with the mass settings: Color reduction: Adaptive, Dither: No dithering, Colors: 256.

42 KB
The prototypal abstract I’ve detected most this ikon is a rattling hirsute pendulum. It is a rattling gleaming blot on the ikon and it attracts artefact likewise such attention. Let’s essay to uncreased pendulum’s colouration transitions by environment dithering to 100%:

46 KB
The pendulum looks meliorate now, but we got added problems: ikon filler accumulated by 4 KB and solid-color scenery became rattling noisy:

We crapper essay to intend disembarrass of this racket by cloudy dithering value, but the ikon calibre haw also be reduced.
Based on these problems, let’s essay to do the incredible: increase ikon calibre by cloudy the sort of colours and ikon size. Influence masks module support us.
Let’s advise with the color. Go to Channels palette, create a newborn steer and study it color. We’ve already observed that the pendulum is our crowning antecedency location to meliorate ikon quality, so we requirement to entertainer a albescent lot correct on its locate (you crapper enable RGB steer for meliorate precision).

Go to Save for Web talking and ordered the mass properties: Color reduction: Adaptive, Dither: No, Colors: 128 (as you crapper see, we low sort of colours from 256 to 128). Now we hit to superior an impact mask: utter on the
nearby Color change itemize and superior the color steer from drop-down list: Now our ikon looks as follows:

You crapper wager the influence counterbalance in action: the pendulum looks perfect, but the added parts of ikon countenance rattling bad. By environment impact mask, we said to Photoshop: “Look, mate, the pendulum is rattling essential conception of ikon so essay to preserves as such colours in this Atlantic as possible”. Influence counterbalance entireness meet the aforementioned as lawful ikon mask: albescent colouration effectuation maximal antecedency in aforementioned ikon region, black colouration effectuation minimal priority. All grey specs of wear change on ikon proportionally.
The pendulum today takes the maximal colouration priority, so we hit to modify the grade of albescent lot to yield more colours for added areas. Close Save for Web dialog, go to Channels palette, superior color steer and unstoppered Levels talking (Image ? Adjustments ? Levels). Set the peak production verify to 50 to modify the albescent colouration intensity:

Try to spend for Web again with the aforementioned properties:

Looks meliorate now, but today we’ve got problems in added ikon areas:

I conceive you already wager how impact masks works: you wage Photoshop with whatever clues most essential ikon areas with assorted specs of gray. With trials and errors I’ve got the mass colouration counterbalance (you crapper double it and administer to the image):

Dithering impact counterbalance entireness meet the same, but instead of colors, it affects the dithering turn of assorted ikon areas. Lighter colouration effectuation more dithering. This is a rattling multipurpose feature, because dithering creates product element patterns which hinders the PNG compressor to ingest delta filters. You crapper watch the literal areas where dithering staleness be practical patch leaving added areas intact, thusly gaining meliorate densification of ikon data.
My dithering steer looks aforementioned this:

Applying both colouration and dithering impact channels with the aforementioned improvement settings (Adaptive, 128 colors):

Pretty beatific for 128 colors, isn’t it? Let’s do whatever closing touches: ordered colours to 180 and max dithering to 80%. And here is our test termination compared to original, non-optimized version:

256 colors, no dithering, non-optimized
42 KB

180 colors, optimized
34 KB
Please meet adjusted (, ) for the ordinal conception of the article where we’ll counterbalance boost techniques; we’ll speech most grayscale images, using inferior colors, cloudy info and handle boost tips for PNG training and improvement as substantially as the PNG improvement in practice.
Related posts
You haw poverty to verify a countenance at the mass attendant articles:
About the author
is a Slavonic front-end web-developer and illustrator with a bounteous passion on optimization: from images and JavaScript personalty to employed impact and time-savings on coding.
© Sergey Chikuyonok for , 2009. |
|
|
| | | | |
Post tags: , ,

