Skip to content

Cannot embed non-transparent image onto a transparent background #366

@diegocsandrim

Description

@diegocsandrim

Hi!

There is a strange behavior when resizing JPEG to PNG in embed mode.

sharp('input.jpg')
  .resize(2000, 1000)
  .background({ r: 0, g: 0, b: 0, a: 0 })
  .embed()
  .toFormat(sharp.format.png)
  .toFile('output1.png', function (err, info) {
    console.log(err || info);
  });

I am expecting to have the input image resized to maximum width or height, keeping aspect ratio, and above a transparent background.
But I am getting a full transparent image!

JPEG input
JPEG input
Rezize output
Resize output

The same does not happens when input image is a PNG, with the same code:

sharp('input.png')
  .resize(2000, 1000)
  .background({ r: 0, g: 0, b: 0, a: 0 })
  .embed()
  .toFormat(sharp.format.png)
  .toFile('output2.png', function (err, info) {
    console.log(err || info);
  });

PNG input
JPEG input
Rezize output
Resize output

This is really strange to me, am I making a mistake?

A add a git repo to test it:
https://github.com/diegocsandrim/sharp-test

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions