Skip to content
This repository has been archived by the owner on Jun 30, 2020. It is now read-only.

Commit

Permalink
added support to merge two images
Browse files Browse the repository at this point in the history
  • Loading branch information
ojii committed Sep 19, 2012
1 parent 4d35cfb commit fc0b93e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
9 changes: 9 additions & 0 deletions pymaging/image.py
Expand Up @@ -212,3 +212,12 @@ def crop(self, width, height, padding_top, padding_left):
def draw(self, shape, color):
for x, y, pixelcolor in shape.iter_pixels(color):
self.set_color(x, y, pixelcolor)

def merge(self, padding_top, padding_left, image):
"""
Merges the two images, putting the image given on top of this image with with the given padding
"""
# there *must* be a better/faster way to do this:
for x in range(min([image.width, self.width - padding_left])):
for y in range(min([image.height, self.height- padding_top])):
self.set_color(padding_left + x, padding_top + y, image.get_color(x, y))
44 changes: 44 additions & 0 deletions pymaging/tests/test_basic.py
Expand Up @@ -198,3 +198,47 @@ def test_draw_line_steep(self):
[Black, White, Black, Black, Black],
[Black, White, Black, Black, Black],
])

def test_merge_simple(self):
main = image_factory([
[Black, Black, Black, Black, Black],
[Black, Black, Black, Black, Black],
[Black, Black, Black, Black, Black],
[Black, Black, Black, Black, Black],
[Black, Black, Black, Black, Black],
])
other =image_factory([
[White, White, White],
[White, White, White],
[White, White, White],
])
main.merge(1, 1, other)
self.assertImage(main, [
[Black, Black, Black, Black, Black],
[Black, White, White, White, Black],
[Black, White, White, White, Black],
[Black, White, White, White, Black],
[Black, Black, Black, Black, Black],
])

def test_merge_partial(self):
main = image_factory([
[Black, Black, Black, Black, Black],
[Black, Black, Black, Black, Black],
[Black, Black, Black, Black, Black],
[Black, Black, Black, Black, Black],
[Black, Black, Black, Black, Black],
])
other =image_factory([
[White, White, White],
[White, White, White],
[White, White, White],
])
main.merge(3, 3, other)
self.assertImage(main, [
[Black, Black, Black, Black, Black],
[Black, Black, Black, Black, Black],
[Black, Black, Black, Black, Black],
[Black, Black, Black, White, White],
[Black, Black, Black, White, White],
])

0 comments on commit fc0b93e

Please sign in to comment.