Merge pull request #1772 from radarhere/copy

Added __copy__ method to Image
This commit is contained in:
wiredfool 2016-04-01 03:35:21 -07:00
commit 39fb128898
2 changed files with 27 additions and 4 deletions

View File

@ -1012,6 +1012,8 @@ class Image(object):
im = self.im.copy() im = self.im.copy()
return self._new(im) return self._new(im)
__copy__ = copy
def crop(self, box=None): def crop(self, box=None):
""" """
Returns a rectangular region from this image. The box is a Returns a rectangular region from this image. The box is a

View File

@ -1,16 +1,37 @@
from helper import unittest, PillowTestCase, hopper from helper import unittest, PillowTestCase, hopper
import copy
class TestImageCopy(PillowTestCase): class TestImageCopy(PillowTestCase):
def test_copy(self): def test_copy(self):
def copy(mode): croppedCoordinates = (10, 10, 20, 20)
croppedSize = (10, 10)
for mode in "1", "P", "L", "RGB", "I", "F":
# Internal copy method
im = hopper(mode) im = hopper(mode)
out = im.copy() out = im.copy()
self.assertEqual(out.mode, mode) self.assertEqual(out.mode, im.mode)
self.assertEqual(out.size, im.size) self.assertEqual(out.size, im.size)
for mode in "1", "P", "L", "RGB", "I", "F":
copy(mode) # Python's copy method
im = hopper(mode)
out = copy.copy(im)
self.assertEqual(out.mode, im.mode)
self.assertEqual(out.size, im.size)
# Internal copy method on a cropped image
im = hopper(mode)
out = im.crop(croppedCoordinates).copy()
self.assertEqual(out.mode, im.mode)
self.assertEqual(out.size, croppedSize)
# Python's copy method on a cropped image
im = hopper(mode)
out = copy.copy(im.crop(croppedCoordinates))
self.assertEqual(out.mode, im.mode)
self.assertEqual(out.size, croppedSize)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()