Search code examples
imagemagickpngimagemagick-convert

imagemagick convert does not produce alpha gradient for specific image


I'm using convert to generate images with a fade gradient, converting images like this:


cropped image


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


cropped and faded image


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:


Cropped image that does not seem to like to be faded


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 

incorrect, blank image with something vaguely gradient-looking at the bottom


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)

Solution

  • 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