mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 01:46:18 +03:00
Merge pull request #3377 from radarhere/color_diff
Added support to floodfill for non-RGB colors
This commit is contained in:
commit
f29881fd60
BIN
Tests/images/imagedraw_floodfill_L.png
Normal file
BIN
Tests/images/imagedraw_floodfill_L.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 144 B |
Before Width: | Height: | Size: 232 B After Width: | Height: | Size: 232 B |
BIN
Tests/images/imagedraw_floodfill_RGBA.png
Normal file
BIN
Tests/images/imagedraw_floodfill_RGBA.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 253 B |
|
@ -48,7 +48,7 @@ class TestImageChops(PillowTestCase):
|
|||
def test_add(self):
|
||||
# Arrange
|
||||
im1 = Image.open("Tests/images/imagedraw_ellipse_RGB.png")
|
||||
im2 = Image.open("Tests/images/imagedraw_floodfill.png")
|
||||
im2 = Image.open("Tests/images/imagedraw_floodfill_RGB.png")
|
||||
|
||||
# Act
|
||||
new = ImageChops.add(im1, im2)
|
||||
|
@ -60,7 +60,7 @@ class TestImageChops(PillowTestCase):
|
|||
def test_add_scale_offset(self):
|
||||
# Arrange
|
||||
im1 = Image.open("Tests/images/imagedraw_ellipse_RGB.png")
|
||||
im2 = Image.open("Tests/images/imagedraw_floodfill.png")
|
||||
im2 = Image.open("Tests/images/imagedraw_floodfill_RGB.png")
|
||||
|
||||
# Act
|
||||
new = ImageChops.add(im1, im2, scale=2.5, offset=100)
|
||||
|
@ -82,7 +82,7 @@ class TestImageChops(PillowTestCase):
|
|||
def test_add_modulo(self):
|
||||
# Arrange
|
||||
im1 = Image.open("Tests/images/imagedraw_ellipse_RGB.png")
|
||||
im2 = Image.open("Tests/images/imagedraw_floodfill.png")
|
||||
im2 = Image.open("Tests/images/imagedraw_floodfill_RGB.png")
|
||||
|
||||
# Act
|
||||
new = ImageChops.add_modulo(im1, im2)
|
||||
|
@ -104,7 +104,7 @@ class TestImageChops(PillowTestCase):
|
|||
def test_blend(self):
|
||||
# Arrange
|
||||
im1 = Image.open("Tests/images/imagedraw_ellipse_RGB.png")
|
||||
im2 = Image.open("Tests/images/imagedraw_floodfill.png")
|
||||
im2 = Image.open("Tests/images/imagedraw_floodfill_RGB.png")
|
||||
|
||||
# Act
|
||||
new = ImageChops.blend(im1, im2, 0.5)
|
||||
|
@ -181,7 +181,7 @@ class TestImageChops(PillowTestCase):
|
|||
|
||||
def test_invert(self):
|
||||
# Arrange
|
||||
im = Image.open("Tests/images/imagedraw_floodfill.png")
|
||||
im = Image.open("Tests/images/imagedraw_floodfill_RGB.png")
|
||||
|
||||
# Act
|
||||
new = ImageChops.invert(im)
|
||||
|
@ -228,7 +228,7 @@ class TestImageChops(PillowTestCase):
|
|||
|
||||
def test_multiply_green(self):
|
||||
# Arrange
|
||||
im = Image.open("Tests/images/imagedraw_floodfill.png")
|
||||
im = Image.open("Tests/images/imagedraw_floodfill_RGB.png")
|
||||
green = Image.new("RGB", im.size, "green")
|
||||
|
||||
# Act
|
||||
|
@ -273,7 +273,7 @@ class TestImageChops(PillowTestCase):
|
|||
def test_screen(self):
|
||||
# Arrange
|
||||
im1 = Image.open("Tests/images/imagedraw_ellipse_RGB.png")
|
||||
im2 = Image.open("Tests/images/imagedraw_floodfill.png")
|
||||
im2 = Image.open("Tests/images/imagedraw_floodfill_RGB.png")
|
||||
|
||||
# Act
|
||||
new = ImageChops.screen(im1, im2)
|
||||
|
|
|
@ -344,19 +344,26 @@ class TestImageDraw(PillowTestCase):
|
|||
self.assert_image_similar(im, Image.open(expected), 1)
|
||||
|
||||
def test_floodfill(self):
|
||||
# Arrange
|
||||
im = Image.new("RGB", (W, H))
|
||||
draw = ImageDraw.Draw(im)
|
||||
draw.rectangle(BBOX2, outline="yellow", fill="green")
|
||||
centre_point = (int(W/2), int(H/2))
|
||||
red = ImageColor.getrgb("red")
|
||||
im_floodfill = Image.open("Tests/images/imagedraw_floodfill.png")
|
||||
|
||||
# Act
|
||||
ImageDraw.floodfill(im, centre_point, red)
|
||||
for mode, value in [
|
||||
("L", 1),
|
||||
("RGBA", (255, 0, 0, 0)),
|
||||
("RGB", red)
|
||||
]:
|
||||
# Arrange
|
||||
im = Image.new(mode, (W, H))
|
||||
draw = ImageDraw.Draw(im)
|
||||
draw.rectangle(BBOX2, outline="yellow", fill="green")
|
||||
centre_point = (int(W/2), int(H/2))
|
||||
|
||||
# Assert
|
||||
self.assert_image_equal(im, im_floodfill)
|
||||
# Act
|
||||
ImageDraw.floodfill(im, centre_point, value)
|
||||
|
||||
# Assert
|
||||
expected = "Tests/images/imagedraw_floodfill_"+mode+".png"
|
||||
im_floodfill = Image.open(expected)
|
||||
self.assert_image_equal(im, im_floodfill)
|
||||
|
||||
# Test that using the same colour does not change the image
|
||||
ImageDraw.floodfill(im, centre_point, red)
|
||||
|
|
|
@ -430,8 +430,11 @@ def floodfill(image, xy, value, border=None, thresh=0):
|
|||
edge = new_edge
|
||||
|
||||
|
||||
def _color_diff(rgb1, rgb2):
|
||||
def _color_diff(color1, color2):
|
||||
"""
|
||||
Uses 1-norm distance to calculate difference between two rgb values.
|
||||
Uses 1-norm distance to calculate difference between two values.
|
||||
"""
|
||||
return abs(rgb1[0]-rgb2[0]) + abs(rgb1[1]-rgb2[1]) + abs(rgb1[2]-rgb2[2])
|
||||
if isinstance(color2, tuple):
|
||||
return sum([abs(color1[i]-color2[i]) for i in range(0, len(color2))])
|
||||
else:
|
||||
return abs(color1-color2)
|
||||
|
|
Loading…
Reference in New Issue
Block a user