Exemplo 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)
Exemplo 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)
Exemplo 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))
Exemplo 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)
Exemplo 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)
Exemplo 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))
Exemplo 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)
Exemplo 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')
Exemplo 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)
Exemplo 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)
Exemplo 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)
Exemplo 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)
Exemplo 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)
Exemplo 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)
Exemplo 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))
Exemplo 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)
Exemplo 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)
Exemplo 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
Exemplo 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
Exemplo 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
Exemplo 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')
Exemplo 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)
Exemplo 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')
Exemplo 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')
Exemplo 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')
Exemplo 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)
Exemplo 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)
Exemplo 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())
Exemplo 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)
Exemplo 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)
Exemplo 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())
Exemplo 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)
Exemplo 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))
Exemplo 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
Exemplo 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))
Exemplo 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
Exemplo 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)
Exemplo 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)
Exemplo 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
Exemplo 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
Exemplo 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
Exemplo 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
Exemplo 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)
Exemplo 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)
Exemplo 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)
Exemplo 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)
Exemplo 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)
Exemplo 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
Exemplo 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
Exemplo 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
Exemplo 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])
Exemplo 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])