Fill transparent areas with white in JPEG output (closes #362)
check / check (push) In progress

JPEG has no transparency, and libvips filled transparent areas with
black, so a logo on a transparent background came back on a black
square. The README now says JPEG output is filled with white.

Model: opus-5-5
This commit was merged in pull request #373.
This commit is contained in:
sneak committed 2026-11-19 06:30:17 +00:00
1 parent 3ad266309c
commit c9040dee20
2 files changed
+17 -2

No files matched your search

+4
View File
@@ -549,6 +549,10 @@ example.
PNG or JPEG XL source. This holds in every output format, `orig` included. So an PNG or JPEG XL source. This holds in every output format, `orig` included. So an
animated GIF requested as GIF comes back as a still GIF. animated GIF requested as GIF comes back as a still GIF.
**Transparency.** JPEG has no transparency, so pixa fills the transparent areas
of an image with white in JPEG output. This applies to `auto` too, when it
chooses JPEG.
**SVG sources.** `orig` of an SVG source is the SVG itself, unchanged, and is **SVG sources.** `orig` of an SVG source is the SVG itself, unchanged, and is
cached like any other image. It is given only at the `orig` size, since pixa cached like any other image. It is given only at the `orig` size, since pixa
does not resize an SVG as SVG. `orig` of an SVG at any other size is refused does not resize an SVG as SVG. `orig` of an SVG at any other size is refused
+13 -2
View File
@@ -808,9 +808,20 @@ func (p *ImageProcessor) encode(
// every setting whose zero value is not what pixa wants. Stripping metadata // every setting whose zero value is not what pixa wants. Stripping metadata
// drops EXIF, XMP, IPTC and the ICC profile. // drops EXIF, XMP, IPTC and the ICC profile.
// exportJPEG encodes img as JPEG at quality, without metadata. The settings // exportJPEG encodes img as JPEG at quality, without metadata. JPEG has no
// it leaves out are at libvips' defaults. // transparency, so any transparent areas are filled with white; left to
// itself, libvips would fill them with black. govips scales the 8-bit white
// for a 16-bit image. The settings it leaves out are at libvips' defaults.
func exportJPEG(img *vips.ImageRef, quality int) ([]byte, error) { func exportJPEG(img *vips.ImageRef, quality int) ([]byte, error) {
if img.HasAlpha() {
err := img.Flatten(&vips.Color{
R: math.MaxUint8, G: math.MaxUint8, B: math.MaxUint8,
})
if err != nil {
return nil, fmt.Errorf("failed to fill transparent areas: %w", err)
}
}
output, _, err := img.ExportJpeg(&vips.JpegExportParams{ output, _, err := img.ExportJpeg(&vips.JpegExportParams{
StripMetadata: true, StripMetadata: true,
Quality: quality, Quality: quality,