How Do JPG Files Work?

In this blog post, we’ll explore why JPG is so widely used on the web, how it’s compressed, and its pros and cons.

 

Everyone has probably seen a warning at least once that they must use the .jpg file extension when uploading an image to an online forum. While there are various image file formats—such as BMP, JPG, GIF, and PNG—JPG is by far the most widely and longest-used format on the web. Let’s take a closer look at the background, principles, and pros and cons of JPG to understand what has made it so successful.
In an era where everyone owns a digital camera, we know that images are composed of millions of pixels. An image file contains the color information for each individual pixel, and the method of storing this information depends on how the pixels are represented. The most intuitive method is to assign numbers to the pixels and specify a color for each one. A file format that stores the three primary color attributes—R (red), G (green), and B (blue)—for each pixel is called a bitmap, or BMP. The more colors an image has and the larger it is, the more the file size of a BMP increases exponentially.
However, since transmission speed is critical on the internet, there are limitations to exchanging large image files. To overcome this, engineers developed an image storage method that maintains nearly the same image quality while significantly reducing file size, and the result is JPEG.
The key to the JPEG or JPG format’s ability to drastically reduce image file size lies in its use of human visual characteristics. The human eye can easily distinguish even small color differences in bright (high-luminance) areas, but it struggles to distinguish color differences in dark (low-luminance) areas. In other words, when luminance is low, information about saturation is not very important. JPEG compression first converts the image into luminance-centric data, then boldly discards information from areas that are difficult for humans to perceive, thereby reducing file size.
To achieve this, rather than compressing directly in the RGB color space, the image is first converted to a color space called YCbCr. Y represents luminance, while Cb and Cr represent the blue and red color components (chroma). It is important to note that YCbCr is not an absolute color space like RGB, but rather a representation of RGB information transformed for luminance-centric processing. The transformed image is divided into small 8×8-pixel square blocks to simplify the processing.
Within these 8×8 blocks, the Discrete Cosine Transform (DCT)—the core of JPEG compression—and quantization are performed. DCT is a mathematical transformation that represents the array of pixel values within a block as a sum of cosine functions; a key property of this transformation is that information is concentrated in specific locations. In the upper-left portion of the transformed matrix, relatively large coefficients that determine the overall brightness of the block are concentrated, while the lower-right portion contains detailed high-frequency components. To illustrate this property, it is similar to placing items in a locker with multiple compartments and grouping only the important items into a few of them.
During the quantization stage, only a few of these 8×8 (64 total) coefficients are retained, while the rest are discarded. In standard JPEG compression, only a few important coefficients are kept, and the rest are discarded; this process is precisely what causes the loss of quality. While discarding some of this information inevitably leads to a loss in image quality, it significantly reduces the overall file size—in some cases by as much as tenfold.
The JPEG format has been widely used because of its exceptional efficiency in terms of file size relative to data capacity, and it is likely to remain in use for the foreseeable future. However, it also has its drawbacks. Because some information is intentionally discarded through quantization, lossy compression occurs, and if the same image is repeatedly saved as a JPEG, noticeable quality degradation accumulates.
For professionals who need to perform detailed work or multi-step image editing, lossy compression—which degrades quality with each save—can be critical. They prefer lossless compression, which reduces file size without losing any image information. GIF and PNG are the lossless compression formats most commonly encountered by general users, and their image quality does not degrade no matter how many times they are saved. However, GIF faced widespread limitations in the past due to patent issues with its internal algorithms, while PNG was developed to overcome those limitations, offering a wide color range, sharp image quality, and a relatively high compression ratio among lossless formats.
Nevertheless, JPEG has the advantage of a relatively simple compression process, native support in many graphics tools, and fast file loading speeds. Considering the physical limitations of the web and usability, the dominance of JPEG—which offers small file sizes with virtually no visible difference to the human eye—is unlikely to be easily challenged anytime soon.

 

About the author

Cam Tien

I love things that are gentle and cute. I love dogs, cats, and flowers because they make me happy. I also enjoy eating and traveling to discover new things. Besides that, I like to lie back, take in the scenery, and relax to enjoy life.