Skip to content

Commit

Permalink
Added fuzz option to Image.trim()
Browse files Browse the repository at this point in the history
Thanks to @fabiorphp
  • Loading branch information
InFog committed Jun 6, 2013
1 parent 8ce2025 commit f5f9722
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
3 changes: 2 additions & 1 deletion wand/api.py
Expand Up @@ -450,7 +450,8 @@ class MagickPixelPacket(ctypes.Structure):

library.MagickStripImage.argtypes = [ctypes.c_void_p]

library.MagickTrimImage.argtypes = [ctypes.c_void_p]
library.MagickTrimImage.argtypes = [ctypes.c_void_p,
ctypes.c_double]

library.MagickGetSize.argtypes = [ctypes.c_void_p,
ctypes.POINTER(ctypes.c_uint),
Expand Down
8 changes: 6 additions & 2 deletions wand/image.py
Expand Up @@ -1854,14 +1854,18 @@ def strip(self):
if not result:
self.raise_exception()

def trim(self, color=None):
def trim(self, color=None, fuzz=0):
"""Remove solid border from image. Uses top left pixel as a guide
by default, or you can also specify the ``color`` to remove.
:param color: the border color to remove.
if it's omitted top left pixel is used by default
:type color: :class:`~wand.color.Color`
:param fuzz: Defines how much tolerance is acceptable to consider
two colors as the same.
:type fuzz: :class:`numbers.Integral`
.. versionadded:: 0.2.1
.. versionadded:: 0.3.0
Expand All @@ -1871,7 +1875,7 @@ def trim(self, color=None):
"""
with color or self[0, 0] as color:
self.border(color, 1, 1)
result = library.MagickTrimImage(self.wand)
result = library.MagickTrimImage(self.wand, fuzz)
if not result:
self.raise_exception()

Expand Down
7 changes: 7 additions & 0 deletions wandtests/image.py
Expand Up @@ -382,6 +382,13 @@ def trim():
newx, newy = img.size
assert newx < oldx
assert newy < oldy
with Image(filename=asset('trimtest.png')) as img:
img.trim()
trimx, trimy = img.size
img.trim(fuzz=10000)
fuzzx, fuzzy = img.size
assert fuzzx < trimx
assert fuzzy < trimy
with Image(filename=asset('trim-color-test.png')) as img:
assert img.size == (100, 100)
with Color('blue') as blue:
Expand Down

0 comments on commit f5f9722

Please sign in to comment.