使用+;用魔杖将Python中ImageMagick的颜色调平
我要用魔杖切下图像的一些部分。图像有一个透明的背景。但是在我剪切这些部分之前,我首先要对源图像进行一些调整(而不是实际更改源文件) 我想做的调整是:使用+;用魔杖将Python中ImageMagick的颜色调平,python,imagemagick,wand,Python,Imagemagick,Wand,我要用魔杖切下图像的一些部分。图像有一个透明的背景。但是在我剪切这些部分之前,我首先要对源图像进行一些调整(而不是实际更改源文件) 我想做的调整是: 将黑色点更改为灰色,将白色点保留为白色 将所有颜色值缩放到新的灰色和白色范围 将透明背景替换为100%黑色 将图像转换为灰度 我可以使用ImageMagick的简单命令获得所需的结果: convert input.png+clone+level colors灰度、白色-背景黑色-alpha remove-colorspace灰度输出.png 但我怎
convert input.png+clone+level colors灰度、白色-背景黑色-alpha remove-colorspace灰度输出.png
但我怎么用魔杖来做呢?似乎无法应用魔杖的+级别颜色操作。这个问题的解决方案:我想不适用于我的问题。因为magick图像API似乎没有级别颜色方法
效果的示例结果:
输入:
输出:
由于您的输出图像无论如何都是灰色的,因此您实际上不需要
+级别颜色
,您可以这样做:
convert movies.png -channel RGB -colorspace gray +level 50,100% -background black -alpha remove output.png
另一个选项可能是使用-fx
操作符。如果您想象您的像素亮度在0
(黑色)和1
(白色)之间变化,那么如果您将所有亮度除以2,它们将在0
和0.5
之间变化。然后,如果添加0.5
,它们将在0.5
(中灰色)和1
(白色)之间变化-这就是您想要的:
convert movies.png -channel RGB -colorspace gray -fx "(u/2)+0.5" -background black -alpha remove output.png
由于您的输出图像无论如何都是灰色的,因此您实际上不需要
+级别颜色
,您可以这样做:
convert movies.png -channel RGB -colorspace gray +level 50,100% -background black -alpha remove output.png
另一个选项可能是使用-fx
操作符。如果您想象您的像素亮度在0
(黑色)和1
(白色)之间变化,那么如果您将所有亮度除以2,它们将在0
和0.5
之间变化。然后,如果添加0.5
,它们将在0.5
(中灰色)和1
(白色)之间变化-这就是您想要的:
convert movies.png -channel RGB -colorspace gray -fx "(u/2)+0.5" -background black -alpha remove output.png
-level颜色
行为可以通过wand.image.image.level
方法应用,但需要为每个颜色通道执行。提供的两种颜色用作参考黑/白点
例如
从wand.image导入图像
从wand.color导入颜色
从wand.compat导入嵌套
图像(filename='rose:')为rose:
#-水平颜色红色、绿色
带有嵌套(颜色('red'),
颜色(‘绿色’)为(黑色)点,
白点):
#红色通道
玫瑰色水平仪(黑色\点红色,
白点红,
1.0,
"红色")
#绿色通道
玫瑰色水平仪(黑色\点绿色,
白点,绿点,
1.0,
"绿色")
#蓝色通道
玫瑰色水平仪(黑色\点蓝色,
白色,蓝色,
1.0,
"蓝色")
rose.save(filename='output.png')
对于+级别颜色
,只需反转黑色/白色点即可
rose.level(白点红、,
黑点,红点,
1.0,
"红色")
可以通过wand.image.image.level
方法应用-level颜色
行为,但需要为每个颜色通道执行。提供的两种颜色用作参考黑/白点
例如
从wand.image导入图像
从wand.color导入颜色
从wand.compat导入嵌套
图像(filename='rose:')为rose:
#-水平颜色红色、绿色
带有嵌套(颜色('red'),
颜色(‘绿色’)为(黑色)点,
白点):
#红色通道
玫瑰色水平仪(黑色\点红色,
白点红,
1.0,
"红色")
#绿色通道
玫瑰色水平仪(黑色\点绿色,
白点,绿点,
1.0,
"绿色")
#蓝色通道
玫瑰色水平仪(黑色\点蓝色,
白色,蓝色,
1.0,
"蓝色")
rose.save(filename='output.png')
对于+级别颜色
,只需反转黑色/白色点即可
rose.level(白点红、,
黑点,红点,
1.0,
"红色")
fmw42指定了一种在中使用多项式函数在Wand中实现+电平操作的方法
我已经应用了fmw42的解决方案来创建一个执行+级别颜色操作的函数
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)
从wand.image导入图像
#wand_imageToColorize应该是wand.image.image的一个实例。
#i_listRgbRangeLowerLimits=[iRedLowerLimit,iGreenLowerLimit,iBlueLowerLimit]
#i_listRgbRangeUpperLimits=[iRedUpperLimit,IGreenPerLimit,iBlueUpperLimit]
def着色(棒形图像着色,
i_listRgbRangeLowerLimits,i_listRgbRangeUpperLimits):
#输入保证
对于范围(0,3)内的iIndex:
如果我列出了Rgbrangelowerlimits[iIndex]>\
i_listRgbRangeUpperLimits[指数]:
iTemp=i_listRgbRangeLowerLimits[iIndex]
i_listRgbRangeLowerLimits[IIIndex]=i_listRgbRangeUpperLimits[IIIndex]
i_listRgbRangeUpperLimits[iIndex]=iTemp
如果i_listRgbRangeLowerLimits[iIndex]<0:
i_listRgbRangeLowerLimits[iIndex]=0
如果i_listRgbRangeUpperLimits[iIndex]>255:
i_listRgbRangeUpperLimits[iIndex]=255
#着色
str_tupleChannelNames=(“红色”、“绿色”、“蓝色”)
对于范围(0,3)内的iColorComponentIndex:
strChannelName=str_tupleChannelNames[iColorComponentIndex]
fB=浮动(i_列表RGB)功率
convert lenag.png +level-colors red,blue lenag_levelcolors.png
convert lenag.png \( -size 1x1 xc:red xc:blue +append -filter cubic -resize 256x1! \) -clut lenag_rb4.png
convert input.jpg -colorspace gray \( -size 1x1 xc:gray xc:white +append -filter cubic -resize 256x1! \) -clut output.jpg