Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add recolor to make Pop Art #108

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions wand/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ class MagickPixelPacket(ctypes.Structure):
ctypes.POINTER(ctypes.c_size_t)]
library.PixelGetNextIteratorRow.restype = ctypes.POINTER(ctypes.c_void_p)

library.PixelSyncIterator.argtypes = [ctypes.c_void_p]
library.PixelSyncIterator.restype = ctypes.POINTER(ctypes.c_void_p)

library.NewPixelWand.argtypes = []
library.NewPixelWand.restype = ctypes.c_void_p

Expand Down
32 changes: 32 additions & 0 deletions wand/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,38 @@ def __getitem__(self, idx):
return self[:, idx]
raise TypeError('unsupported index type: ' + repr(idx))

def recolor(self, color_func):
"""recolor every pixel in the image with color_func.
The color_func returns a wand.color.Color like this:

def color_func(x, y, color):
return color
"""
if not callable(color_func):
raise TypeError('color_func must be a function return a Color object')
img = self.clone()
iter0 = library.NewPixelIterator(self.wand)
iter1 = library.NewPixelIterator(img.wand)
struct_size = ctypes.sizeof(MagickPixelPacket)
check_return_type = False
for y in xrange(self.height):
width = ctypes.c_size_t()
pixels0 = library.PixelGetNextIteratorRow(iter0, ctypes.byref(width))
pixels1 = library.PixelGetNextIteratorRow(iter1, ctypes.byref(width))
for x in xrange(width.value):
pc = pixels0[x]
packet_buffer = ctypes.create_string_buffer(struct_size)
library.PixelGetMagickColor(pc, packet_buffer)
color = Color(raw=packet_buffer)
new_color = color_func(x, y, color)
if not check_return_type:
check_return_type = True
if not isinstance(new_color, Color):
raise TypeError('color_func result must be a wand.color.Color, not ' + repr(new_color))
library.PixelSetColor(pixels1[x], new_color.string)
library.PixelSyncIterator(iter1)
return img

def __eq__(self, other):
if isinstance(other, type(self)):
return self.signature == other.signature
Expand Down
10 changes: 10 additions & 0 deletions wandtests/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,16 @@ def resize():
c.resize(width=100)
assert c.size == (100, 599)

def dummy_color_func(x, y, color):
return color

@tests.test
def test_recolor():
"""Test recolor image for example: Pop Art"""
with Image(filename=asset('mona-lisa.jpg')) as img:
a = img.recolor(color_func=dummy_color_func)
a.save(filename="mona-lisa_recolor.jpg")

@tests.test
def test_gif():
"""Test the gif image resize/crop/rotate"""
Expand Down