0

Is there a -level function in wand-py to adjust the levels of an image?

How do I access this ?

Gabriel
  • 8,990
  • 6
  • 57
  • 101

1 Answers1

1

The -level operation, or MagickLevelImage C-API, does not currently exists in wand-py. However, wand.api makes adding support for this method very easy.

Example extending the wand.image.Image class:

from ctypes import c_void_p, c_double, c_int
from wand.api import library
from wand.image import Image

# Define C-API method signatures
library.MagickLevelImage.argtypes = [c_void_p,  # wand
                                     c_double,  # black_point
                                     c_double,  # gamma
                                     c_double]  # white_point
library.MagickLevelImage.restype = c_int


class MyImage(Image):
    def level(self, black, white, gamma=1.0):
        # Assert black, gamma, & white are float types
        # between 0.0 & 1.0.
        # Both black & white values must be converted to
        # QuantumRange percentages.
        quantum = float(self.quantum_range)
        return library.MagickLevelImage(self.wand,
                                        black * quantum,
                                        gamma,
                                        white * quantum)

if __name__ == '__main__':
    # convert rose: -level 20%,50% rose_level.png
    with MyImage(filename='rose:') as image:
        image.level(0.2, 0.5)
        image.save(filename='rose_level.png')

A -level function in wand-py

emcconville
  • 23,800
  • 4
  • 50
  • 66
  • 1
    -level and +level can be achieve with some simple computations in Wand using the `function` command with argument `polynomial`. See https://stackoverflow.com/questions/55109692/a-way-to-perform-the-level-imagemagick-operation-in-wand/55110842#55110842 – fmw42 Mar 17 '19 at 19:14