Lunchbox Hands

images

Why a Transparent PNG Turns Black When You Convert It to JPG

JPEG has no alpha channel, and browsers put transparent areas on black when they export one. Measured results from sips, Pillow and sharp, and how to get white.

By ·

You had a logo on a transparent background. You converted it to JPG so a form or a CMS would accept it, and the background came back solid black. Sometimes it comes back white instead, and a different tool on the same file gives a different answer. JPEG cannot store transparency at all, so every converter has to decide what colour goes behind your image, and the ones that don’t decide on purpose end up with black. Which colour you get depends on whether the tool composites onto a background, which background it picks, or whether it just throws the transparency away.

JPEG has nowhere to put transparency

A PNG pixel can carry four values: red, green, blue and alpha. Alpha is opacity, where 0 is fully transparent and the maximum is fully opaque. In CSS hex the alpha is the last pair of an 8-digit color, as in #ff000080 for 50% red, and Android writes the same pair first. JPEG has no fourth slot. The JFIF specification that ordinary .jpg files follow allows “one or three components”: greyscale, or YCbCr colour. A converter going from PNG to JPG has to turn every partly or fully transparent pixel into a solid colour.

The detail that explains the black is what a transparent pixel actually contains. Alpha 0 means “don’t show this”, but the pixel still has red, green and blue values. The PNG specification even names the usual case: “fully transparent black”, a pixel “where the red, green, blue and alpha components are all equal to zero.” Many editors and exporters write transparent areas as (0, 0, 0, 0). So a converter that simply drops the alpha channel shows whatever colour was hiding underneath, and that colour is almost always black.

Browsers composite onto black, by specification

Most converters that run in a web page, ours included, work the same way: decode the image, draw it on a <canvas>, and export with canvas.toBlob(callback, 'image/jpeg', quality) or toDataURL('image/jpeg'). MDN’s toBlob page and toDataURL page document the type argument and the PNG fallback for unsupported types. Neither mentions transparency. The rule is in the HTML standard’s section on serialising the bitmap as a file:

For image types that do not support an alpha component, the serialized image must be the bitmap image composited onto an opaque black background using the source-over compositing operator.

A new canvas starts out fully transparent. Draw a logo with a transparent background on it, ask for JPEG, and the browser is required to put black behind it. Any browser-based PNG-to-JPG tool that exports this way gives you a black background unless it paints its own background first.

Ours did exactly this until this week. The shared helper behind our image format converter, image compressor, bulk processor and image pipeline created a canvas, called drawImage, and exported. Nothing was ever drawn behind the image. A transparent logo converted to JPEG came out on black, and nothing in our tests caught it, because the code ran without error and the image did convert. We found it while researching this post.

The fix is one shared function. Before drawing, it checks the output type. If the format has no alpha, which today means JPEG, it fills the canvas with a solid colour and then draws the image on top:

export function paintImage(ctx, img, width, height, format, background = '#ffffff') {
  if (formatNeedsBackground(format)) {   // true for image/jpeg
    ctx.fillStyle = background
    ctx.fillRect(0, 0, width, height)
  }
  ctx.drawImage(img, 0, 0, width, height)
}

PNG and WebP output skip the fill, so their transparency survives. The format converter now shows a background colour picker when JPEG is the target. It defaults to white, and you can choose any other colour. The compressor, crop, watermark, meme and redaction tools use the same helper with a white fill, so they can’t drift apart. Our PSD-to-PNG, social image resizer and Instagram grid splitter already filled white before JPEG export, which is why the bug went unnoticed in those.

What other tools actually do, measured

The fill-versus-drop distinction changes the result, so we tested it with two 64 × 64 PNGs. Both have the same visible content: an opaque blue square, a tile of 50%-opaque red (255, 0, 0, 128) to stand in for an anti-aliased edge, and a tile of 25%-opaque black (0, 0, 0, 64) to stand in for a drop shadow. They differ only in what hides under the fully transparent areas. In the first file it’s the usual (0, 0, 0, 0). In the second it’s (255, 0, 255, 0), invisible magenta. Then we converted both with each tool on this Mac (macOS 27.0) and read back the pixels from the JPEGs:

Tool and commandTransparent area, file 1 (hidden black)Transparent area, file 2 (hidden magenta)50% red edge25% shadow
sips -s format jpeg (sips-316, macOS 27.0)(255, 255, 255)(255, 255, 255)(255, 127, 126)(191, 191, 191)
Pillow 13.0.0 im.save('x.jpg') on an RGBA imageerror: cannot write mode RGBA as JPEGsame error––
Pillow im.convert('RGB')(0, 0, 0)(255, 0, 254)(254, 0, 0)(0, 0, 0)
Pillow, paste onto a white image using alpha as the mask(255, 255, 255)(255, 255, 255)(255, 127, 126)(191, 191, 191)
sharp 0.35.5 (libvips 8.18.7) .jpeg()(0, 0, 0)(0, 0, 0)(128, 0, 0)(0, 0, 0)
sharp .flatten({ background: '#ffffff' }).jpeg()(255, 255, 255)(255, 255, 255)(255, 127, 126)(191, 191, 191)

JPEG compression shifts values by a point here and there. Pillow and sharp were set to quality 95 with no chroma subsampling, and sips used its defaults. ImageMagick isn’t installed on this machine, so it isn’t in the table.

The table shows three different behaviours:

  • Composite onto a chosen colour. sips, Pillow’s paste and sharp’s flatten all blend each pixel with white according to its alpha. The hidden colour doesn’t matter, because a pixel at alpha 0 contributes nothing.
  • Composite onto black. sharp’s default does the same blend, but onto black. Its flatten documentation says the background “defaults to black”. That’s also what the HTML spec requires of browsers. The hidden colour still doesn’t matter, but every transparent area turns black.
  • Drop the alpha. Pillow’s convert('RGB') discards the alpha channel and keeps the stored colour. On a typical file that’s black. On the magenta file, the invisible magenta suddenly shows up. A converter that “randomly” produces pink or green backgrounds is probably doing this, and the file had that colour hidden under its transparency all along.

Desktop apps vary in the same way, and the only way to know is to test the one you use. The one Apple tool we measured, sips on macOS 27, filled white on both files.

If you’re writing the conversion yourself, this is the line that matters:

from PIL import Image
im = Image.open('logo.png').convert('RGBA')
bg = Image.new('RGB', im.size, (255, 255, 255))
bg.paste(im, mask=im.getchannel('A'))
bg.save('logo.jpg', quality=90)
await sharp('logo.png').flatten({ background: '#ffffff' }).jpeg({ quality: 90 }).toFile('logo.jpg')

Soft edges and shadows: why black ruins more than the background

A solid black background is easy to see. The subtler damage is at the edges. Logos and cut-outs are anti-aliased, so their outlines are rows of partly transparent pixels, and drop shadows are entirely partly transparent pixels. Flattening blends each one with the background:

result = alpha × colour + (1 − alpha) × background

Take the 50% red edge pixel, (255, 0, 0) at alpha 128/255 ≈ 0.502:

BackgroundResultWhat you see
black (0, 0, 0)(128, 0, 0)dark maroon fringe
white (255, 255, 255)(255, 127, 127)light pink, blends into white
dark slate (17, 24, 39)(136, 12, 19)dark red, blends into the slate

The measured outputs above match: sharp’s default produced (128, 0, 0), and the white-filling tools produced (255, 127, 126).

Now put that JPEG on a white web page. The background might be fixed by cropping or recolouring, but every edge pixel has already been blended with black. The logo keeps a dark outline, a white logo’s edges go grey (a 50% white pixel on black becomes (128, 128, 128)), and soft shadows behave worst of all. The 25% shadow pixel flattened onto black is (0, 0, 0), exactly the same colour as the background, so the shadow disappears into it. Fixing the background later can’t bring the shadow back.

This is also why PNG stores colour and alpha separately. The PNG spec notes that premultiplied storage is “in effect … already composited against a black background”, and PNG avoids it so that “an image editor can take a PNG image and easily change its transparency.” Converting to JPEG does that compositing permanently.

Pick the background the image will actually sit on

White is a sensible default because most documents, forms and product listings are white. It’s only right if that’s where the image ends up. Flatten onto the colour of the destination:

  • A logo for a dark-themed site or a dark email header should be flattened onto that exact hex value. Otherwise its edges will be pale against the dark page.
  • A product cut-out for a marketplace that requires a white background: flatten onto white.
  • A sticker or icon that has to work on several backgrounds can’t be a JPEG. One flattened file can only match one background.

In our image format converter, choose JPEG and a background colour field appears below the format buttons. Type the destination’s hex value, or pick one of the presets.

When not to convert to JPG at all

If the transparency is part of the image, keep a format that stores it:

  • PNG is lossless and keeps full alpha. It’s the right archive copy for logos and anything you’ll re-export.
  • WebP keeps transparency too. Google’s documentation says “WebP supports transparency in both lossless and lossy modes”, and files are usually much smaller than PNG.
  • AVIF stores alpha as a separate auxiliary image (the AVIF spec defines an “AV1 Alpha Image Item” for it). Our converter doesn’t write AVIF. PNG vs JPG vs WebP vs AVIF covers when its extra compression is worth the slower encode.

Convert to JPG when the destination requires it, or when the image is a photograph with no transparency. A logo on a website should almost always stay PNG, WebP or SVG. If file size is the problem, compress it as WebP and keep the alpha instead of flattening it.

The reverse mistake: a JPG renamed .png is still opaque

The same confusion runs the other way. Someone sends a “transparent PNG” that shows a white or checkerboard background in every viewer. Usually it’s a JPEG that was renamed, or a PNG exported from a JPEG. Renaming changes nothing inside the file, and converting a JPEG to PNG copies its opaque pixels. The white behind the logo is real pixel data, and the checkerboard pattern, if there is one, was saved into the image itself.

Two quick checks: the file type checker reads the file’s signature bytes and tells you whether a .png is really a PNG, and a real PNG with transparency shows the background of whatever page or viewer it’s placed on. If it’s a JPEG underneath, the transparency has to be recreated, either by masking in an editor or, for logos, by tracing it to SVG. If an SVG is your source and the export went blank instead of black, that’s a different set of causes.

Quick fix

  1. Keep the original transparent PNG. Every JPEG you make is a one-way copy.
  2. Convert with a tool that lets you set the background, and set it to the colour of the destination page or document.
  3. Check the edges at 100% zoom on that destination. A dark outline means the edges were blended with black.
  4. If the image has to work on more than one background, don’t use JPEG. Use PNG or WebP.

Our image format converter does steps 2 and 4 in your browser without uploading anything.