ImageMagick convert在特定图像上不生成Alpha渐变。

huangapple go评论55阅读模式
英文:

imagemagick convert does not produce alpha gradient for specific image

问题

我使用convert生成带有渐变淡出效果的图像,将图像转换为如下所示:

ImageMagick convert在特定图像上不生成Alpha渐变。

到像这样的图像(注意底部的渐变效果):

ImageMagick convert在特定图像上不生成Alpha渐变。

源图像(例如上面的第一张图像)是从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)。

有一张图像似乎无法正常工作:

ImageMagick convert在特定图像上不生成Alpha渐变。

在这张图像上运行相应的命令只会生成一个底部略微渐变的空白图像:

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:


ImageMagick convert在特定图像上不生成Alpha渐变。


to images like this (note the fade at the bottom):


ImageMagick convert在特定图像上不生成Alpha渐变。


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:


ImageMagick convert在特定图像上不生成Alpha渐变。


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 

ImageMagick convert在特定图像上不生成Alpha渐变。


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

huangapple
  • 本文由 发表于 2023年7月18日 00:47:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定