def map(image, lookup, interpolation): # @ReservedAssignment if not interpolation: interpolation = 'average' interpolation = enum_lookup(interpolation, interpolations) c_call(image, 'clut', lookup, interpolation)
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)
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))
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)
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)
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))
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')
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)
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)
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)
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))
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)
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
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
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')
def shutdown(): logger.debug('Cleaning up traced instances') _cleanup() c_call(None, 'terminus') if jython: from java.lang import System # @UnresolvedImport System.exit(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')
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)
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())
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)
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)
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))
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
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)
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
def _get_state(self, key, enum=None): value = c_call(self, ("get", key)) if enum: value = enum_reverse_lookup(enum, value) return value
def _get_state(self, key, enum=None): value = c_call(self, ('get', key)) if enum: value = enum_reverse_lookup(enum, value) return value
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)
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)
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)
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)
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)
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
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
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])
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])