DEV Community

mpoiiii
mpoiiii

Posted on • Updated on

The Best Ultimate Guide to PNG and JPG: Detailed Introduction and Conversion Principles

Here's a detailed introduction to PNG (Portable Network Graphics) and JPG (Joint Photographic Experts Group) formats, so that you can confidently discuss this topic if asked in an interview, and provide a comprehensive explanation for up to ten minutes!

In digital image processing, PNG and JPG are two of the most commonly used image formats. Simply put, the main difference between the two is whether the image compression is lossy or lossless. PNG is a lossless compression format, while JPG is a lossy compression format.

Detailed Introduction to JPG

History of JPG

JPG is the first international digital image compression standard established by ISO and CCITT for static images, and it remains the most widely used image compression standard to this day.

JPG, also known as JPEG, is a widely-used lossy compression standard for photographic images, developed by the Joint Photographic Experts Group.

The team was formed in 1986 and released the JPG standard in 1992, which was subsequently certified as ISO 10918-1 in 1994.

The JPG format defines how to convert an image into a series of byte data streams, detailing the image compression and encoding process.

However, the JPG standard does not specify how these byte data should be stored or encapsulated on specific storage media (e.g., hard drives, memory, or other storage devices). This means that the storage and management of JPG files can be decided by developers according to different system and application needs.

JPG's compression method is typically lossy, meaning that the image quality suffers noticeable degradation during the compression process.

JPG Compression Modes

JPEG compression includes the following modes:

  1. Sequential Encoding: The image is processed once from left to right, top to bottom.
  2. Progressive Encoding: Suitable for longer image transmission times, the image is processed in multiple passes, gradually becoming clearer, similar to GIF transmission on the web.
  3. Lossless Encoding: The image is compressed and decompressed without any loss.
  4. Hierarchical Encoding: The image is compressed at multiple resolutions, allowing even high-resolution images to be displayed on low-resolution devices.

For details on the encoding algorithm, refer to the Wikipedia process.

Detailed Introduction to PNG

History of PNG

In early 1995, Unisys started charging for the use of the LZW data compression algorithm used in the GIF format. To avoid patent issues, the PNG format and Multiple-image Network Graphics (MNG) format were developed, for single images and animations respectively.

In August 1999, Unisys stopped offering free GIF patent licenses for free software and non-commercial software developers, further promoting the adoption of the PNG format.

During the promotion of the PNG format, many web browsers took a long time to fully support it. For example, Microsoft's default browser, Internet Explorer, did not support PNG's transparency effect until version 7.0.

Earlier versions (such as 6.0 SP1) required downloading patches or relying on additional scripts provided by websites to achieve support. This led to PNG not being widely adopted initially.

Early browsers did not support PNG images, with JPEG and GIF being the mainstream formats at the time. Due to GIF's limited color depth, images with color gradients usually used the JPEG format.

However, JPEG compression caused slight blurring of images. In contrast, PNG provided more accurate images at the respective color depth while maintaining a smaller file size. Over time, PNG became a preferred format for handling images with small gradients, and more browsers began to support PNG well.

PNG Storage Types

PNG images are primarily stored in three types:

  1. PNG 8: This type uses 8-bit storage, with each pixel representing 2 to the power of 8 (i.e., 256) colors, typically used for black-and-white or indexed color images.

  2. PNG 24: This type uses 24-bit storage, with each pixel composed of three 8-bit values representing the red (R), green (G), and blue (B) channels. Each channel ranges from 0 to 255, representing approximately 16.7 million colors in total.

  3. PNG 32: This type uses 32-bit storage, which adds an 8-bit transparency (Alpha) channel to PNG 24. Each pixel contains red (R), green (G), blue (B), and transparency (A) values, supporting around 16.7 million colors with 256 levels of transparency.

JPG to PNG and PNG to JPG Conversion Principles

Here are the basic principles for converting between these two formats:

  1. From JPG to PNG:

    • Decode JPG: First, decode the JPEG file. JPEG is a lossy compression format that compresses image data into a smaller file size using Discrete Cosine Transform (DCT). During decoding, the JPEG file is converted back to its original pixel data.
    • Encode as PNG: Then, encode the decoded pixel data into PNG format. PNG is a lossless compression format that compresses pixel data into a smaller file size using algorithms like DEFLATE, while retaining all original image details.
  2. From PNG to JPG:

    • Decode PNG: First, decode the PNG file. PNG files store pixel data using lossless compression algorithms. During decoding, the PNG file is converted back to its original pixel data.
    • Encode as JPG: Then, encode the decoded pixel data into JPEG format. JPEG uses lossy compression, compressing image data into a smaller file size using DCT and quantization, which results in some loss of image details.

Practical Implementation

Nowadays, converting image formats no longer requires writing code manually; numerous tools are available to accomplish this easily. These tools support not only conversions between PNG and JPG but also among various image formats. For instance, some websites offer conversion services for dozens of formats, including JPG to PNG and PNG to JPG.

Top comments (0)