Skip to content

Change opacity/alpha #554

Closed
Closed

Description

@strarsis

It would be great if the opacity/alpha of a sharp image instance could be directly changed,
e.g. making it semitransparent, putting out as raw to buffer and overlaying another image with it.
(Also useful for visually debugging overlay compositions).

Activity

  1. changed the title [-]Change opacity[/-] [+]Change opacity/alpha[/+] on Aug 29, 2016
  2. lovell commented on Sep 2, 2016

    @lovell
    Owner

    Hello, a possible workaround for this might be to overlay with a raw pixel buffer containing tiled, semi-transparent pixel value, something like the following (untested):

    sharp(input)
      .overlayWith(
        new Buffer([0, 0, 0, 128]),
        { tile: true, raw: { width: 1, height: 1, channels: 4 } }
      )...
  3. strarsis commented on Sep 2, 2016

    @strarsis
    Author

    Thanks. Though an API method could be more intuitive.

  4. strarsis commented on Sep 14, 2016

    @strarsis
    Author
    sharp(input)
      .overlayWith(
        new Buffer([0, 0, 0, 128]),
        { tile: true, raw: { width: 1, height: 1, channels: 4 } }
      )

    The code works insofar that it adds an alpha channel -
    but the output image is now different compared to the input image, much darker.

    When I remove tile: true there will be still one pixel changed -
    but the output image should be identical to input image plus an alpha channel.

  5. lovell commented on Sep 14, 2016

    @lovell
    Owner

    Hello, [0, 0, 0, 128] was an example of adding a semi-transparent alpha channel. If you want it to be fully-transparent, you can use [0, 0, 0, 0].

    If you know the image dimensions then an alternative method of adding a fully-transparent alpha channel is by passing a zero-filled Buffer to joinChannel, for example (untested):

    sharp(input).joinChannel(Buffer.alloc(width * height), {
      raw: {
        width: width,
        height: height,
        channels: 1
      }
    })...
  6. quinton-ashley commented on Mar 7, 2018

    @quinton-ashley

    This is a whole lot of code for simply adding an alpha channel to the images.

    let img, meta;
    try {
    	img = sharp(file);
    	meta = await img.metadata();
    	img = await img.joinChannel(Buffer.alloc(meta.width * meta.height, 255), {
    		raw: {
    			width: meta.width,
    			height: meta.height,
    			channels: 1
    		}
    	});
    } catch (err) {
    	error(err);
    	return;
    }
    file = path.parse(file);
    await img.png().toFile(projDir + '/' + file.name + '.png');
    

    It would be more user friendly to just have a dedicated method since joinChannel is so cumbersome and overlaying four channels on the image seems like a very high performance cost for such a simple task.

    let img = await sharp(file).addAlpha();
    await img.png().toFile(...);
    
  7. lovell commented on Mar 8, 2018

    @lovell
    Owner

    Rather than dealing with raw data and Buffer allocation, something like the following might be a bit simpler (create was added in v0.17.3).

    .joinChannel({ create: { background: 'black', channels: 1, width, height } })

    EDIT: this won't work as the create feature only supports 3 and 4 channels.

  8. quinton-ashley commented on Mar 8, 2018

    @quinton-ashley

    Why even make it that verbose though? If someone is adding an alpha channel to an image it must be for the entire image. You can't selectively make one part of the image RGB format and another RGBA format.

  9. lovell commented on Mar 9, 2018

    @lovell
    Owner

    @quinton-ashley I've created #1153 for this suggestion; PRs always welcome.

  10. slavafomin commented on Nov 19, 2018

    @slavafomin

    Hello!

    I was also looking for a simple method to change the image transparency, but couldn't find one.

    Why is this issue closed and why is it classified as a question? I think it's a perfectly valid feature request. @lovell

    I would really like to see this in the library. Thank you for your hard work.

  11. lovell commented on Nov 19, 2018

    @lovell
    Owner

    @slavafomin This feature request is being tracked at #618

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions