Image background color not applying when converting transparent PNG to JPG

45 replies
Last updated: Dec 1, 2025
Hey all, thanks for the awesome platform! Love using it in a few of my projects. I have a question about images - if I convert a transparent png to a jpg on the fly, does the bg param not apply to the flattened image? Can’t seem to get it to work (though sure I saw this working in development a few weeks back).. it always sets a black background. Thx
AI Update

Great to hear you're enjoying Sanity! I can help with your image transformation question about the bg parameter.

Yes, the bg parameter should definitely work when converting transparent PNGs to JPG format. According to the Sanity documentation, the bg parameter is designed to "fill in any transparent areas in the image with a color" and accepts hexadecimal color values (RGB, ARGB, RRGGBB, or AARRGGBB).

The black background you're seeing is actually the default behavior when converting transparent images to JPG without specifying a background color. Here are some things to check:

1. Verify your parameter format

Make sure your hex color doesn't include the # symbol - just use the hex value directly:

  • āœ… Correct: ?fm=jpg&bg=ffffff (for white)
  • āœ… Correct: ?fm=jpg&bg=ff0000 (for red)
  • āŒ Wrong: ?fm=jpg&bg=#ffffff

2. Example URL

Here's what a working URL should look like:

https://cdn.sanity.io/images/[project]/[dataset]/[asset].png?fm=jpg&bg=ffffff

This will convert your PNG to JPG and fill transparent areas with white instead of black.

3. Using the image-url helper

If you're using the @sanity/image-url helper library, you can chain methods like this:

imageUrlBuilder(client)
  .image(yourImage)
  .format('jpg')
  .bg('ffffff')
  .url()

4. Check for caching

Since the Sanity CDN caches transformed images, you might be seeing a cached version with the old black background. Try adding a cache-busting parameter or wait a bit for the cache to clear. You could also test with a slightly different size parameter to force a new transformation.

The bg parameter works independently and doesn't require any specific fit mode or other parameters - it simply fills transparent areas with your specified color whenever the image format doesn't support transparency (like JPG).

If you're still seeing black backgrounds after trying these suggestions, double-check that you're actually hitting the new URL with the bg parameter included. Hope this helps!

Show original thread
45 replies

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?