Why a PNG turns black when it becomes a JPG
You converted a logo and got a black square with the logo somewhere inside it. Here is the line of code responsible.
What actually happened
A JPEG has three channels and every pixel in it is opaque. A PNG with transparency has four, and the fourth one says how much of each pixel is there. When a converter opens the PNG it gets four channels. When it writes the JPEG it must produce three. The fourth channel has to be resolved against something, and that something is the matte.
A great deal of image code allocates a buffer, does not fill it, and composites the source over it. An unfilled buffer is zeroes. Zero in all three channels is black. Nobody chose black; black is what memory looks like when nothing has been written to it. Other libraries do choose it, on the grounds that a defined default beats an undefined one, which is true and is still black.
The result is that the transparent region — which was nothing — becomes the most visually dominant part of the image. If your logo was ninety percent empty frame, the JPG is ninety percent black.
How to tell this from the other failure
Two different faults look similar at a glance and have nothing else in common.
- A solid black field where the empty parts of the frame were: the matte. Fixed by choosing a different one.
- A thin dark rim around the shape, with the empty parts correct: the fringe. The artwork carries dark colour in its part-covered pixels and the matte is not involved. Fixed by the edge repair.
- Black over the whole image including the artwork: the decode failed, not the composite. That is a broken or unusual PNG, and the tool should tell you what it read rather than hand you a black rectangle.
The fix, in four moves
- 1Open the original PNG again. The black JPG is not a starting point; nothing in it can be undone.
- 2Choose the colour the picture is going to sit on. White for a document, the page colour for a web page, and a sampled colour if the artwork came out of something dark.
- 3Look at the preview before you export. This is the whole point of making the decision visible.
- 4Keep the PNG somewhere if the transparency might be needed again. The JPG is a copy for one destination, not a replacement.
Why converters do not ask
Asking costs a screen. A converter that takes a file and returns a file has one job and measures itself on how few steps that takes, so the matte is a constant somewhere in its source and has been for years. It is a reasonable trade for the nine files in ten with no alpha channel in them. It is a bad one for the tenth, and the tenth is why anyone searches for this.
Three questions that follow
- Is the file corrupted?
- No. The JPG is a correct file holding exactly the pixels it was given. The pixels were wrong before the encoder ever saw them.
- Can I get the original back from the black JPG?
- Not from the JPG. Every transparent pixel is now an opaque black pixel and nothing in the file records that it used to be anything else. Go back to the PNG, which is still on your disk, and convert it again.
- Why does it happen more with logos than photographs?
- A photograph usually has no alpha channel at all, so there is nothing for a matte to fill. A logo is mostly alpha. The same code path runs on both and only one of them has anything to show for it.