ReCaseText
10 min read

Image Format Conversion: When to Use PNG, JPEG, and WebP — and How to Switch Between Them

PNG, JPEG, and WebP each compress images differently and excel at different content types. Here's how each format works internally, when to choose which, and what actually happens when you convert between them in the browser.

Every image on your computer or phone is stored in a specific format — a set of rules that dictate how pixel data is organized, compressed, and decoded. The format you choose determines the file size, the visual quality, whether transparency is preserved, and which applications and browsers can display the image. Choosing the wrong format is one of the most common mistakes in web development and digital media workflows, and it usually manifests as files that are either unnecessarily large or visually degraded in ways that the right format would have avoided entirely.

This article explains how the three most important web image formats — PNG, JPEG, and WebP — work internally, what each one is optimized for, and what happens at the byte level when you convert between them. For a broader comparison that also covers AVIF and JPEG XL, see our article on WebP vs PNG vs JPEG. This article goes deeper into the conversion process itself and the practical decisions around it.

How PNG Works

PNG (Portable Network Graphics) is a lossless format, meaning the decompressed image is bit-for-bit identical to the original. No pixel data is ever discarded. PNG achieves compression through a two-stage pipeline: filtering and DEFLATE.

In the filtering stage, each row of pixels is processed through one of five prediction filters. Instead of storing raw pixel values, the filter stores the difference between each pixel and a predicted value derived from neighboring pixels. For images with smooth gradients or repeating patterns, these differences are mostly small numbers clustered around zero, which compress much more efficiently than the raw values would.

The five filter types are None (store raw values), Sub (difference from the pixel to the left), Up (difference from the pixel above), Average (difference from the mean of the left and above pixels), and Paeth (a more complex predictor that chooses the nearest of left, above, or upper-left). PNG encoders can choose a different filter for each row, and good encoders test multiple strategies to find the combination that produces the smallest output.

After filtering, the data is compressed with DEFLATE — the same lossless algorithm used in ZIP files and gzip. DEFLATE combines LZ77 dictionary-based compression (finding and referencing repeated byte sequences) with Huffman coding (using shorter bit patterns for more frequent values). The result is typically a 2:1 to 5:1 compression ratio for photographic content and much better ratios for graphics with large areas of solid color.

PNG supports 8-bit and 16-bit color depth per channel, full alpha transparency (256 levels of opacity per pixel), and an indexed color mode with up to 256 palette colors. The alpha channel support is what makes PNG essential for logos, icons, UI elements, and any graphic that needs to be composited over a background.

The tradeoff is file size. Because PNG is lossless, photographic images compressed as PNG are typically 5 to 10 times larger than the same image compressed as JPEG at a visually equivalent quality. For photographs where transparency is not needed, PNG is almost never the right choice for web delivery.

How JPEG Works

JPEG uses lossy compression through a pipeline of color space conversion, chroma subsampling, block-based Discrete Cosine Transform, quantization, and Huffman entropy coding. Our companion article on image compression explains this pipeline in detail, so here we focus on the characteristics that matter for format conversion decisions.

JPEG does not support transparency. There is no alpha channel. If you convert a PNG with transparency to JPEG, the transparent areas must be filled with a solid color — typically white or black, depending on the tool. This is a one-way data loss: converting back to PNG does not restore the transparency.

JPEG does not support lossless mode in its standard implementation (there is a JPEG Lossless specification, but it has essentially zero browser and tooling support). Every JPEG file has been through the quantization step, which means some detail has been permanently discarded. Converting a JPEG to PNG does not restore this detail — it simply wraps the already-lossy pixel data in a lossless container, producing a file that is larger than the JPEG but no better in quality.

JPEG excels at photographs and images with smooth, continuous-tone gradients. Its DCT-based compression is specifically tuned for this type of content. It struggles with sharp edges, text, geometric patterns, and flat color areas — these produce visible ringing artifacts and block boundary discontinuities that worsen at lower quality settings.

How WebP Works

WebP, developed by Google and based on the VP8 video codec's intraframe compression, offers both lossy and lossless modes in a single format. Lossy WebP achieves 25 to 35 percent smaller files than JPEG at equivalent perceptual quality. Lossless WebP achieves roughly 26 percent smaller files than PNG according to Google's compression study.

WebP supports alpha transparency in both lossy and lossless modes. This is a significant advantage over JPEG — you can have a photograph with transparency at a fraction of the file size that PNG would require. WebP also supports animation, making it a potential replacement for GIF with dramatically better compression.

The lossy mode uses block prediction, adaptive block sizing (4 by 4, 8 by 8, or 16 by 16 based on content), arithmetic entropy coding (more efficient than JPEG's Huffman coding), and a deblocking filter that reduces the block boundary artifacts that plague JPEG at low quality settings. The result is visually cleaner output at the same file size, or the same visual quality at a smaller file size.

The limitation of WebP is encoding support. All modern browsers can display WebP images, but Safari on iOS and macOS cannot create WebP through the Canvas API's toBlob and toDataURL methods. This means browser-based tools that use Canvas for format conversion cannot produce WebP output on Safari. Our image format converter detects this limitation at runtime and disables WebP as an output option when it is not supported by the current browser.

What Happens During Format Conversion

Converting between image formats is not a simple container swap — it involves decoding the source format to raw pixel data and then re-encoding that pixel data in the target format. Understanding this process reveals why certain conversions are lossless and others are not.

PNG to JPEG decodes the PNG to an uncompressed RGBA bitmap, discards the alpha channel (filling transparent areas with a background color), converts from RGB to YCbCr, applies chroma subsampling, runs the DCT and quantization pipeline, and encodes with Huffman coding. This conversion is lossy: detail is permanently discarded, and transparency is lost. The file will be much smaller.

JPEG to PNG decodes the JPEG to an uncompressed RGB bitmap (reversing the DCT, dequantizing, converting from YCbCr back to RGB), then encodes that bitmap with PNG's filter-and-DEFLATE pipeline. This conversion is technically lossless — no additional detail is lost beyond what JPEG already discarded — but the output file is much larger. The only reason to do this is when you need to edit the image in a tool that does not support JPEG, or when you need to add transparency to a photograph (the alpha channel will be added by the PNG encoder, but the original JPEG pixels remain lossy).

PNG to WebP (lossless) decodes the PNG and re-encodes with WebP's lossless codec. The pixel data is identical, but the file is roughly 26 percent smaller because WebP's lossless compression is more efficient than PNG's DEFLATE. Transparency is preserved. This is a free file size win with zero quality tradeoff.

PNG to WebP (lossy) decodes the PNG and re-encodes with WebP's lossy codec. Detail is discarded similarly to JPEG but with better compression efficiency. Transparency can be preserved (WebP lossy supports alpha). The file will be dramatically smaller than the PNG.

JPEG to WebP (lossy) decodes the JPEG and re-encodes with WebP's lossy codec. This is a double-lossy conversion — the image was already degraded by JPEG compression, and WebP applies a second round of lossy encoding. The additional degradation is usually minimal if the WebP quality setting is set to match or exceed the original JPEG quality, but it exists. For archival purposes, avoid round-tripping through multiple lossy formats.

WebP to PNG decodes the WebP and re-encodes as PNG. If the WebP was lossless, the result is identical pixels in a larger file. If the WebP was lossy, the result is the lossy pixels wrapped in a lossless container — larger file, no quality improvement.

WebP to JPEG decodes the WebP and re-encodes as JPEG. Alpha transparency is lost. If the WebP was lossy, this is another double-lossy conversion. The primary use case is compatibility — sending an image to a system or person that does not support WebP.

Runtime Format Detection in the Browser

The Canvas API's toDataURL method accepts a MIME type parameter, but browsers are not required to support every type. The specification mandates support for image/png and recommends image/jpeg. Support for image/webp varies: Chrome, Firefox, and Edge support it; Safari does not.

The reliable way to detect support at runtime is to create a small test canvas, call toDataURL("image/webp"), and check whether the returned string starts with data:image/webp. If the browser does not support WebP encoding, it silently falls back to PNG, and the returned string starts with data:image/png instead. This one-time detection takes microseconds and allows the tool to show or hide the WebP option accurately.

No current browser supports image/avif through toDataURL or toBlob. AVIF encoding is computationally expensive and would require a WebAssembly-based encoder to implement in the browser — something that is technically possible but adds significant page weight and processing time. For this reason, browser-based format converters typically offer PNG, JPEG, and conditionally WebP, while AVIF conversion is handled by server-side tools or desktop applications.

Choosing the Right Format

The decision tree for format selection is straightforward once you understand each format's strengths.

If the image needs transparency, use WebP (lossy or lossless) where browser support allows, or PNG as a universal fallback. JPEG cannot represent transparency at all.

If the image is a photograph or continuous-tone image without transparency, use WebP lossy for the smallest file size with good quality, JPEG as a compatibility fallback, or PNG only if you need pixel-perfect lossless preservation (accepting the much larger file size).

If the image contains text, sharp edges, screenshots, diagrams, or flat color areas, use PNG for lossless preservation or WebP lossless for a smaller lossless file. JPEG is a poor choice because its DCT quantization introduces visible artifacts around sharp edges. Our article on image compression explains why JPEG handles sharp content poorly.

If you are generating images for a specific platform or system with format requirements, match those requirements. Some CMSes only accept JPEG and PNG. Some CDNs automatically convert to WebP or AVIF for supported browsers. Some email clients strip WebP attachments. Know your target environment.

For web delivery, the modern best practice is to provide WebP as the primary format with JPEG or PNG fallbacks using the HTML picture element. The browser automatically selects the best supported format, and you get the file size benefits of WebP for the roughly 96 percent of global browser traffic that supports it, with graceful degradation for the remainder.

Quality Settings During Conversion

When converting to a lossy format (JPEG or WebP lossy), the quality setting determines how much detail is discarded. The Canvas API's toBlob and toDataURL methods accept a quality parameter between 0 and 1. A value of 0.8 (quality 80) is the sweet spot for most photographic content — visually indistinguishable from the original at normal viewing distances, with a 10:1 to 15:1 compression ratio.

When converting to a lossless format (PNG or WebP lossless), there is no quality setting because no data is discarded. The compression level affects encoding speed and file size but not image quality. PNG tools may offer compression level settings (1 through 9), but these control how hard the encoder works to find optimal DEFLATE parameters — higher levels produce slightly smaller files at the cost of longer encoding times. The decoded image is identical regardless of the compression level.

Our image format converter exposes the quality slider for lossy formats and hides it for lossless formats, keeping the interface clean and the options relevant. Combined with the image compressor for fine-tuning the quality-size tradeoff, and the image resizer for ensuring correct dimensions before conversion, you have a complete client-side image optimization pipeline.

The Bottom Line

Image format conversion is a decoding-and-re-encoding process with predictable tradeoffs. PNG preserves everything but produces large files. JPEG compresses photographs aggressively but discards transparency and introduces artifacts on sharp content. WebP offers the best of both worlds — smaller files than either PNG or JPEG with support for transparency and both lossy and lossless modes — but lacks encoding support in Safari's Canvas API. Understanding what each format preserves and discards lets you choose the right format for every image and avoid the common mistake of converting blindly and either bloating file sizes or degrading quality unnecessarily. Our image format converter handles the conversion entirely in your browser, with runtime format detection and no server upload required.

References

MDN — Image File Type and Format Guide — Mozilla's comprehensive reference for every web image format, including supported features, browser compatibility, and MIME types.

Google — WebP Compression Study — Google's own comparison of WebP vs. JPEG compression efficiency at equivalent perceptual quality levels.

libpng — PNG Compression and Filtering — Deep technical reference for PNG's filter prediction and DEFLATE compression pipeline.

MDN — HTMLCanvasElement.toDataURL() — Documentation for the Canvas API's format and quality parameters, including browser support details.

Can I Use — Canvas toBlob WebP — Browser support table for WebP encoding through the Canvas API.