英文:
imagemagick convert does not produce alpha gradient for specific image
问题
我使用convert
生成带有渐变淡出效果的图像,将图像转换为如下所示:
到像这样的图像(注意底部的渐变效果):
源图像(例如上面的第一张图像)是从PDF文件转换而来的PNG图像(如果这有所帮助)。我使用的convert
命令如下:
convert source.png -alpha set -background none -channel A \
-sparse-color barycentric "0,%[h] none 0,{img_height*(1-fade_height)} white" \
+channel source_fade.png
其中{img_height*(1-fade_height)}
会被替换为图像的高度乘以一个确定淡出开始位置的因子(例如,对于1000像素高的图像,可能是800)。
有一张图像似乎无法正常工作:
在这张图像上运行相应的命令只会生成一个底部略微渐变的空白图像:
convert img/covers/Venn_1888_cover_cropped.png \
-alpha set -background none -channel A \
-sparse-color barycentric "0,%[h] none 0,778 white" \
+channel img/covers/Venn_1888_cover_cropped_fade.png
可能出现这种情况的原因是什么?如果有帮助的话,我使用的是MacOS 13.4.1(22F82),并且使用Homebrew安装的ImageMagick:
# convert --version
Version: ImageMagick 7.1.1-11 Q16-HDRI aarch64 21206 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP(5.0)
Delegates (built-in): bzlib fontconfig freetype gslib heic jng jp2 jpeg jxl lcms lqr ltdl lzma openexr png ps raw tiff webp xml zlib
Compiler: gcc (4.2)
英文:
I'm using convert to generate images with a fade gradient, converting images like this:
to images like this (note the fade at the bottom):
The source images (e.g., first image above) are PNGs converted from PDFs (if that helps at all). The convert command I use is:
convert source.png -alpha set -background none -channel A \
-sparse-color barycentric "0,%[h] none 0,{img_height*(1-fade_height)} white" \
+channel source_fade.png
where {img_height*(1-fade_height)}
is replaced by the image's height times a factor that determines where I want the fade to start (e.g. for a 1000px height image, maybe 800).
There is one image for which this does not seem to work:
Running the corresponding command on this image results in just a blank image with something vaguely gradient-looking at the bottom:
convert img/covers/Venn_1888_cover_cropped.png \
-alpha set -background none -channel A \
-sparse-color barycentric "0,%[h] none 0,778 white" \
+channel img/covers/Venn_1888_cover_cropped_fade.png
Why might this be happening? If it helps, I'm on MacOS 13.4.1 (22F82), with homebrew imagemagick:
# convert --version
Version: ImageMagick 7.1.1-11 Q16-HDRI aarch64 21206 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules OpenMP(5.0)
Delegates (built-in): bzlib fontconfig freetype gslib heic jng jp2 jpeg jxl lcms lqr ltdl lzma openexr png ps raw tiff webp xml zlib
Compiler: gcc (4.2)
答案1
得分: 1
这是解决方案,但我不确定这是否是Imagemagick 7中的一个bug。
您的良好结果来自具有alpha通道的SRGB图像。您的不良结果来自没有alpha通道的灰度图像。
解决方法是在读取不良图像后将其转换为SRGB,即:
convert img/covers/Venn_1888_cover_cropped.png \
-colorspace sRGB -alpha set -background none -channel A \
-sparse-color barycentric "0,%[h] none 0,778 white" \
+channel img/covers/Venn_1888_cover_cropped_fade.png
英文:
This is solution, but I am not sure if this is a bug in Imagemagick 7.
Your good result comes from an SRGB image with an alpha channel. Your bad result comes from a Grayscale image with no alpha channel.
The solution is to convert the bad image to SRGB via -colorspace sRGB after reading it. That is
convert img/covers/Venn_1888_cover_cropped.png \
-colorspace sRGB -alpha set -background none -channel A \
-sparse-color barycentric "0,%[h] none 0,778 white" \
+channel img/covers/Venn_1888_cover_cropped_fade.png
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论