问题
Using the +level ImageMagick operator (https://imagemagick.org/script/command-line-options.php#level) in command line will produce an output image with the channel values of the input image compressed to the range specified.
For example, the following ImageMagick command will cause the output pixel intensity values to be present between 45% and 70% of the total pixel value range.
magick input.jpg +level 45%,70% output.jpg
How do you perform the +level ImageMagick operation in Wand?
The wand.image.BaseImage.level() (http://docs.wand-py.org/en/0.5.1/wand/image.html#wand.image.BaseImage.level) seems to perform the -level ImageMagick operation.
As specified on https://imagemagick.org/script/command-line-options.php#level, there is a close relationship between the -level and +level ImageMagick operators.
I have no idea how to do the +level ImageMagick operation in Wand.
Can someone shed a light on this?
回答1:
Sorry, I do not know Wand syntax that well. Perhaps someone who does can add to this answer or post another that provides the proper syntax.
But you can achieve the equivalent to +level using the wand function
command with argument polynomial
, where the polynomial arguments are the equivalent of the equation a*x+b
. See http://docs.wand-py.org/en/0.5.1/wand/image.html.
You have to compute a and b from your values and the following equation to achieve the equivalent of +level.
a*X+b = Y
When X=0, then Y=0.45.
When X=1, then y=0.70.
So we have two linear equations that need to be solved.
0*a+b=0.45
1*a+b=0.70
From the top equation, you have
b=0.45
Substitute b into the bottom equation and get
a+0.45=0.70 --> a=0.25
In ImageMagick, you would then use
convert image.suffix -function polynomial "0.45, 0.25" result.suffix
See https://imagemagick.org/Usage/transform/#function_polynomial
In Wand function, you will need to select polynomial and then provide the a and b values above.
A guess at the Wand function
syntax would be something like:
from wand.image import Image
with Image(filename='yourimage.suffix') as img:
a = 0.25
b = 0.45
img.function('polynomial', [a, b])
...
来源:https://stackoverflow.com/questions/55109692/a-way-to-perform-the-level-imagemagick-operation-in-wand