0

I try to use Wand and can't find any mappings for brightness-contrast command.

source img

  • Tried to use modulate for changing brightness:

    value = 100 + value # no changes = 0 in console and 100 in wand img.modulate(brightness=value)

and I got some strange artefacts with white pixels: brightness change attempt

  • For working with contrast Wand has just contrast_stretch() and I can't understand how to do something like this

    convert '-brightness-contrast 0x%d'

40min
  • 117
  • 1
  • 9

1 Answers1

1

Luckily, -brightness-contrast just calls -function Polynomial methods which is implemented in . Some very simple math is needed to translate the brightness x contrast arguments into 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")

rose.png

emcconville
  • 23,800
  • 4
  • 50
  • 66
  • very simple :) Yes, it's works, but I don't understand how. However imagemagick is blackbox for me too. I just can't understand why Wand developers didn't create similar API like in shell. – 40min May 31 '17 at 14:55
  • 1
    As an explanation, if you want to increase brightness by 10% then use the equivalent of ImageMagick -modulate(110,100,100). But you seem to want to change the contrast and not the brightest. You could do that using the equivalent of -level 0x90% or +level 0x110%. Using the equivalent of -function polynomial in linear mode, you have two arguments a and b, which are the equivalent of slope and intercept. Slope controls contrast and intercept controls brightness. – fmw42 May 31 '17 at 16:45
  • Absolutely! [`Image.modulate`](http://docs.wand-py.org/en/0.4.4/wand/image.html#wand.image.BaseImage.modulate) & [`Image.level`](http://docs.wand-py.org/en/0.4.4/wand/image.html#wand.image.Image.level) exists on the `wand` library. – emcconville May 31 '17 at 18:02
  • Guys, thank you very match for answers. I have another question about Wand/Imagemagick can you look at it? https://stackoverflow.com/questions/44327442/matching-wandimagemagick-on-camanjs-functionality – 40min Jun 02 '17 at 11:11