tests for supported modes

This commit is contained in:
Alexander 2019-11-26 03:36:58 +03:00
parent 7e978ae542
commit 8b6ad4a471
3 changed files with 72 additions and 3 deletions

View File

@ -0,0 +1,61 @@
from PIL import Image
from .helper import PillowTestCase, hopper
class TestImageReduce(PillowTestCase):
# There are several internal implementations
remarkable_factors = [
1, 2, 3, 4, 5, 6, # special implementations
(1, 2), (1, 3), (1, 4), # 1xN implementation
(2, 1), (3, 1), (4, 1), # Nx1 implementation
# general implementation with different paths
(4, 6), (5, 6), (4, 7), (5, 7),
]
def test_args(self):
im = Image.new('L', (10, 10))
self.assertEqual((4, 4), im.reduce(3).size)
self.assertEqual((4, 10), im.reduce((3, 1)).size)
self.assertEqual((10, 4), im.reduce((1, 3)).size)
with self.assertRaises(ValueError):
im.reduce(0)
with self.assertRaises(TypeError):
im.reduce(2.0)
with self.assertRaises(ValueError):
im.reduce((0, 10))
def check_correctness(self, im, factor):
if not isinstance(factor, (list, tuple)):
factor = (factor, factor)
# Image.reduce() should works very similar to Image.resize(BOX)
# when image size is dividable by the factor.
desired_size = (im.width // factor[0], im.height // factor[1])
im = im.crop((0, 0, desired_size[0] * factor[0], desired_size[1] * factor[1]))
reduced = im.reduce(factor)
resized = im.resize(desired_size, Image.BOX)
epsilon = 0.5 * len(reduced.getbands())
self.assert_image_similar(reduced, resized, epsilon)
def test_mode_RGB(self):
im = hopper('RGB')
for factor in self.remarkable_factors:
self.check_correctness(im, factor)
def test_mode_LA(self):
im = Image.open("Tests/images/transparent.png").convert('LA')
for factor in self.remarkable_factors:
print(factor)
self.check_correctness(im, factor)
def test_mode_RGBA(self):
im = Image.open("Tests/images/transparent.png").convert('RGBA')
for factor in self.remarkable_factors:
print(factor)
self.check_correctness(im, factor)

View File

@ -1899,6 +1899,14 @@ class Image(object):
if not isinstance(factor, (list, tuple)):
factor = (factor, factor)
if factor == (1, 1):
return self.copy()
if self.mode in ["LA", "RGBA"]:
im = self.convert(self.mode[:-1] + "a")
im = im.reduce(factor)
return im.convert(self.mode)
self.load()
return self._new(self.im.reduce(factor))

View File

@ -365,7 +365,7 @@ ImagingReduceNx1(Imaging imOut, Imaging imIn, int xscale)
void
ImagingReduce2x2(Imaging imOut, Imaging imIn)
{
/* Fast special case for xscale = 2 and yscale = 2.
/* Optimized implementation for xscale = 2 and yscale = 2.
*/
int xscale = 2, yscale = 2;
int x, y;
@ -435,7 +435,7 @@ ImagingReduce2x2(Imaging imOut, Imaging imIn)
void
ImagingReduce3x3(Imaging imOut, Imaging imIn)
{
/* Fast special case for xscale = 3 and yscale = 3.
/* Optimized implementation for xscale = 3 and yscale = 3.
*/
int xscale = 3, yscale = 3;
int x, y;
@ -519,7 +519,7 @@ ImagingReduce3x3(Imaging imOut, Imaging imIn)
void
ImagingReduce4x4(Imaging imOut, Imaging imIn)
{
/* Fast special case for xscale = 4 and yscale = 4.
/* Optimized implementation for xscale = 4 and yscale = 4.
*/
int xscale = 4, yscale = 4;
int x, y;