mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-26 07:04:45 +03:00
Added Image average_difference
This commit is contained in:
parent
1e56ed8c00
commit
c1fefec997
|
@ -33,19 +33,6 @@ else:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
def convert_to_comparable(a, b):
|
|
||||||
new_a, new_b = a, b
|
|
||||||
if a.mode == 'P':
|
|
||||||
new_a = Image.new('L', a.size)
|
|
||||||
new_b = Image.new('L', b.size)
|
|
||||||
new_a.putdata(a.getdata())
|
|
||||||
new_b.putdata(b.getdata())
|
|
||||||
elif a.mode == 'I;16':
|
|
||||||
new_a = a.convert('I')
|
|
||||||
new_b = b.convert('I')
|
|
||||||
return new_a, new_b
|
|
||||||
|
|
||||||
|
|
||||||
class PillowTestCase(unittest.TestCase):
|
class PillowTestCase(unittest.TestCase):
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
@ -130,14 +117,7 @@ class PillowTestCase(unittest.TestCase):
|
||||||
a.size, b.size,
|
a.size, b.size,
|
||||||
msg or "got size %r, expected %r" % (a.size, b.size))
|
msg or "got size %r, expected %r" % (a.size, b.size))
|
||||||
|
|
||||||
a, b = convert_to_comparable(a, b)
|
ave_diff = a.average_difference(b)
|
||||||
|
|
||||||
diff = 0
|
|
||||||
for ach, bch in zip(a.split(), b.split()):
|
|
||||||
chdiff = ImageMath.eval("abs(a - b)", a=ach, b=bch).convert('L')
|
|
||||||
diff += sum(i * num for i, num in enumerate(chdiff.histogram()))
|
|
||||||
|
|
||||||
ave_diff = float(diff)/(a.size[0]*a.size[1])
|
|
||||||
try:
|
try:
|
||||||
self.assertGreaterEqual(
|
self.assertGreaterEqual(
|
||||||
epsilon, ave_diff,
|
epsilon, ave_diff,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
from helper import unittest, PillowTestCase, hopper
|
from helper import unittest, PillowTestCase, hopper
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image, ImageFilter
|
||||||
from PIL._util import py3
|
from PIL._util import py3
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
@ -492,6 +492,27 @@ class TestImage(PillowTestCase):
|
||||||
|
|
||||||
self.assertEqual(ext_individual, ext_multiple)
|
self.assertEqual(ext_individual, ext_multiple)
|
||||||
|
|
||||||
|
def test_average_difference(self):
|
||||||
|
for mode_a, mode_b in [
|
||||||
|
("RGB", "RGBA"),
|
||||||
|
("P", "RGB"),
|
||||||
|
("I;16", "RGB")
|
||||||
|
]:
|
||||||
|
im_a = hopper(mode_a)
|
||||||
|
im_b = hopper(mode_b)
|
||||||
|
self.assertEqual(im_a.average_difference(im_b), 0)
|
||||||
|
|
||||||
|
im = hopper()
|
||||||
|
im_resized = im.resize((im.width * 2, im.height * 2))
|
||||||
|
diff = im.average_difference(im_resized)
|
||||||
|
self.assertEqual(diff, 0)
|
||||||
|
self.assertEqual(im_resized.average_difference(im), diff)
|
||||||
|
|
||||||
|
im_blur = im.filter(ImageFilter.BLUR)
|
||||||
|
diff = im.average_difference(im_blur)
|
||||||
|
self.assertNotEqual(diff, 0)
|
||||||
|
self.assertEqual(im_blur.average_difference(im), diff)
|
||||||
|
|
||||||
def test_remap_palette(self):
|
def test_remap_palette(self):
|
||||||
# Test illegal image mode
|
# Test illegal image mode
|
||||||
im = hopper()
|
im = hopper()
|
||||||
|
|
|
@ -851,6 +851,41 @@ class Image(object):
|
||||||
"""
|
"""
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def average_difference(self, im):
|
||||||
|
"""
|
||||||
|
Returns the average pixel value difference between this and another
|
||||||
|
image
|
||||||
|
|
||||||
|
:param im: Comparison image
|
||||||
|
"""
|
||||||
|
from . import ImageMath
|
||||||
|
|
||||||
|
a, b = self, im
|
||||||
|
if a.mode == 'I;16':
|
||||||
|
a = a.convert('I')
|
||||||
|
if b.mode != a.mode:
|
||||||
|
b = b.convert(a.mode)
|
||||||
|
|
||||||
|
if a.mode == 'P':
|
||||||
|
a_data = a.getdata()
|
||||||
|
b_data = b.getdata()
|
||||||
|
a = new('L', a.size)
|
||||||
|
b = new('L', b.size)
|
||||||
|
a.putdata(a_data)
|
||||||
|
b.putdata(b_data)
|
||||||
|
|
||||||
|
if (a.width * a.height) < (b.width * b.height):
|
||||||
|
b = b.resize(a.size)
|
||||||
|
elif (b.width * b.height) < (a.width * a.height):
|
||||||
|
a = a.resize(b.size)
|
||||||
|
|
||||||
|
diff = 0
|
||||||
|
for ach, bch in zip(a.split(), b.split()):
|
||||||
|
chdiff = ImageMath.eval("abs(a - b)", a=ach, b=bch).convert('L')
|
||||||
|
diff += sum(i * num for i, num in enumerate(chdiff.histogram()))
|
||||||
|
|
||||||
|
return float(diff) / (a.size[0] * a.size[1])
|
||||||
|
|
||||||
def convert(self, mode=None, matrix=None, dither=None,
|
def convert(self, mode=None, matrix=None, dither=None,
|
||||||
palette=WEB, colors=256):
|
palette=WEB, colors=256):
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue
Block a user