Python Wand 与 imagemagick 亮度对比命令

Python Wand vs imagemagick brightness-contrast command

我尝试使用 Wand,但找不到亮度对比度命令的任何映射。

source img

我得到了一些带有白色像素的奇怪人工制品: brightness change attempt

幸运的是,-brightness-contrast 只调用 -function Polynomial 方法,这些方法在 中实现。需要一些非常简单的数学运算才能将 brightness x contrast 参数转换为 slop x intercept.

import math
from wand.image import Image

class MyImage(Image):
    def brightness_contrast(self, brightness=0.0, contrast=0.0):
        slope=math.tan((math.pi * (contrast/100.0+1.0)/4.0))
        if slope < 0.0:
          slope=0.0
        intercept=brightness/100.0+((100-brightness)/200.0)*(1.0-slope)
        self.function("polynomial", [slope, intercept])

with MyImage(filename="rose:") as img:
    img.brightness_contrast(0.0, 10.0)
    img.save(filename="rose.png")