将 PNG 图像中的透明度替换为白色背景
我有一个带有 Alpha 通道(即透明度)的 PNG 图像,我需要创建将图像层组合到白色背景上的版本。我想使用可编写脚本的命令,使用 CLI 工具(例如 Image Magick)将 PNG 直接无损地转换为 PNG。
导致错误的非工作 Image Magick 命令的示例是:
convert input.png -background white -flatten output.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
示例:
随意将
白色
替换为您想要的任何其他颜色。 Imagemagick 文档介绍了-alpha remove
操作:Example:
Feel free to replace
white
with any other color you want. Imagemagick documentation says this about the-alpha remove
operation:这对我有用:
文档参考:
-flatten
命令行选项-layers
命令行选项(-flatten
相当于-layers flatten
)This works for me:
Documentation references:
-flatten
command-line option-layers
command-line option (-flatten
is equivalent to-layers flatten
)在 ImageMagick 中拼合图像和应用背景图像很简单,
但是命令的顺序非常重要
要在透明图像上应用任何背景并拼合它,请先应用背景,然后拼合它。反过来则不行。
Flattening image and applying background image is straight forward in ImageMagick
However, order of the commands is very important
To apply any background on a transparent image and flatten it, first apply the background than flatten it. The reverse doesn't work.
唯一对我有用的是所有答案的混合:
The only one that worked for me was a mix of all the answers:
以下是如何将目录中所有文件夹中的相同图像替换为白色而不是透明:
mogrify -background White -flatten */*.png
here's how to replace the same image in all folders in a directory with white instead of transparent:
mogrify -background white -flatten */*.png
使用 -flatten 让我非常生气,因为 -flatten 与 mogrify 裁剪和调整大小相结合根本不起作用。官方的、对我来说唯一正确的方法是“删除”alpha 通道。
-alpha 删除 -alpha 关闭(JPG 不需要)
请参阅文档:http://www.imagemagick.org/Usage/屏蔽/#删除
Using -flatten made me completely mad because -flatten in combination with mogrify crop and resizing simply doesn't work. The official and for me only correct way is to "remove" the alpha channel.
-alpha remove -alpha off (not needed with JPG)
See documention: http://www.imagemagick.org/Usage/masking/#remove
Alpha 删除 部分">ImageMagick 使用指南 建议使用
-alpha remove
选项,例如:...使用您选择的
-background
颜色。该指南指出:
它还添加了注释:
因此,如果您不需要 alpha 通道,您可以通过添加
-alpha off
选项来缩小输出图像的大小,例如:还有关于其他常用的消除透明度的技术的更多详细信息在从图像中删除透明度部分。
该部分提到了使用
-flatten
作为消除透明度的技术的一个重要警告:因此,如果您一次转换多个图像,例如从 PDF 文件生成缩略图,
-flatten
将不会执行您想要的操作(它将所有页面的所有图像拼合为一张图像)。另一方面,使用 -alpha 删除技术仍然会生成多个图像,每个图像都删除了透明度。The Alpha Remove section of the ImageMagick Usage Guide suggests using the
-alpha remove
option, e.g.:...using the
-background
color of your choosing.The guide states:
It additionally adds the note:
Thus, if you do not need the alpha channel you can make your output image size smaller by adding the
-alpha off
option, e.g:There are more details on other, often-used techniques for removing transparency described in the Removing Transparency from Images section.
Included in that section is mention of an important caveat to the usage of
-flatten
as a technique for removing transparency:So, if you are converting several images at once, e.g. generating thumbnails from a PDF file,
-flatten
will not do what you want (it will flatten all images for all pages into one image). On the other hand, using the-alpha remove
technique will still produce multiple images, each one having transparency removed.您的命令似乎是正确的,因此问题可能是由于缺少对 PNG () 的支持。您可以使用
convert -list configure
检查或尝试以下操作:It appears that your command is correct so the problem might be due to missing support for PNG (). You can check with
convert -list configure
or just try the following:这并不完全是您问题的答案,但我在尝试找出如何删除 alpha 通道时发现了您的问题,所以我决定在这里添加这个答案:
如果您想使用 imagemagick 删除 alpha 通道,您可以使用这个命令:
This is not exactly the answer to your question, but I found your question while trying to figure out how to remove the alpha channel, so I decided to add this answer here:
If you want to remove alpha channel using imagemagick, you can use this command:
好吧,看起来我决定安装“graphics magick”而不是“image magick”有一些粗糙的边缘 - 当我重新安装真正的粗糙的旧“image magick”时,上面的命令运行得很好。
编辑,很久以后——有一天我会检查“graphics magick”是否解决了这个问题。
Welp it looks like my decision to install "graphics magick" over "image magick" has some rough edges - when I reinstall genuine crufty old "image magick", then the above command works perfectly well.
edit, a long time later — One of these days I'll check to see if "graphics magick" has fixed this issue.
我需要:
-alpha背景
和-flatten
,或-fill
。我制作了一个新的PNG,背景透明,中间有一个红点。
convert image.png -background green -alpha off green.png
失败:它生成了一个黑色背景的图像convert image.png -background green -alpha background -flatten green.png
生成了具有正确绿色背景的图像。当然,对于我重命名为
image.png
的另一个文件,它没有执行任何操作。对于该文件,我发现透明像素的颜色为“#d5d5d5”,因此我用绿色填充该颜色:convert image.png -fill green -opaque "#d5d5d5" green.png 将透明像素替换为正确的绿色。
I needed either: both
-alpha background
and-flatten
, or-fill
.I made a new PNG with a transparent background and a red dot in the middle.
convert image.png -background green -alpha off green.png
failed: it produced an image with black backgroundconvert image.png -background green -alpha background -flatten green.png
produced an image with the correct green background.Of course, with another file that I renamed
image.png
, it failed to do anything. For that file, I found that the color of the transparent pixels was "#d5d5d5" so I filled that color with green:convert image.png -fill green -opaque "#d5d5d5" green.png
replaced the transparent pixels with the correct green.我看到这个问题和答案对我确实有帮助,但后来我需要对很多文件执行此操作,所以如果您在一个文件夹中有多个图像(PNG 图像)并且您想对所有文件执行此操作:
I saw this question and answers which really help me but then I was needed to do it for a lot of files, So in case you have multiple images (PNG images) in one folder and you want to do it for all:
都试过了,没有一个有效。这个人做了:
Tried all, none worked. This one did:
这将创建一个图像,将第一个具有透明度的图像放在第二个图像的顶部,
最初在 这篇文章
this creates an image just placing the 1st with transparency on top of the 2nd
originally found the tip on this post
要实际从文件中删除 Alpha 通道,请使用 alpha off 选项:
To actually remove the alpha channel from the file, use the alpha off option:
这是 -alpha 关闭,而不是 -alpha 删除!当任何图标中存在 Alpha 通道时,iOS 应用商店上传失败!
操作方法如下:
mogrify -alpha 关闭 *.png
It's -alpha off, NOT -alpha remove! iOS app store upload fails when there is an alpha channel in any icon!!
Here's how to do it:
mogrify -alpha off *.png
这对我来说很有效:
这是一个具有透明背景的起始图像,以防它有助于测试:
< img src="https://i.sstatic.net/Ftndd.png" alt="具有透明背景的图像">
此外,对于 PC 上的一次性使用,您始终可以在 Windows Paint 中打开 PNG 文件并单击“保存”。这会自动将透明度变成不透明的白色。
This does the job for me:
Here is a starter image with a transparent background in case it helps with testing:
Also, for one-offs on PC, you can always open the PNG file in Windows Paint and click Save. This will automatically turn the transparency to opaque white.