Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Can't figure out how to properly discard the alpha channel #355

Answered by jcupitt
Nakilon asked this question in Q&A
Discussion options

I again appear to be dumber that I suppose when I deal with images and my laziness or lameness plays with me. So I still can't figure out the alpha channel thing.

I want to just properly convert 4 channels to 3 channels.

The issue is... see these two images:

If I do

image = image.has_alpha? ? image.flatten : image

one image becomes just black.

If I do:

image = (image.has_alpha? ? image.flatten.composite2(image.bandsplit[3], :screen) : image)

another one becomes just white.

I guess I totally misunderstand the thing.

UPD: Note: one image is downloaded from tg stickers, another one is a html5 base64 exported from p5js canvas after using the text and geometry primitives. I.e. none is made by me band by band so I suppose the images aren't "corrupted" in any way.

You must be logged in to vote

Hey @Nakilon,

Your first image has RGB all zero, and only has non zero values in the alpha channel. I find vipsdisp useful for looking at images like this: it will show you exactly what's in every pixel in the image:

By default, vips_flatten() will flatten against black (ie. 0), so the output is a black image on a black background :( If you flatten against 255 instead, you'll be able to see the alpha:

image = image.flatten(background: 255) if image.has_alpha?

That should work for your second image too.

Maybe the black default for flatten wasn't a great idea :(

Replies: 1 comment 2 replies

Comment options

Hey @Nakilon,

Your first image has RGB all zero, and only has non zero values in the alpha channel. I find vipsdisp useful for looking at images like this: it will show you exactly what's in every pixel in the image:

image

By default, vips_flatten() will flatten against black (ie. 0), so the output is a black image on a black background :( If you flatten against 255 instead, you'll be able to see the alpha:

image = image.flatten(background: 255) if image.has_alpha?

That should work for your second image too.

Maybe the black default for flatten wasn't a great idea :(

You must be logged in to vote
2 replies
Comment options

Woah, 4 channel eyedropper! Is it available on macOS though?

Thank you for the prompt response and making me see the problem.

I believe I saw the mention of the "default background" somewhere in https://en.wikipedia.org/wiki/Alpha_compositing when I was solving the issue with that image few weeks ago, can't find it now.
That is interesting because on canvas the default color is white I guess (while as you showed it stores the color as black at least in the export by document.getElementsByTagName('canvas')[0].toDataURL()). Also it's white in macOS preview. It's white in Telegram. But black in Skype (when you click/zoom it).

Comment options

You'd need to compile from source I guess :( Though I've not tried.

https://github.com/jcupitt/vipsdisp

Answer selected by Nakilon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /