2014-06-10 13:10:47 +04:00
|
|
|
from helper import unittest, PillowTestCase, tearDownModule, lena
|
2012-10-16 00:26:38 +04:00
|
|
|
|
|
|
|
from PIL import Image
|
2014-05-13 00:27:02 +04:00
|
|
|
from PIL import ImageColor
|
2012-10-16 00:26:38 +04:00
|
|
|
from PIL import ImageDraw
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
import sys
|
|
|
|
|
2014-05-12 18:32:04 +04:00
|
|
|
# Image size
|
2014-06-23 23:51:31 +04:00
|
|
|
W, H = 100, 100
|
2014-05-12 18:32:04 +04:00
|
|
|
|
|
|
|
# Bounding box points
|
2014-06-23 23:51:31 +04:00
|
|
|
X0 = int(W / 4)
|
|
|
|
X1 = int(X0 * 3)
|
|
|
|
Y0 = int(H / 4)
|
|
|
|
Y1 = int(X0 * 3)
|
2014-05-12 18:32:04 +04:00
|
|
|
|
|
|
|
# Two kinds of bounding box
|
2014-06-23 23:51:31 +04:00
|
|
|
BBOX1 = [(X0, Y0), (X1, Y1)]
|
|
|
|
BBOX2 = [X0, Y0, X1, Y1]
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2014-05-12 22:45:16 +04:00
|
|
|
# Two kinds of coordinate sequences
|
2014-06-23 23:51:31 +04:00
|
|
|
POINTS1 = [(10, 10), (20, 40), (30, 30)]
|
|
|
|
POINTS2 = [10, 10, 20, 40, 30, 30]
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestImageDraw(PillowTestCase):
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_sanity(self):
|
|
|
|
im = lena("RGB").copy()
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
draw = ImageDraw.ImageDraw(im)
|
|
|
|
draw = ImageDraw.Draw(im)
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
draw.ellipse(list(range(4)))
|
|
|
|
draw.line(list(range(10)))
|
|
|
|
draw.polygon(list(range(100)))
|
|
|
|
draw.rectangle(list(range(4)))
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_deprecated(self):
|
|
|
|
im = lena().copy()
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
draw = ImageDraw.Draw(im)
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self.assert_warning(DeprecationWarning, lambda: draw.setink(0))
|
|
|
|
self.assert_warning(DeprecationWarning, lambda: draw.setfill(0))
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def helper_arc(self, bbox):
|
|
|
|
# Arrange
|
2014-06-23 23:51:31 +04:00
|
|
|
im = Image.new("RGB", (W, H))
|
2014-06-10 13:10:47 +04:00
|
|
|
draw = ImageDraw.Draw(im)
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
|
|
|
# FIXME Fill param should be named outline.
|
|
|
|
draw.arc(bbox, 0, 180)
|
|
|
|
del draw
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
|
|
|
self.assert_image_equal(
|
|
|
|
im, Image.open("Tests/images/imagedraw_arc.png"))
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_arc1(self):
|
2014-06-23 23:51:31 +04:00
|
|
|
self.helper_arc(BBOX1)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_arc2(self):
|
2014-06-23 23:51:31 +04:00
|
|
|
self.helper_arc(BBOX2)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_bitmap(self):
|
|
|
|
# Arrange
|
|
|
|
small = Image.open("Tests/images/pil123rgba.png").resize((50, 50))
|
2014-06-23 23:51:31 +04:00
|
|
|
im = Image.new("RGB", (W, H))
|
2014-06-10 13:10:47 +04:00
|
|
|
draw = ImageDraw.Draw(im)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
|
|
|
draw.bitmap((10, 10), small)
|
|
|
|
del draw
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
|
|
|
self.assert_image_equal(
|
|
|
|
im, Image.open("Tests/images/imagedraw_bitmap.png"))
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def helper_chord(self, bbox):
|
|
|
|
# Arrange
|
2014-06-23 23:51:31 +04:00
|
|
|
im = Image.new("RGB", (W, H))
|
2014-06-10 13:10:47 +04:00
|
|
|
draw = ImageDraw.Draw(im)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
|
|
|
draw.chord(bbox, 0, 180, fill="red", outline="yellow")
|
|
|
|
del draw
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
|
|
|
self.assert_image_equal(
|
|
|
|
im, Image.open("Tests/images/imagedraw_chord.png"))
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_chord1(self):
|
2014-06-23 23:51:31 +04:00
|
|
|
self.helper_chord(BBOX1)
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_chord2(self):
|
2014-06-23 23:51:31 +04:00
|
|
|
self.helper_chord(BBOX2)
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def helper_ellipse(self, bbox):
|
|
|
|
# Arrange
|
2014-06-23 23:51:31 +04:00
|
|
|
im = Image.new("RGB", (W, H))
|
2014-06-10 13:10:47 +04:00
|
|
|
draw = ImageDraw.Draw(im)
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
|
|
|
draw.ellipse(bbox, fill="green", outline="blue")
|
|
|
|
del draw
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
|
|
|
self.assert_image_equal(
|
|
|
|
im, Image.open("Tests/images/imagedraw_ellipse.png"))
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_ellipse1(self):
|
2014-06-23 23:51:31 +04:00
|
|
|
self.helper_ellipse(BBOX1)
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_ellipse2(self):
|
2014-06-23 23:51:31 +04:00
|
|
|
self.helper_ellipse(BBOX2)
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def helper_line(self, points):
|
|
|
|
# Arrange
|
2014-06-23 23:51:31 +04:00
|
|
|
im = Image.new("RGB", (W, H))
|
2014-06-10 13:10:47 +04:00
|
|
|
draw = ImageDraw.Draw(im)
|
2014-05-12 18:32:04 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
2014-06-23 22:07:09 +04:00
|
|
|
draw.line(points, fill="yellow", width=2)
|
2014-06-10 13:10:47 +04:00
|
|
|
del draw
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
|
|
|
self.assert_image_equal(
|
|
|
|
im, Image.open("Tests/images/imagedraw_line.png"))
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_line1(self):
|
2014-06-23 23:51:31 +04:00
|
|
|
self.helper_line(POINTS1)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_line2(self):
|
2014-06-23 23:51:31 +04:00
|
|
|
self.helper_line(POINTS2)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def helper_pieslice(self, bbox):
|
|
|
|
# Arrange
|
2014-06-23 23:51:31 +04:00
|
|
|
im = Image.new("RGB", (W, H))
|
2014-06-10 13:10:47 +04:00
|
|
|
draw = ImageDraw.Draw(im)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
|
|
|
draw.pieslice(bbox, -90, 45, fill="white", outline="blue")
|
|
|
|
del draw
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
|
|
|
self.assert_image_equal(
|
|
|
|
im, Image.open("Tests/images/imagedraw_pieslice.png"))
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_pieslice1(self):
|
2014-06-23 23:51:31 +04:00
|
|
|
self.helper_pieslice(BBOX1)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_pieslice2(self):
|
2014-06-23 23:51:31 +04:00
|
|
|
self.helper_pieslice(BBOX2)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def helper_point(self, points):
|
|
|
|
# Arrange
|
2014-06-23 23:51:31 +04:00
|
|
|
im = Image.new("RGB", (W, H))
|
2014-06-10 13:10:47 +04:00
|
|
|
draw = ImageDraw.Draw(im)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
2014-06-23 22:07:09 +04:00
|
|
|
draw.point(points, fill="yellow")
|
2014-06-10 13:10:47 +04:00
|
|
|
del draw
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
|
|
|
self.assert_image_equal(
|
|
|
|
im, Image.open("Tests/images/imagedraw_point.png"))
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_point1(self):
|
2014-06-23 23:51:31 +04:00
|
|
|
self.helper_point(POINTS1)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_point2(self):
|
2014-06-23 23:51:31 +04:00
|
|
|
self.helper_point(POINTS2)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def helper_polygon(self, points):
|
|
|
|
# Arrange
|
2014-06-23 23:51:31 +04:00
|
|
|
im = Image.new("RGB", (W, H))
|
2014-06-10 13:10:47 +04:00
|
|
|
draw = ImageDraw.Draw(im)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
2014-06-23 22:07:09 +04:00
|
|
|
draw.polygon(points, fill="red", outline="blue")
|
2014-06-10 13:10:47 +04:00
|
|
|
del draw
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
|
|
|
self.assert_image_equal(
|
|
|
|
im, Image.open("Tests/images/imagedraw_polygon.png"))
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_polygon1(self):
|
2014-06-23 23:51:31 +04:00
|
|
|
self.helper_polygon(POINTS1)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_polygon2(self):
|
2014-06-23 23:51:31 +04:00
|
|
|
self.helper_polygon(POINTS2)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def helper_rectangle(self, bbox):
|
|
|
|
# Arrange
|
2014-06-23 23:51:31 +04:00
|
|
|
im = Image.new("RGB", (W, H))
|
2014-06-10 13:10:47 +04:00
|
|
|
draw = ImageDraw.Draw(im)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
|
|
|
draw.rectangle(bbox, fill="black", outline="green")
|
|
|
|
del draw
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
|
|
|
self.assert_image_equal(
|
|
|
|
im, Image.open("Tests/images/imagedraw_rectangle.png"))
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_rectangle1(self):
|
2014-06-23 23:51:31 +04:00
|
|
|
self.helper_rectangle(BBOX1)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_rectangle2(self):
|
2014-06-23 23:51:31 +04:00
|
|
|
self.helper_rectangle(BBOX2)
|
2014-05-12 22:45:16 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
def test_floodfill(self):
|
|
|
|
# Arrange
|
2014-06-23 23:51:31 +04:00
|
|
|
im = Image.new("RGB", (W, H))
|
2014-06-10 13:10:47 +04:00
|
|
|
draw = ImageDraw.Draw(im)
|
2014-06-23 23:51:31 +04:00
|
|
|
draw.rectangle(BBOX2, outline="yellow", fill="green")
|
|
|
|
centre_point = (int(W/2), int(H/2))
|
2014-05-13 00:27:02 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
|
|
|
ImageDraw.floodfill(im, centre_point, ImageColor.getrgb("red"))
|
|
|
|
del draw
|
2014-05-13 00:27:02 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
|
|
|
self.assert_image_equal(
|
|
|
|
im, Image.open("Tests/images/imagedraw_floodfill.png"))
|
2014-05-13 00:27:02 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
@unittest.skipIf(hasattr(sys, 'pypy_version_info'),
|
|
|
|
"Causes fatal RPython error on PyPy")
|
|
|
|
def test_floodfill_border(self):
|
|
|
|
# floodfill() is experimental
|
2014-05-13 00:27:02 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Arrange
|
2014-06-23 23:51:31 +04:00
|
|
|
im = Image.new("RGB", (W, H))
|
2014-06-10 13:10:47 +04:00
|
|
|
draw = ImageDraw.Draw(im)
|
2014-06-23 23:51:31 +04:00
|
|
|
draw.rectangle(BBOX2, outline="yellow", fill="green")
|
|
|
|
centre_point = (int(W/2), int(H/2))
|
2014-06-06 09:02:18 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Act
|
|
|
|
ImageDraw.floodfill(
|
|
|
|
im, centre_point, ImageColor.getrgb("red"),
|
|
|
|
border=ImageColor.getrgb("black"))
|
|
|
|
del draw
|
2014-05-13 00:33:05 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Assert
|
|
|
|
self.assert_image_equal(
|
|
|
|
im, Image.open("Tests/images/imagedraw_floodfill2.png"))
|
2014-05-13 00:33:05 +04:00
|
|
|
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
2014-05-13 00:33:05 +04:00
|
|
|
|
2014-05-12 18:32:04 +04:00
|
|
|
# End of file
|