mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-11-04 01:47:47 +03:00 
			
		
		
		
	Merge pull request #157 from wiredfool/webp_tests
approximate test for lossy image writing
This commit is contained in:
		
						commit
						8244203366
					
				| 
						 | 
					@ -2,7 +2,7 @@ from tester import *
 | 
				
			||||||
 | 
					
 | 
				
			||||||
from PIL import Image
 | 
					from PIL import Image
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_read():
 | 
					def xtest_read():
 | 
				
			||||||
    """ Can we write a webp without error. Does it have the bits we expect?"""
 | 
					    """ Can we write a webp without error. Does it have the bits we expect?"""
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
    file = "Images/lena.webp"
 | 
					    file = "Images/lena.webp"
 | 
				
			||||||
| 
						 | 
					@ -37,7 +37,18 @@ def test_write():
 | 
				
			||||||
    assert_no_exception(lambda: im.load())
 | 
					    assert_no_exception(lambda: im.load())
 | 
				
			||||||
    assert_no_exception(lambda: im.getdata())
 | 
					    assert_no_exception(lambda: im.getdata())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # If we're using the exact same version of webp, this test should pass.
 | 
				
			||||||
 | 
					    # but it doesn't if the webp is generated on Ubuntu and tested on Fedora.
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
    # generated with: dwebp -ppm temp.webp -o lena_webp_write.ppm
 | 
					    # generated with: dwebp -ppm temp.webp -o lena_webp_write.ppm
 | 
				
			||||||
    target = Image.open('Tests/images/lena_webp_write.ppm')
 | 
					    #target = Image.open('Tests/images/lena_webp_write.ppm')
 | 
				
			||||||
    assert_image_equal(im, target)
 | 
					    #assert_image_equal(im, target)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # This test asserts that the images are similar. If the average pixel difference
 | 
				
			||||||
 | 
					    # between the two images is less than the epsilon value, then we're going to
 | 
				
			||||||
 | 
					    # accept that it's a reasonable lossy version of the image. The included lena images
 | 
				
			||||||
 | 
					    # for webp are showing ~16 on Ubuntu, the jpegs are showing ~18. 
 | 
				
			||||||
 | 
					    target = lena('RGB')
 | 
				
			||||||
 | 
					    assert_image_similar(im, target, 20.0)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -150,6 +150,26 @@ def assert_image_equal(a, b, msg=None):
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        success()
 | 
					        success()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def assert_image_similar(a, b, epsilon, msg=None):
 | 
				
			||||||
 | 
					    epsilon = float(epsilon)
 | 
				
			||||||
 | 
					    if a.mode != b.mode:
 | 
				
			||||||
 | 
					        return failure(msg or "got mode %r, expected %r" % (a.mode, b.mode))
 | 
				
			||||||
 | 
					    elif a.size != b.size:
 | 
				
			||||||
 | 
					        return failure(msg or "got size %r, expected %r" % (a.size, b.size))
 | 
				
			||||||
 | 
					    diff = 0
 | 
				
			||||||
 | 
					    try:
 | 
				
			||||||
 | 
					        ord(b'0')
 | 
				
			||||||
 | 
					        for abyte,bbyte in zip(a.tobytes(),b.tobytes()):
 | 
				
			||||||
 | 
					            diff += abs(ord(abyte)-ord(bbyte))
 | 
				
			||||||
 | 
					    except:
 | 
				
			||||||
 | 
					        for abyte,bbyte in zip(a.tobytes(),b.tobytes()):
 | 
				
			||||||
 | 
					            diff += abs(abyte-bbyte)
 | 
				
			||||||
 | 
					    ave_diff = float(diff)/(a.size[0]*a.size[1])
 | 
				
			||||||
 | 
					    if epsilon < ave_diff:
 | 
				
			||||||
 | 
					        failure(msg or "average pixel value difference %.4f > epsilon %.4f" %(ave_diff, epsilon))
 | 
				
			||||||
 | 
					    else:
 | 
				
			||||||
 | 
					        success()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def tempfile(template, *extra):
 | 
					def tempfile(template, *extra):
 | 
				
			||||||
    import os, sys
 | 
					    import os, sys
 | 
				
			||||||
    files = []
 | 
					    files = []
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user