Esempio n. 1
0
def map(image, lookup, interpolation):  # @ReservedAssignment
    if not interpolation:
        interpolation = 'average'

    interpolation = enum_lookup(interpolation, interpolations)

    c_call(image, 'clut', lookup, interpolation)
Esempio n. 2
0
def add_noise(image, attenuate, noise_type):
    if not noise_type:
        noise_type = 'gaussian'

    noise_type = enum_lookup(noise_type, noises)

    c_call(image, 'add_noise', noise_type, attenuate)
Esempio n. 3
0
def get_range(image):
    minimum, maximum = c_double(), c_double()

    c_call(image, ('get', 'range'), byref(minimum), byref(maximum))

    return tuple(x.value / (2 ** magick.get_depth() - 1)
                 for x in (minimum, maximum))
Esempio n. 4
0
def sepia(image, threshold, saturation):
    threshold = (2 ** magick.get_depth() - 1) * threshold

    c_call(image, 'sepia_tone', threshold)

    if saturation:
        modulate(image, 0, saturation, 0)
Esempio n. 5
0
def overlay(image, other, x, y, composite):
    if not composite:
        composite = composites.over

    composite = enum_lookup(composite, composites)

    c_call(image, 'composite', other, composite, x, y)
Esempio n. 6
0
def init_dll(dll):
    def shutdown():
        logger.debug('Cleaning up traced instances')
        _cleanup()

        c_call(None, 'terminus')

        if jython:
            from java.lang import System  # @UnresolvedImport
            System.exit(0)

    logger.debug('Critical section - init MagickWand')
    with __lock:
        if not dll.__inited:
            c_call(None, 'genesis', __init=False)

            logger.debug('Registering atexit handler')
            atexit.register(shutdown)

            dll.__inited = True

    version = magick.get_version()
    if version < min_version:
        msg = formattable('Unsupported version of MagickWand {0}')
        warn(msg.format(version))
Esempio n. 7
0
def map(image, lookup, interpolation):  # @ReservedAssignment
    if not interpolation:
        interpolation = 'average'

    interpolation = enum_lookup(interpolation, interpolations)

    c_call(image, 'clut', lookup, interpolation)
Esempio n. 8
0
def flip(image, axis):
    if axis.name == 'x':
        c_call(image, 'flip')
    elif axis.name == 'y':
        c_call(image, 'flop')
    else:
        raise PystaciaException('axis must be X or Y')
Esempio n. 9
0
def charcoal(image, radius, strength, bias):
    if strength is None:
        strength = radius
    if bias is None:
        bias = 0

    c_call(image, 'charcoal', radius, strength, bias)
Esempio n. 10
0
def charcoal(image, radius, strength, bias):
    if strength is None:
        strength = radius
    if bias is None:
        bias = 0

    c_call(image, 'charcoal', radius, strength, bias)
Esempio n. 11
0
def overlay(image, other, x, y, composite):
    if not composite:
        composite = composites.over

    composite = enum_lookup(composite, composites)

    c_call(image, 'composite', other, composite, x, y)
Esempio n. 12
0
def add_noise(image, attenuate, noise_type):
    if not noise_type:
        noise_type = 'gaussian'

    noise_type = enum_lookup(noise_type, noises)

    c_call(image, 'add_noise', noise_type, attenuate)
Esempio n. 13
0
def write(
        image,
        filename,
        format,  # @ReservedAssignment
        compression,
        quality,
        flatten,
        background):
    if not format:
        format = splitext(filename)[1][1:].lower()  # @ReservedAssignment

    if flatten is None:
        flatten = (image.type.name.endswith('matte')
                   and format not in ('png', 'tiff', 'tif', 'bmp', 'gif'))

    if not background:
        background = 'white'

    if flatten:
        background = blank(image.width, image.height, background)
        background.overlay(image)
        image = background

    with state(image, format=format, compression_quality=quality):
        c_call(image, 'write', filename)
Esempio n. 14
0
def sepia(image, threshold, saturation):
    threshold = (2**magick.get_depth() - 1) * threshold

    c_call(image, 'sepia_tone', threshold)

    if saturation:
        modulate(image, 0, saturation, 0)
Esempio n. 15
0
def get_range(image):
    minimum, maximum = c_double(), c_double()

    c_call(image, ('get', 'range'), byref(minimum), byref(maximum))

    return tuple(x.value / (2**magick.get_depth() - 1)
                 for x in (minimum, maximum))
Esempio n. 16
0
def fill(image, fill, blend):
    # image magick ignores alpha setting of color
    # let's incorporate it into blend
    blend *= fill.alpha

    blend = from_rgb(blend, blend, blend)

    c_call(image, 'colorize', fill, blend)
Esempio n. 17
0
def fill(image, fill, blend):
    # image magick ignores alpha setting of color
    # let's incorporate it into blend
    blend *= fill.alpha

    blend = from_rgb(blend, blend, blend)

    c_call(image, 'colorize', fill, blend)
Esempio n. 18
0
def read(spec, width=None, height=None, factory=None):
    image = _instantiate(factory)

    if width and height:
        c_call('magick', 'set_size', image, width, height)

    c_call(image, 'read', spec)

    return image
Esempio n. 19
0
def get_options():
    options = {}

    size = c_size_t()
    keys = c_call('magick_', 'query_configure_options', '*', byref(size))
    for key in [native_str(keys[i]) for i in range(size.value)]:
        options[key] = c_call('magick_', 'query_configure_option', key)

    return options
Esempio n. 20
0
def read(spec, width=None, height=None, factory=None):
    image = _instantiate(factory)

    if width and height:
        c_call('magick', 'set_size', image, width, height)

    c_call(image, 'read', spec)

    return image
Esempio n. 21
0
def despeckle(image):
    """Attempt to remove speckle preserving edges.

       Resulting image almost solid color areas are smoothed preserving
       edges.

       This method can be chained.
    """
    c_call(image, 'despeckle')
Esempio n. 22
0
    def shutdown():
        logger.debug('Cleaning up traced instances')
        _cleanup()

        c_call(None, 'terminus')

        if jython:
            from java.lang import System  # @UnresolvedImport
            System.exit(0)
Esempio n. 23
0
def denoise(image):
    """Attempt to remove noise preserving edges.

       Applies a digital filter that improves the quality of a
       noisy image.

       This method can be chained.
    """
    c_call(image, 'enhance')
Esempio n. 24
0
def denoise(image):
    """Attempt to remove noise preserving edges.

       Applies a digital filter that improves the quality of a
       noisy image.

       This method can be chained.
    """
    c_call(image, 'enhance')
Esempio n. 25
0
def despeckle(image):
    """Attempt to remove speckle preserving edges.

       Resulting image almost solid color areas are smoothed preserving
       edges.

       This method can be chained.
    """
    c_call(image, 'despeckle')
Esempio n. 26
0
def set_string(color, value):
    try:
        c_call(color, 'set_color', value)
    except PystaciaException:
        info = exc_info()
        matches = info[1].args[0].startswith
        if matches('unrecognized color') or matches('UnrecognizedColor'):
            raise PystaciaException('Unknown color string representation')

        reraise(*info)
Esempio n. 27
0
def set_string(color, value):
    try:
        c_call(color, 'set_color', value)
    except PystaciaException:
        info = exc_info()
        matches = info[1].args[0].startswith
        if matches('unrecognized color') or matches('UnrecognizedColor'):
            raise PystaciaException('Unknown color string representation')

        reraise(*info)
Esempio n. 28
0
def set_color(image, fill):
    if get_c_method('image', ('set', 'color'), throw=False):
        c_call(image, ('set', 'color'), fill)

        # MagickSetImageColor doesnt copy alpha
        if not fill.opaque:
            set_alpha(image, fill.alpha)
    else:
        width, height = image.size
        image._free()
        image.__init__(blank(width, height, fill)._claim())
Esempio n. 29
0
def radial_blur(image, angle):
    """Performs radial blur.

       :param angle: Blur angle in degrees
       :type angle: ``float``

       Radial blurs image within given angle.

       This method can be chained.
    """
    c_call(image, 'radial_blur', angle)
Esempio n. 30
0
def skew(image, offset, axis):
    if not axis:
        axis = axes.x

    if axis == axes.x:
        x_angle = degrees(atan(offset / image.height))
        y_angle = 0
    elif axis == axes.y:
        x_angle = 0
        y_angle = degrees(atan(offset / image.width))
    c_call(image, 'shear', from_string('transparent'), x_angle, y_angle)
Esempio n. 31
0
def set_color(image, fill):
    if get_c_method('image', ('set', 'color'), throw=False):
        c_call(image, ('set', 'color'), fill)

        # MagickSetImageColor doesnt copy alpha
        if not fill.opaque:
            set_alpha(image, fill.alpha)
    else:
        width, height = image.size
        image._free()
        image.__init__(blank(width, height, fill)._claim())
Esempio n. 32
0
def radial_blur(image, angle):
    """Performs radial blur.

       :param angle: Blur angle in degrees
       :type angle: ``float``

       Radial blurs image within given angle.

       This method can be chained.
    """
    c_call(image, 'radial_blur', angle)
Esempio n. 33
0
    def test_exception(self):
        img = sample()

        self.assertRaises(PystaciaException,
                          lambda: c_call('magick', 'set_format', img, 'lolz'))
        c_call('magick', 'set_format', img, 'bmp')

        img.close()

        self.assertRaises(AttributeError,
                          lambda: get_c_method('magick', 'non_existant'))
        self.assertFalse(get_c_method('magick', 'non_existant', throw=False))
Esempio n. 34
0
def ping(filename):
    image = _instantiate(None)

    c_call(image, 'ping', filename)

    result = {
        'width': image.width,
        'height': image.height,
        'format': image.format
    }

    image.close()
    return result
Esempio n. 35
0
    def test_exception(self):
        img = sample()

        self.assertRaises(PystaciaException,
                          lambda: c_call('magick', 'set_format',
                                         img, 'lolz'))
        c_call('magick', 'set_format', img, 'bmp')

        img.close()

        self.assertRaises(AttributeError,
                          lambda: get_c_method('magick', 'non_existant'))
        self.assertFalse(get_c_method('magick', 'non_existant', throw=False))
Esempio n. 36
0
def ping(filename):
    image = _instantiate(None)

    c_call(image, 'ping', filename)

    result = {
        'width': image.width,
        'height': image.height,
        'format': image.format
    }

    image.close()
    return result
Esempio n. 37
0
    def function(image, *args):
        kwargs = dict(zip(names, args))

        if kwargs['strength'] is None:
            kwargs['strength'] = kwargs['radius']

        if 'bias' in kwargs and kwargs['bias'] is None:
            kwargs['bias'] = 0

        order_ = order or names

        values = [kwargs[k] for k in order_]

        c_call(image, c_name, *values)
Esempio n. 38
0
    def function(image, *args):
        kwargs = dict(zip(names, args))

        if kwargs['strength'] is None:
            kwargs['strength'] = kwargs['radius']

        if 'bias' in kwargs and kwargs['bias'] is None:
            kwargs['bias'] = 0

        order_ = order or names

        values = [kwargs[k] for k in order_]

        c_call(image, c_name, *values)
Esempio n. 39
0
def ping_blob(blob):
    image = _instantiate(None)

    if hasattr(blob, 'read'):
        blob = blob.read()

    c_call(image, ('ping', 'blob'), blob, len(blob))

    result = {
        'width': image.width,
        'height': image.height,
        'format': image.format
    }

    image.close()
    return result
Esempio n. 40
0
    def _get_state(self, key, enum=None):
        value = c_call(self, ("get", key))

        if enum:
            value = enum_reverse_lookup(enum, value)

        return value
Esempio n. 41
0
def ping_blob(blob):
    image = _instantiate(None)

    if hasattr(blob, 'read'):
        blob = blob.read()

    c_call(image, ('ping', 'blob'), blob, len(blob))

    result = {
        'width': image.width,
        'height': image.height,
        'format': image.format
    }

    image.close()
    return result
Esempio n. 42
0
    def _get_state(self, key, enum=None):
        value = c_call(self, ('get', key))

        if enum:
            value = enum_reverse_lookup(enum, value)

        return value
Esempio n. 43
0
def rescale(image, width, height, factor, filter, blur):  # @ReservedAssignment
    if not filter:
        filter = filters.undefined  # @ReservedAssignment

    width, height = _proportionally(image, width, height)

    if not width and not height:
        if not factor:
            msg = 'Either width, height or factor must be provided'
            raise PystaciaException(msg)

        width, height = image.size
        if not hasattr(factor, '__getitem__'):
            factor = (factor, factor)
        width, height = width * factor[0], height * factor[1]

    c_call(image, 'resize', width, height, enum_lookup(filter, filters), blur)
Esempio n. 44
0
def splice(image, x, y, width, height):
    background = from_string('transparent')

    # preserve background color
    old_color = Color()

    c_call(image, ('get', 'background_color'), old_color)
    c_call(image, ('set', 'background_color'), background)

    c_call(image, 'splice', width, height, x, y)

    c_call(image, ('set', 'background_color'), old_color)
Esempio n. 45
0
def write(image, filename, format,  # @ReservedAssignment
          compression, quality, flatten, background):
    if not format:
        format = splitext(filename)[1][1:].lower()  # @ReservedAssignment

    if flatten is None:
        flatten = (image.type.name.endswith('matte') and
                   format not in ('png', 'tiff', 'tif', 'bmp', 'gif'))

    if not background:
        background = 'white'

    if flatten:
        background = blank(image.width, image.height, background)
        background.overlay(image)
        image = background

    with state(image, format=format, compression_quality=quality):
        c_call(image, 'write', filename)
Esempio n. 46
0
def trim(image, similarity, background):
    # TODO: guessing of background?
    if not background:
        background = from_string('transparent')

    # preserve background color
    old_color = Color()

    c_call(image, ('get', 'background_color'), old_color)
    c_call(image, ('set', 'background_color'), background)

    c_call(image, 'trim', similarity * 000)

    c_call(image, ('set', 'background_color'), old_color)
Esempio n. 47
0
def wave(image, amplitude, length, offset, axis):
    if not axis:
        axis = axes.x

    transparent = from_string('transparent')

    # preserve background color
    old_color = Color()

    c_call(image, ('get', 'background_color'), old_color)
    c_call(image, ('set', 'background_color'), transparent)

    c_call(image, 'wave', amplitude, length)

    c_call(image, ('set', 'background_color'), old_color)
Esempio n. 48
0
def read_raw(raw, format, width, height,  # @ReservedAssignment
             depth, factory=None):
    image = _instantiate(factory)

    c_call('magick', 'set_size', image, width, height)
    c_call('magick', 'set_depth', image, depth)
    format = format.upper()  # @ReservedAssignment
    c_call('magick', 'set_format', image, format)

    if hasattr(raw, 'read'):
        raw = raw.read()

    c_call(image, ('read', 'blob'), raw, len(raw))

    return image
Esempio n. 49
0
def read_blob(blob, format, factory):  # @ReservedAssignment
    image = _instantiate(factory)

    #resource = image.resource
    #if format:
        # ensure we always get bytes
    #    format = b(format.upper())  # @ReservedAssignment
    #    old_format = cdll.MagickGetImageFormat(resource)
    #    template = formattable('Format "{0}" unsupported')
    #    guard(resource,
    #          lambda: cdll.MagickSetFormat(resource, format),
    #          template.format(format))

    if hasattr(blob, 'read'):
        blob = blob.read()

    c_call(image, ('read', 'blob'), blob, len(blob))

    #if format:
    #    guard(resource,
    #              lambda: cdll.MagickSetFormat(resource, old_format))

    return image
Esempio n. 50
0
def read_blob(blob, format, factory):  # @ReservedAssignment
    image = _instantiate(factory)

    #resource = image.resource
    #if format:
    # ensure we always get bytes
    #    format = b(format.upper())  # @ReservedAssignment
    #    old_format = cdll.MagickGetImageFormat(resource)
    #    template = formattable('Format "{0}" unsupported')
    #    guard(resource,
    #          lambda: cdll.MagickSetFormat(resource, format),
    #          template.format(format))

    if hasattr(blob, 'read'):
        blob = blob.read()

    c_call(image, ('read', 'blob'), blob, len(blob))

    #if format:
    #    guard(resource,
    #              lambda: cdll.MagickSetFormat(resource, old_format))

    return image
Esempio n. 51
0
def threshold(image, factor, mode):
    if not mode:
        mode = 'default'

    if mode == 'default':
        factor = (2 ** magick.get_depth() - 1) * factor
    elif mode == 'random':
        factor = [(2 ** magick.get_depth() - 1) * f for f in factor]
    else:
        factor = color.from_rgb(factor, factor, factor)

    if mode == 'default':
        c_call(image, 'threshold', factor)
    elif mode == 'white':
        c_call(image, 'white_threshold', factor)
    elif mode == 'black':
        c_call(image, 'black_threshold', factor)
    elif mode == 'random':
        c_call(image, 'random_threshold', factor[0], factor[1])
Esempio n. 52
0
def threshold(image, factor, mode):
    if not mode:
        mode = 'default'

    if mode == 'default':
        factor = (2**magick.get_depth() - 1) * factor
    elif mode == 'random':
        factor = [(2**magick.get_depth() - 1) * f for f in factor]
    else:
        factor = color.from_rgb(factor, factor, factor)

    if mode == 'default':
        c_call(image, 'threshold', factor)
    elif mode == 'white':
        c_call(image, 'white_threshold', factor)
    elif mode == 'black':
        c_call(image, 'black_threshold', factor)
    elif mode == 'random':
        c_call(image, 'random_threshold', factor[0], factor[1])