How to use the wand.assertions.assert_string function in Wand

To help you get started, we’ve selected a few Wand examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github emcconville / wand / wand / font.py View on Github external
def __new__(cls, path, size=0, color=None, antialias=True,
                stroke_color=None, stroke_width=None):
        assertions.assert_string(path=path)
        assertions.assert_real(size=size)
        if color is None:
            color = Color('black')
        elif isinstance(color, string_type):
            color = Color(color)
        assertions.assert_color(color=color)
        if stroke_color:
            if isinstance(stroke_color, string_type):
                stroke_color = Color(stroke_color)
            assertions.assert_color(stroke_color=stroke_color)
        if stroke_width is not None:
            assertions.assert_real(stroke_width=stroke_width)
        path = text(path)
        return tuple.__new__(cls, (path, size, color, bool(antialias),
                                   stroke_color, stroke_width))
github emcconville / wand / wand / drawing.py View on Github external
def comment(self, message=None):
        """Adds a comment to the vector stream.

        :param message: the comment to set.
        :type message: :class:`basestring`

        .. versionadded:: 0.4.0
        """
        if message is None:
            message = b''
        else:
            assertions.assert_string(message=message)
            message = binary(message)
        library.DrawComment(self.resource, message)
github emcconville / wand / wand / drawing.py View on Github external
def get_font_metrics(self, image, text, multiline=False):
        """Queries font metrics from the given ``text``.

        :param image: the image to be drawn
        :type image: :class:`~wand.image.BaseImage`
        :param text: the text string for get font metrics.
        :type text: :class:`basestring`
        :param multiline: text is multiline or not
        :type multiline: `boolean`

        """
        if not isinstance(image, BaseImage):
            raise TypeError('image must be a wand.image.BaseImage instance,'
                            ' not ' + repr(image))
        assertions.assert_string(text=text)
        if multiline:
            font_metrics_f = library.MagickQueryMultilineFontMetrics
        else:
            font_metrics_f = library.MagickQueryFontMetrics
        if isinstance(text, text_type):
            if self.text_encoding:
                text = text.encode(self.text_encoding)
            else:
                text = binary(text)
        result = font_metrics_f(image.wand, self.resource, text)
        if not result:  # pragma: no cover
            # Error on drawing context
            self.raise_exception()
            # Or error on image canvas
            image.raise_exception()
            # Generate a generic error if ImageMagick couldn't emit one.
github emcconville / wand / wand / drawing.py View on Github external
def text(self, x, y, body):
        """Writes a text ``body`` into (``x``, ``y``).

        :param x: the left offset where to start writing a text
        :type x: :class:`numbers.Integral`
        :param y: the baseline where to start writing text
        :type y: :class:`numbers.Integral`
        :param body: the body string to write
        :type body: :class:`basestring`

        """
        assertions.assert_unsigned_integer(x=x, y=y)
        assertions.assert_string(body=body)
        if not body:
            raise ValueError('body string cannot be empty')
        if isinstance(body, text_type):
            # According to ImageMagick C API docs, we can use only UTF-8
            # at this time, so we do hardcoding here.
            # http://imagemagick.org/api/drawing-wand.php#DrawSetTextEncoding
            if not self.text_encoding:
                self.text_encoding = 'UTF-8'
            body = body.encode(self.text_encoding)
        body_p = ctypes.create_string_buffer(body)
        library.DrawAnnotation(
            self.resource, x, y,
            ctypes.cast(body_p, ctypes.POINTER(ctypes.c_ubyte))
        )
github emcconville / wand / wand / drawing.py View on Github external
can be found in the :const:`COMPOSITE_OPERATORS`
                         list
        :param type: :const:`COMPOSITE_OPERATORS`
        :param left: the column offset of the composited drawing source
        :type left: :class:`numbers.Real`
        :param top: the row offset of the composited drawing source
        :type top: :class:`numbers.Real`
        :param width: the total columns to include in the composited source
        :type width: :class:`numbers.Real`
        :param height: the total rows to include in the composited source
        :type height: :class:`numbers.Real`

        .. versionadded:: 0.4.0

        """
        assertions.assert_string(operator=operator)
        assertions.assert_real(left=left, top=top, width=width, height=height)
        try:
            op = COMPOSITE_OPERATORS.index(operator)
        except IndexError:
            raise IndexError(repr(operator) + ' is an invalid composite '
                             'operator type; see wand.image.COMPOSITE_'
                             'OPERATORS dictionary')
        okay = library.DrawComposite(self.resource, op, left, top, width,
                                     height, image.wand)
        if okay == 0:
            self.raise_exception()
github emcconville / wand / wand / drawing.py View on Github external
def font(self, font):
        assertions.assert_string(font=font)
        library.DrawSetFont(self.resource, binary(font))
github emcconville / wand / wand / drawing.py View on Github external
def text_encoding(self, encoding):
        if encoding is None:
            # encoding specify an empty string to set text encoding
            # to system's default.
            encoding = b''
        else:
            assertions.assert_string(text_encoding=encoding)
            encoding = binary(encoding)
        library.DrawSetTextEncoding(self.resource, encoding)
github emcconville / wand / wand / drawing.py View on Github external
:type pattern_id: :class:`basestring`
        :param left: x ordinate of top left corner.
        :type left: :class:`numbers.Real`
        :param top: y ordinate of top left corner.
        :type top: :class:`numbers.Real`
        :param width: width of pattern space.
        :type width: :class:`numbers.Real`
        :param height: height of pattern space.
        :type height: :class:`numbers.Real`
        :returns: success of push operation
        :rtype: `bool`

        .. versionadded:: 0.4.0

        """
        assertions.assert_string(pattern_id=pattern_id)
        assertions.assert_real(left=left, top=top, width=width, height=height)
        okay = library.DrawPushPattern(self.resource, binary(pattern_id),
                                       left, top,
                                       width, height)
        return bool(okay)
github emcconville / wand / wand / drawing.py View on Github external
def set_stroke_pattern_url(self, url):
        """Sets the pattern used for stroking object outlines. Only local
        URLs ("#identifier") are supported at this time. These local URLs are
        normally created by defining a named stroke pattern with
        Drawing.push_pattern & Drawing.pop_pattern.

        :param url: URL to use to obtain stroke pattern.
        :type url: :class:`basestring`

        .. versionadded:: 0.4.0

        """
        assertions.assert_string(url=url)
        if url[0] != '#':
            raise ValueError('value not a relative URL, '
                             'expecting "#identifier"')
        okay = library.DrawSetStrokePatternURL(self.resource, binary(url))
        if okay == 0:
            # ThrowDrawException(DrawError,"URLNotFound",fill_url)
            self.raise_exception()