mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-11-04 09:57:43 +03:00 
			
		
		
		
	Add __eq__ and __ne__ to Image to be able to test image equality when pickling. Pickle more data.
This commit is contained in:
		
							parent
							
								
									6c938b784b
								
							
						
					
					
						commit
						2a6f2c5442
					
				
							
								
								
									
										37
									
								
								PIL/Image.py
									
									
									
									
									
								
							
							
						
						
									
										37
									
								
								PIL/Image.py
									
									
									
									
									
								
							| 
						 | 
					@ -563,6 +563,21 @@ class Image:
 | 
				
			||||||
            self.save(file, format)
 | 
					            self.save(file, format)
 | 
				
			||||||
        return file
 | 
					        return file
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def __eq__(self, other):
 | 
				
			||||||
 | 
					        a = (self.mode == other.mode)
 | 
				
			||||||
 | 
					        b = (self.size == other.size)
 | 
				
			||||||
 | 
					        c = (self.getpalette() == other.getpalette())
 | 
				
			||||||
 | 
					        d = (self.info == other.info)
 | 
				
			||||||
 | 
					        e = (self.category == other.category)
 | 
				
			||||||
 | 
					        f = (self.readonly == other.readonly)
 | 
				
			||||||
 | 
					        g = (self.pyaccess == other.pyaccess)
 | 
				
			||||||
 | 
					        h = (self.tobytes() == other.tobytes())
 | 
				
			||||||
 | 
					        return a and b and c and d and e and f and g and h
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def __ne__(self, other):
 | 
				
			||||||
 | 
					        eq = (self == other)
 | 
				
			||||||
 | 
					        return not eq
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __repr__(self):
 | 
					    def __repr__(self):
 | 
				
			||||||
        return "<%s.%s image mode=%s size=%dx%d at 0x%X>" % (
 | 
					        return "<%s.%s image mode=%s size=%dx%d at 0x%X>" % (
 | 
				
			||||||
            self.__class__.__module__, self.__class__.__name__,
 | 
					            self.__class__.__module__, self.__class__.__name__,
 | 
				
			||||||
| 
						 | 
					@ -582,15 +597,23 @@ class Image:
 | 
				
			||||||
        raise AttributeError(name)
 | 
					        raise AttributeError(name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __getstate__(self):
 | 
					    def __getstate__(self):
 | 
				
			||||||
        return [self.mode, self.size, self.tobytes()]
 | 
					        return [
 | 
				
			||||||
 | 
					            self.info,
 | 
				
			||||||
 | 
					            self.mode,
 | 
				
			||||||
 | 
					            self.size,
 | 
				
			||||||
 | 
					            self.getpalette(),
 | 
				
			||||||
 | 
					            self.tobytes()]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __setstate__(self, state):
 | 
					    def __setstate__(self, state):
 | 
				
			||||||
        Image.__init__(self)
 | 
					        Image.__init__(self)
 | 
				
			||||||
        self.tile = []
 | 
					        self.tile = []
 | 
				
			||||||
        mode, size, data = state
 | 
					        info, mode, size, palette, data = state
 | 
				
			||||||
 | 
					        self.info = info
 | 
				
			||||||
        self.mode = mode
 | 
					        self.mode = mode
 | 
				
			||||||
        self.size = size
 | 
					        self.size = size
 | 
				
			||||||
        self.im = core.new(mode, size)
 | 
					        self.im = core.new(mode, size)
 | 
				
			||||||
 | 
					        if mode in ("L", "P"):
 | 
				
			||||||
 | 
					            self.putpalette(palette)
 | 
				
			||||||
        self.frombytes(data)
 | 
					        self.frombytes(data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def tobytes(self, encoder_name="raw", *args):
 | 
					    def tobytes(self, encoder_name="raw", *args):
 | 
				
			||||||
| 
						 | 
					@ -870,7 +893,7 @@ class Image:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        new_im = self._new(im)
 | 
					        new_im = self._new(im)
 | 
				
			||||||
        if delete_trns:
 | 
					        if delete_trns:
 | 
				
			||||||
            #crash fail if we leave a bytes transparency in an rgb/l mode.
 | 
					            # crash fail if we leave a bytes transparency in an rgb/l mode.
 | 
				
			||||||
            del(new_im.info['transparency'])
 | 
					            del(new_im.info['transparency'])
 | 
				
			||||||
        if trns is not None:
 | 
					        if trns is not None:
 | 
				
			||||||
            if new_im.mode == 'P':
 | 
					            if new_im.mode == 'P':
 | 
				
			||||||
| 
						 | 
					@ -2188,8 +2211,8 @@ def open(fp, mode="r"):
 | 
				
			||||||
                fp.seek(0)
 | 
					                fp.seek(0)
 | 
				
			||||||
                return factory(fp, filename)
 | 
					                return factory(fp, filename)
 | 
				
			||||||
        except (SyntaxError, IndexError, TypeError):
 | 
					        except (SyntaxError, IndexError, TypeError):
 | 
				
			||||||
            #import traceback
 | 
					            # import traceback
 | 
				
			||||||
            #traceback.print_exc()
 | 
					            # traceback.print_exc()
 | 
				
			||||||
            pass
 | 
					            pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if init():
 | 
					    if init():
 | 
				
			||||||
| 
						 | 
					@ -2201,8 +2224,8 @@ def open(fp, mode="r"):
 | 
				
			||||||
                    fp.seek(0)
 | 
					                    fp.seek(0)
 | 
				
			||||||
                    return factory(fp, filename)
 | 
					                    return factory(fp, filename)
 | 
				
			||||||
            except (SyntaxError, IndexError, TypeError):
 | 
					            except (SyntaxError, IndexError, TypeError):
 | 
				
			||||||
                #import traceback
 | 
					                # import traceback
 | 
				
			||||||
                #traceback.print_exc()
 | 
					                # traceback.print_exc()
 | 
				
			||||||
                pass
 | 
					                pass
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    raise IOError("cannot identify image file %r"
 | 
					    raise IOError("cannot identify image file %r"
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -3,18 +3,6 @@ from tester import *
 | 
				
			||||||
from PIL import Image
 | 
					from PIL import Image
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def test_frombytes_tobytes():
 | 
					 | 
				
			||||||
    # Arrange
 | 
					 | 
				
			||||||
    im = Image.open('Images/lena.jpg')
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # Act
 | 
					 | 
				
			||||||
    data = im.tobytes()
 | 
					 | 
				
			||||||
    new_im = Image.frombytes(im.mode, im.size, data)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    # Assert
 | 
					 | 
				
			||||||
    assert_image_equal(im, new_im)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def helper_test_pickle_file(pickle, protocol=0):
 | 
					def helper_test_pickle_file(pickle, protocol=0):
 | 
				
			||||||
    im = Image.open('Images/lena.jpg')
 | 
					    im = Image.open('Images/lena.jpg')
 | 
				
			||||||
    filename = tempfile('temp.pkl')
 | 
					    filename = tempfile('temp.pkl')
 | 
				
			||||||
| 
						 | 
					@ -29,8 +17,8 @@ def helper_test_pickle_file(pickle, protocol=0):
 | 
				
			||||||
    assert_image_equal(im, loaded_im)
 | 
					    assert_image_equal(im, loaded_im)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def helper_test_pickle_string(pickle, protocol=0):
 | 
					def helper_test_pickle_string(pickle, protocol=0, file='Images/lena.jpg'):
 | 
				
			||||||
    im = Image.open('Images/lena.jpg')
 | 
					    im = Image.open(file)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    # Act
 | 
					    # Act
 | 
				
			||||||
    dumped_string = pickle.dumps(im, protocol)
 | 
					    dumped_string = pickle.dumps(im, protocol)
 | 
				
			||||||
| 
						 | 
					@ -62,4 +50,21 @@ def test_cpickle_image():
 | 
				
			||||||
        helper_test_pickle_string(cPickle, protocol)
 | 
					        helper_test_pickle_string(cPickle, protocol)
 | 
				
			||||||
        helper_test_pickle_file(cPickle, protocol)
 | 
					        helper_test_pickle_file(cPickle, protocol)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def test_pickle_p_mode():
 | 
				
			||||||
 | 
					    # Arrange
 | 
				
			||||||
 | 
					    import pickle
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    # Act / Assert
 | 
				
			||||||
 | 
					    for file in [
 | 
				
			||||||
 | 
					            "Tests/images/test-card.png",
 | 
				
			||||||
 | 
					            "Tests/images/zero_bb.png",
 | 
				
			||||||
 | 
					            "Tests/images/zero_bb_scale2.png",
 | 
				
			||||||
 | 
					            "Tests/images/non_zero_bb.png",
 | 
				
			||||||
 | 
					            "Tests/images/non_zero_bb_scale2.png",
 | 
				
			||||||
 | 
					            "Tests/images/p_trns_single.png",
 | 
				
			||||||
 | 
					            "Tests/images/pil123p.png"
 | 
				
			||||||
 | 
					    ]:
 | 
				
			||||||
 | 
					        helper_test_pickle_string(pickle, file=file)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# End of file
 | 
					# End of file
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -242,7 +242,8 @@ def assert_image_equal(a, b, msg=None):
 | 
				
			||||||
        failure(msg or "got size %r, expected %r" % (a.size, b.size))
 | 
					        failure(msg or "got size %r, expected %r" % (a.size, b.size))
 | 
				
			||||||
    elif a.tobytes() != b.tobytes():
 | 
					    elif a.tobytes() != b.tobytes():
 | 
				
			||||||
        failure(msg or "got different content")
 | 
					        failure(msg or "got different content")
 | 
				
			||||||
        # generate better diff?
 | 
					    elif a != b:
 | 
				
			||||||
 | 
					        failure(msg or "images different")
 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        success()
 | 
					        success()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user