将 PNG 图像中的透明度替换为白色背景

发布于 2024-08-22 21:54:48 字数 273 浏览 7 评论 0 原文

我有一个带有 Alpha 通道(即透明度)的 PNG 图像,我需要创建将图像层组合到白色背景上的版本。我想使用可编写脚本的命令,使用 CLI 工具(例如 Image Magick)将 PNG 直接无损地转换为 PNG。

导致错误的非工作 Image Magick 命令的示例是:

convert input.png -background white -flatten output.png

I have a PNG image with an alpha channel (i.e. transparency), and I need to create versions with the image layer composed onto a white background. I want to use a scriptable command using a CLI tool such as Image Magick to directly convert PNG to PNG losslessly.

An example of a non-working Image Magick command which results in an error is:

convert input.png -background white -flatten output.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

尛丟丟 2024-08-29 21:54:48
-background white -alpha remove -alpha off

示例:

convert image.png -background white -alpha remove -alpha off white.png

随意将白色替换为您想要的任何其他颜色。 Imagemagick 文档介绍了 -alpha remove 操作:

此操作简单快捷,无需任何操作即可完成工作
任何额外的内存使用,或可能相关的其他副作用
使用替代的透明度去除技术。 因此,这就是
消除图像透明度的首选方法。

-background white -alpha remove -alpha off

Example:

convert image.png -background white -alpha remove -alpha off white.png

Feel free to replace white with any other color you want. Imagemagick documentation says this about the -alpha remove operation:

This operation is simple and fast, and does the job without needing
any extra memory use, or other side effects that may be associated
with alternative transparency removal techniques. It is thus the
preferred way of removing image transparency.

缘字诀 2024-08-29 21:54:48

这对我有用:

convert -flatten img1.png img1-white.png

文档参考:

This works for me:

convert -flatten img1.png img1-white.png

Documentation references:

衣神在巴黎 2024-08-29 21:54:48

在 ImageMagick 中拼合图像和应用背景图像很简单,

但是命令的顺序非常重要

要在透明图像上应用任何背景并拼合它,请先应用背景,然后拼合它。反过来则不行。

$ convert sourceimage.png -background BackgroundColor -flatten destinationimage.png

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.

$ convert sourceimage.png -background BackgroundColor -flatten destinationimage.png
乄_柒ぐ汐 2024-08-29 21:54:48

唯一对我有用的是所有答案的混合:

convert in.png -background white -alpha remove -flatten -alpha off out.png

The only one that worked for me was a mix of all the answers:

convert in.png -background white -alpha remove -flatten -alpha off out.png
撩起发的微风 2024-08-29 21:54:48

以下是如何将目录中所有文件夹中的相同图像替换为白色而不是透明:

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

红衣飘飘貌似仙 2024-08-29 21:54:48

使用 -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

∞琼窗梦回ˉ 2024-08-29 21:54:48

Alpha 删除 部分">ImageMagick 使用指南 建议使用-alpha remove 选项,例如:

convert in.png  -background white  -alpha remove  out.png

...使用您选择的-background 颜色。

该指南指出:

此操作简单快速,并且不需要任何额外的内存使用,也不需要其他可能与替代透明度去除技术相关的副作用。因此,它是消除图像透明度的首选方法。

它还添加了注释:

请注意,虽然透明度被“删除”,但 Alpha 通道仍将保持打开状态,但现在将是完全不透明的。如果您不再需要 Alpha 通道,则可以使用 Alpha Off 来禁用它。

因此,如果您不需要 alpha 通道,您可以通过添加 -alpha off 选项来缩小输出图像的大小,例如:

convert in.png  -background white  -alpha remove  -alpha off  out.png

还有关于其他常用的消除透明度的技术的更多详细信息在从图像中删除透明度部分。

该部分提到了使用 -flatten 作为消除透明度的技术的一个重要警告:

但是,这不适用于“mogrify”或多个图像序列,基本上是因为“-flatten”运算符实际上旨在将多个图像合并为单个图像。

因此,如果您一次转换多个图像,例如从 PDF 文件生成缩略图,-flatten 将不会执行您想要的操作(它将所有页面的所有图像拼合为一张图像)。另一方面,使用 -alpha 删除技术​​仍然会生成多个图像,每个图像都删除了透明度。

The Alpha Remove section of the ImageMagick Usage Guide suggests using the -alpha remove option, e.g.:

convert in.png  -background white  -alpha remove  out.png

...using the -background color of your choosing.

The guide states:

This operation is simple and fast, and does the job without needing any extra memory use, or other side effects that may be associated with alternative transparency removal techniques. It is thus the prefered way of removing image transparency.

It additionally adds the note:

Note that while transparency is 'removed' the alpha channel will remain turned on, but will now be fully-opaque. If you no longer need the alpha channel you can then use Alpha Off to disable it.

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:

convert in.png  -background white  -alpha remove  -alpha off  out.png

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:

However this will not work with "mogrify" or with a sequence of multiple images, basically because the "-flatten" operator is really designed to merge multiple images into a single image.

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.

紫南 2024-08-29 21:54:48

您的命令似乎是正确的,因此问题可能是由于缺少对 PNG () 的支持。您可以使用 convert -list configure 检查或尝试以下操作:

sudo yum install libpng libpng-devel

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:

sudo yum install libpng libpng-devel
月亮邮递员 2024-08-29 21:54:48

这并不完全是您问题的答案,但我在尝试找出如何删除 alpha 通道时发现了您的问题,所以我决定在这里添加这个答案:

如果您想使用 imagemagick 删除 alpha 通道,您可以使用这个命令:

mogrify -alpha off ./*.png

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:

mogrify -alpha off ./*.png
一抹微笑 2024-08-29 21:54:48

好吧,看起来我决定安装“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.

为你鎻心 2024-08-29 21:54:48

我需要:-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 background

convert 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.

谁对谁错谁最难过 2024-08-29 21:54:48

我看到这个问题和答案对我确实有帮助,但后来我需要对很多文件执行此操作,所以如果您在一个文件夹中有多个图像(PNG 图像)并且您想对所有文件执行此操作:

find ./ -name "*.png" -exec convert {} -flatten {} \;

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:

find ./ -name "*.png" -exec convert {} -flatten {} \;
一袭白衣梦中忆 2024-08-29 21:54:48

都试过了,没有一个有效。这个人做了:

convert input.png -channel rgba -alpha set \
            -fill none -opaque white \
            -fill white -opaque black \
            -fill white -opaque none \
            -alpha off output.png

Tried all, none worked. This one did:

convert input.png -channel rgba -alpha set \
            -fill none -opaque white \
            -fill white -opaque black \
            -fill white -opaque none \
            -alpha off output.png
牵你的手,一向走下去 2024-08-29 21:54:48

这将创建一个图像,将第一个具有透明度的图像放在第二个图像的顶部,

composite -gravity center ImgWithTransp.png BackgroundSameSizeOfImg.png ResultImg.png

最初在 这篇文章

this creates an image just placing the 1st with transparency on top of the 2nd

composite -gravity center ImgWithTransp.png BackgroundSameSizeOfImg.png ResultImg.png

originally found the tip on this post

壹場煙雨 2024-08-29 21:54:48

要实际从文件中删除 Alpha 通道,请使用 alpha off 选项:

convert in.png -background white -alpha off out.png

To actually remove the alpha channel from the file, use the alpha off option:

convert in.png -background white -alpha off out.png
数理化全能战士 2024-08-29 21:54:48

这是 -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

挽袖吟 2024-08-29 21:54:48

这对我来说很有效:

magick convert OLD.png -background white -alpha remove NEW.png

这是一个具有透明背景的起始图像,以防它有助于测试:

< img src="https://i.sstatic.net/Ftndd.png" alt="具有透明背景的图像">

此外,对于 PC 上的一次性使用,您始终可以在 Windows Paint 中打开 PNG 文件并单击“保存”。这会自动将透明度变成不透明的白色。

This does the job for me:

magick convert OLD.png -background white -alpha remove NEW.png

Here is a starter image with a transparent background in case it helps with testing:

image with transparent background

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文