Skip to content

Commit

Permalink
Merge pull request #98 from lkoenig/master
Browse files Browse the repository at this point in the history
Adding method for painting a color in transparent
  • Loading branch information
dahlia committed Mar 14, 2013
2 parents 92f3d51 + 495a26c commit 7ecfe3f
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions wand/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ class MagickPixelPacket(ctypes.Structure):
ctypes.c_char_p]
library.MagickTransformImage.restype = ctypes.c_void_p

library.MagickTransparentPaintImage.argtypes = [ctypes.c_void_p, ctypes.c_void_p,
ctypes.c_double, ctypes.c_double,
ctypes.c_int]

library.MagickLiquidRescaleImage.argtypes = [
ctypes.c_void_p, ctypes.c_size_t, ctypes.c_size_t,
ctypes.c_double, ctypes.c_double
Expand Down
34 changes: 34 additions & 0 deletions wand/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,40 @@ def transparentize(self, transparency):
t)
self.raise_exception()

def transparent_color(self, color, alpha, fuzz = 0, invert=False):
"""Makes the color ```color`` a transparent color with a tolerance of
fuzz. The ``alpha``parameter specify the transparency level and the
parameter ``fuzz`` specify the tolerance.
:param color: The color that should be made transparent on the image,
color object
:type color: :class:`wand.color.Color`
:param alpha: the level of transparency: 1.0 is fully opaque
and 0.0 is fully transparent.
:type alpha: :class:`numbers.Real`
:param fuzz: By default target must match a particular pixel color
exactly. However, in many cases two colors may differ
by a small amount. The fuzz member of image defines how
much tolerance is acceptable to consider two colors as the
same. For example, set fuzz to 10 and the color red at
intensities of 100 and 102 respectively are now interpreted
as the same color for the color.
:type fuzz: :class:`numbers.Integral`
:param invert: Boolean to tell to paint the inverse selection.
:type invert: :class:`bool`
.. versionadded:: 0.2.4
"""
if not isinstance(alpha, numbers.Real):
raise TypeError('alpha must be an float, not ' + repr(left))
elif not isinstance(fuzz, numbers.Integral):
raise TypeError('fuzz must be an integer, not ' + repr(left))
elif not isinstance(color, Color):
raise TypeError('color must be a wand.color.Color object, not ' +
repr(color))
library.MagickTransparentPaintImage(self.wand, color.resource, alpha, fuzz, invert)
self.raise_exception()

def composite(self, image, left, top):
"""Places the supplied ``image`` over the current image, with the top
left corner of ``image`` at coordinates ``left``, ``top`` of the
Expand Down
9 changes: 9 additions & 0 deletions wandtests/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,15 @@ def rotate():
assert black == cloned[2, 70] == cloned[35, 37]
assert black == cloned[85, 88] == cloned[52, 120]

@tests.test
def transparent_color():
"""TransparentPaint test"""
with Image(filename=asset('rotatetest.gif')) as img:
img.alpha_channel = True
with Color('white') as white:
img.transparent_color(white, 0.0, 2, 0)
assert img[75, 50].alpha == 0
assert img[0, 50].alpha == 1.0

@tests.test
def signature():
Expand Down

0 comments on commit 7ecfe3f

Please sign in to comment.