【问题标题】:Using +level-colors from ImageMagick in Python with Wand使用 Wand 在 Python 中使用 ImageMagick 中的 +level-colors
【发布时间】:2016-12-03 04:46:18
【问题描述】:

我将使用 Wand 剪切图像的某些部分。图像具有透明背景。但在我切出部分之前,我首先要对源图像进行一些调整(实际上不改变源文件)。

我想做的调整是:

  1. 将黑点更改为灰色,将白点保留为白色
  2. 将所有颜色值缩放到新的灰色和白色范围
  3. 将透明背景替换为 100% 黑色
  4. 将图像转换为灰度

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

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

但是我如何使用 Wand 做到这一点?似乎无法从 Wand 应用 +level-colors 操作。这个问题的解决方案也是:Is there a -level function in wand-py 不适用于我的问题,我猜。因为看起来魔法图像 API 没有 level-colors 方法。

效果示例:

输入: 输出:

【问题讨论】:

  • 您发布的是 JPEG 而不是 PNG,因此没有透明像素....请发布正确的起始图像。
  • 其实我确实上传了一个透明的PNG,但是上传服务把它变成了一个jpg。至少图片说明了我所追求的效果。

标签: python imagemagick wand


【解决方案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

【讨论】:

  • 你是对的!这给出了我所追求的输出。但是如何在 Python 中使用 Wand 做到这一点?当我用image.level(0.5, 1) 尝试link 的代码时,亮度会变暗。问题似乎是链接的问题想要使用-level 而我需要在Wand 中使用+level
  • 对不起,我不会说 Python,我只是想帮助你用替代方法来做同样的事情,这样你就可以到达你想去的地方。我添加了另一种方法...
  • 完美解决马克!
【解决方案2】:

-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,只需反转黑/白点。

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

【讨论】:

  • 感谢您的回答!但是你能举例说明如何使用这段代码来达到预期的效果吗?您似乎使用红色和绿色作为黑白点。在我的问题中,我使用灰色和白色。预期的结果是黑人变成灰色。图像的整体外观会比以前更灰暗。在您的示例中,颜色已完全改变。当我尝试您对+level-colors 的建议时,颜色会反转。那不是我想要的……
【解决方案3】:

fmw42 在A way to perform the +level ImageMagick operation in Wand? 中指定了一种使用多项式函数在 Wand 中实现 +level 操作的方法。

我已经应用了 fmw42 的解决方案来创建一个执行 +level-colors 操作的函数。

from wand.image import Image

#wand_imageToColorize should be an instance of wand.image.Image.
#i_listRgbRangeLowerLimits = [iRedLowerLimit, iGreenLowerLimit, iBlueLowerLimit]
#i_listRgbRangeUpperLimits = [iRedUpperLimit, iGreenUpperLimit, iBlueUpperLimit]
def Colorize(wand_imageToColorize,
             i_listRgbRangeLowerLimits, i_listRgbRangeUpperLimits):
    #input assurance
    for iIndex in range(0, 3):
        if i_listRgbRangeLowerLimits[iIndex] > \
            i_listRgbRangeUpperLimits[iIndex]:
            iTemp = i_listRgbRangeLowerLimits[iIndex]
            i_listRgbRangeLowerLimits[iIndex] = i_listRgbRangeUpperLimits[iIndex]
            i_listRgbRangeUpperLimits[iIndex] = iTemp

        if i_listRgbRangeLowerLimits[iIndex] < 0:
            i_listRgbRangeLowerLimits[iIndex] = 0

        if i_listRgbRangeUpperLimits[iIndex] > 255:
            i_listRgbRangeUpperLimits[iIndex] = 255

    #perform colorization
    str_tupleChannelNames = ('red', 'green', 'blue')

    for iColorComponentIndex in range(0, 3):
        strChannelName = str_tupleChannelNames[iColorComponentIndex]

        fB = float(i_listRgbRangeLowerLimits[iColorComponentIndex]) / 255.0
        fA = float(i_listRgbRangeUpperLimits[iColorComponentIndex]) / 255.0 - fB

        wand_imageToColorize.function('polynomial', [fA, fB], strChannelName)

【讨论】:

    【解决方案4】:

    你的命令毫无意义。 +clone 会导致两个相同的输出图像。通常克隆需要它在一组括号中。

    也许你的意思是

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


    除了您的图像是JPG而不是PNG这一事实之外,我可以实现与您所拥有的类似的东西,通过

    convert input.jpg -colorspace gray -evaluate add 50% result.jpg
    


    稍微调整一下这个值 (50%) 可以让它更接近。

    请参阅 http://docs.wand-py.org/en/0.5.1/wand/image.html 上的 wand.image.EVALUATE_OPS 以了解 -evaluate 添加 50%

    【讨论】:

      【解决方案5】:

      我的另一个答案可能更简单,并且可以完成相关工作。但是这里有一个正式的方法来做 +level-colors 使用 -clut。

      例如:

      输入:


      这里是+level-colors

      convert lenag.png +level-colors red,blue lenag_levelcolors.png
      


      使用 -clut,虽然不是完美匹配,但您可以非常接近。

      convert lenag.png \( -size 1x1 xc:red xc:blue +append -filter cubic -resize 256x1! \) -clut lenag_rb4.png
      


      对于 OP 原始处理,可以这样做:

      convert input.jpg -colorspace gray \( -size 1x1 xc:gray xc:white +append -filter cubic -resize 256x1! \) -clut output.jpg
      


      http://docs.wand-py.org/en/0.5.1/wand/image.html查看魔杖clut

      【讨论】:

        猜你喜欢
        • 2020-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-10
        • 2014-05-25
        • 2015-04-25
        • 2021-12-14
        相关资源
        最近更新 更多