首页 文章

使用带有Wand的Python中ImageMagick的级别颜色

提问于
浏览
0

我将使用Wand来剪切图像的某些部分 . 图像具有透明背景 . 但在我删除部分之前,我首先要对源图像进行一些调整(而不是实际更改源文件) .

我想做的调整是:

  • 将黑点更改为灰色,并将白点保持为白色

  • 将所有颜色值缩放到新的灰色和白色范围

  • 用100%黑色替换透明背景

  • 将图像转换为灰度

我可以使用ImageMagick的简单命令获得所需的结果:

convert input.png +clone +level-colors gray,white -background black -alpha remove -colorspace Gray output.png

但是我如何使用Wand做到这一点?似乎没有办法从Wand应用级别颜色操作 . 也是这个问题的解决方案:Is there a -level function in wand-py没有't apply to my problem, I guess. Because it seems the magick image API doesn' t有一个水平颜色方法 .

效果的示例结果:

输入:
Before
输出:
After

2 回答

  • 1

    既然你的输出图像是灰色的,你真的不需要 +level-colors ,你可以做同样的事情:

    convert movies.png -channel RGB -colorspace gray +level 50,100% -background black -alpha remove output.png
    

    另一种选择可能是使用 -fx 运算符 . 如果你想象你的像素亮度在 0 (黑色)和 1 (白色)之间变化,那么如果你将所有亮度除以2,它们将在 00.5 之间变化 . 然后,如果你添加 0.5 ,它们将在 0.5 (中灰色)和 1 (白色)之间变化 - 这是你想要的:

    convert movies.png -channel RGB -colorspace gray -fx "(u/2)+0.5" -background black -alpha remove output.png
    
  • 1

    -level-colors 行为可以通过 wand.image.Image.level 方法应用,但需要为每个颜色通道执行 . 提供的两种颜色用作参考黑/白点 .

    例如...

    from wand.image import Image
    from wand.color import Color
    from wand.compat import nested
    
    with Image(filename='rose:') as rose:
        # -level-colors red,green
        with nested(Color('red'),
                    Color('green')) as (black_point,
                                        white_point):
            # Red channel
            rose.level(black_point.red,
                       white_point.red,
                       1.0,
                       'red')
            # Green channel
            rose.level(black_point.green,
                       white_point.green,
                       1.0,
                       'green')
            # Blue channel
            rose.level(black_point.blue,
                       white_point.blue,
                       1.0,
                       'blue')
        rose.save(filename='output.png')
    

    -level-colors

    对于 +level-colors ,只需反转黑/白点 .

    rose.level(white_point.red,
               black_point.red,
               1.0,
               'red')
    

相关问题