New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Though no pixel has transparency after -flatten, an alpha channel is added and saved in PNG #6985
Comments
Add -alpha off after -flatten.
No alpha channel
On Imagemagick 7, use magick, not magick convert. |
Thank you for your quick response. I still wonder:
|
|
From the documentation for -alpha remove at https://imagemagick.org/Usage/masking/#alpha_remove
|
This is true when that IM does have an annoying habit of adding a redundant opaque alpha channel, in some circumstances. And many operations by default include the alpha channel, which can give unexpected results. When IM processes images, I would like it to not add an alpha channel if it is fully opaque. However, the rules for when this might occur are complex. A simple solution would be to add a check for opacity after every operation. But that would be expensive; it might need all pixels to be examined. When IM writes images, I would like it to remove the alpha channel if it is fully opaque, by default. But this would be a new feature, and might break scripts. Another possibility: add a new option to IM: `-alpha off-if-opaque'. If there is no alpha channel, or it has any transparency, this does nothing. If it has a fully opaque alpha channel, it is turned off. |
Ensure that certain algorithms, in accordance with the SVG standard, necessitate the presence of an alpha channel for tasks like compositing. If the alpha channel is absent, we automatically incorporate it. The decision of whether to retain it or not is left to the user as part of their workflow. If opting to exclude it, append |
I am sorry for my quite lengthy reply but I do like to think about each detail I am given. @fmw42 (how can I quote on GutHub keeping the reference to the original author 🤔)
Exactly, but no background specified does yield a different result than specifying white.
I wasn't aware of that. So, in addition to potentially
Whenever the number of colors is small enough so a palette can be used when saving, for example. I can see that this is probably supposed to be a pure storage optimization but nevertheless it is removing the alpha channel. It may be quite unexpected that a composed image sometimes has no alpha channel after saving and sometimes it will, because the number of colors of a composed image may not be easily predictable. An image that happens to have 250 colors will not have an alpha channel stored, an image that happens to have 260 colors will have.
By "unused", I mean an alpha channel that was added for technical reasons within an operation but has all its values set to 100% after the operation.
As you pointed out,
I agree and in my opinion it is very bothersome for a human having to remember all the complexity and nasty details around a functional behavior. I always thought the paramount purpose of a software was to make life easier, not the opposite. 😔
I tend to disagree for two reasons. 1) You don't need to add a check after every operation, because transparency does not come out of no where. Each operation already knows (or at least should know) if it can introduce or eliminate transparency. For instance, resizing will not affect the presence of transparency, extending with a transparent background will always introduce transparency, and in my case, flattening with a solid color will always eliminate transparency. 2) It would only be necessary when saving and, as it seems, IM already does such a check when storing an image (only it is incomplete so it isn't effective in my case).
I tend to disagree here, too. Categorical adherence to a once implemented behavior facilitates programming-by-exception and functional singularities. It would be better to enhance existing behavior with an option to go back to the previous behavior, for instance by specifying a compatibility level in the command line of IM or an environment variable. If a user of IM insists on a behavior to not change at all, he or she could always add this switch already the moment he or she creates the script. This is for instance the way it is implemented in some DBMS. Also, I feel like this breaking-change rule is treated arbitrarily. For instance, some time in the past, IM added palette optimization to ICO files, turning truecolor images into 256-color images, which is a total different thing in terms of ICO files, absolutely introducing a breaking change. Additionally, the whole argument about breaking changes ignores the fixing of actual bugs, which may inevitably change the behavior of an operation.
This is what I had in mind, too.
In for a penny, in for a pound. If a car switches on the headlights automatically for some reason or another (may it be for legal ones or just because the ambient light turned low, I don't care), I would expect it to switch them off again as soon as the necessity is gone.
IM makes a decision on the user's behalf when it is asked to rotate an image only if it is landscape. And IM makes a decision on the user's behalf to So, in essence I would still like to understand:
I would be deeply grateful if a developer could elaborate on that. Also, is there any chance we could get an |
On sRGBA(10%,20%,1), see https://imagemagick.org/script/command-line-options.php#alpha:
When the background colour has an alpha channel, such as sRGBA(10%,20%,1) the result will have an alpha channel. To me, that seems like reasonable behaviour.
Most operations can introduce or eliminate transparency. It would be cool if each operation knew whether it had actually introduced or eliminated transparency. But that would be complex and bug-prone and (worse) create a maintenance headache.
Not strictly true. If the input has transparency, resizing it may remove all the transparency. I don't think resizing can ever add transparency to an opaque image, but I could be wrong. Yes, I am being picky here, because the internal code of IM needs to be picky.
I agree. In principle, the script could save the image, then examine whether the result has an alpha channel that is entirely opaque. If it does, it reads the file, EDIT: I realise this isn't @ BlackXStar42's ideal solution, which I summarise as: "If IM automatically adds an alpha channel which turns out to be fully opaque, IM should automatically remove the alpha channel." That would be easier for users of IM, but greatly more difficult to implement. |
We added support for the |
Thank you. |
I do not see this new define listed in the change log. It would be nice to have it there so one knows when it was released. Please add any new features to the change log in the future. Or is this only in the beta for the next release? |
From the name This is fine with me. I prefer it to act like |
The change log is only updated during the release process. For beta releases, you need to check the git main commits for any changes, see this commit for example. @snibgo , we had an AI pick the name for us. We said we need an option name that removes the alpha channel only if its opaque. It suggested |
Does it not add computations to check if every pixel is opaque? If so, I don't think this is a good idea and would leave it to the user to use -alpha off. What is the difference in using the -alpha remove-opaque vs -alpha off? The user needs to type extra in either case. Please correct me if I am wrong. |
|
Thanks for the correction. But still does not not take extra computations to check if every pixel is opaque or does that add negligibly to the processing? |
Yes, it has to loop through pixels, to check for any transparency. |
The AI doesn't know the difference between |
We're confused, aren't we conditionally removing the alpha channel if its opaque. Give it some thought and provide the final option name and we'll push a patch. |
In my opinion -alpha remove-opaque is not a good name. One is not removing opacity. One is removing alpha. -alpha off-if-opaque sounds more correct. Or -alpha remove-if-opaque if that is more proper. Other possibilities would be to use a short equivalent name to remove such as -alpha discard or -alpha detach or -alpha eject -alpha eliminate or -alpha purge though these do not actually say what is happening. So prehaps -alpha off-if-opaque or -alpha remove-if-opaque is best depending upon whether the operation is off or remove. |
|
ImageMagick version
7.1.1-24
Operating system
Windows
Operating system, version and so on
7, 6.1.7601
Description
I am using
-flatten
to composite a partially transparent image with the background color. I expect the image to contain no transparency anymore afterwards. Though there actually is no pixel left anymore which has any transparency, the operation still adds an alpha channel, which is even stored in a PNG afterwards. I am confused as this is a counter-intuitive behavior. I am also confused as I have indeed observed IM to optimize the colors when saving an image as a PNG, potentially removing the alpha channel if no transparency is used. Sometimes it doesn't seem to do that and I don't know why. So how exactly does the optimization work, when does it remove an alpha channel and when does it not? Also, I am wondering if-flatten
adding an unused alpha channel is the intended behavior. If so, I would like to know why so and which option I could use to ask IM to remove an unused alpha channel. I know I could use-alpha Remove
but this will always remove the alpha channel and I would rather like to ask IM if it can be removed.Another strange behavior I observed is that even when I use
-alpha Remove
after-flatten
, I first have to set the background color to a solid color though-flatten
just used a solid background color. Yet another strange behavior I observed is that setting the background color just before-alpha Remove
only works when I use a color name or rgb(...) but not if I use rgba(..., 1.0).Steps to Reproduce
Images
The text was updated successfully, but these errors were encountered: