2014-07-07 21:03:50 +04:00
|
|
|
from helper import unittest, PillowTestCase
|
2014-04-22 09:54:16 +04:00
|
|
|
|
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
class TestPickle(PillowTestCase):
|
|
|
|
|
2015-01-30 15:00:46 +03:00
|
|
|
def helper_pickle_file(self, pickle, protocol=0, mode=None):
|
2014-09-15 09:59:28 +04:00
|
|
|
# Arrange
|
2014-09-05 14:03:56 +04:00
|
|
|
im = Image.open('Tests/images/hopper.jpg')
|
2014-09-15 09:59:28 +04:00
|
|
|
filename = self.tempfile('temp.pkl')
|
2015-01-30 15:00:46 +03:00
|
|
|
if mode:
|
|
|
|
im = im.convert(mode)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
# Act
|
2014-09-15 09:59:28 +04:00
|
|
|
with open(filename, 'wb') as f:
|
2014-06-10 13:10:47 +04:00
|
|
|
pickle.dump(im, f, protocol)
|
2014-09-15 09:59:28 +04:00
|
|
|
with open(filename, 'rb') as f:
|
2014-06-10 13:10:47 +04:00
|
|
|
loaded_im = pickle.load(f)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
self.assertEqual(im, loaded_im)
|
|
|
|
|
2015-01-30 15:00:46 +03:00
|
|
|
def helper_pickle_string(self, pickle, protocol=0,
|
2015-12-10 01:23:58 +03:00
|
|
|
test_file='Tests/images/hopper.jpg', mode=None):
|
|
|
|
im = Image.open(test_file)
|
2015-01-30 15:00:46 +03:00
|
|
|
if mode:
|
|
|
|
im = im.convert(mode)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
# Act
|
|
|
|
dumped_string = pickle.dumps(im, protocol)
|
|
|
|
loaded_im = pickle.loads(dumped_string)
|
|
|
|
|
|
|
|
# Assert
|
|
|
|
self.assertEqual(im, loaded_im)
|
|
|
|
|
|
|
|
def test_pickle_image(self):
|
|
|
|
# Arrange
|
|
|
|
import pickle
|
|
|
|
|
|
|
|
# Act / Assert
|
|
|
|
for protocol in range(0, pickle.HIGHEST_PROTOCOL + 1):
|
|
|
|
self.helper_pickle_string(pickle, protocol)
|
|
|
|
self.helper_pickle_file(pickle, protocol)
|
|
|
|
|
|
|
|
def test_cpickle_image(self):
|
|
|
|
# Arrange
|
|
|
|
try:
|
|
|
|
import cPickle
|
|
|
|
except ImportError:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Act / Assert
|
|
|
|
for protocol in range(0, cPickle.HIGHEST_PROTOCOL + 1):
|
|
|
|
self.helper_pickle_string(cPickle, protocol)
|
|
|
|
self.helper_pickle_file(cPickle, protocol)
|
|
|
|
|
|
|
|
def test_pickle_p_mode(self):
|
|
|
|
# Arrange
|
|
|
|
import pickle
|
|
|
|
|
|
|
|
# Act / Assert
|
2015-04-24 11:24:52 +03:00
|
|
|
for test_file in [
|
2014-06-10 13:10:47 +04:00
|
|
|
"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",
|
2015-09-28 22:24:45 +03:00
|
|
|
"Tests/images/pil123p.png",
|
|
|
|
"Tests/images/itxt_chunks.png"
|
2014-06-10 13:10:47 +04:00
|
|
|
]:
|
2015-12-10 01:23:58 +03:00
|
|
|
self.helper_pickle_string(pickle, test_file=test_file)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2015-01-30 15:00:46 +03:00
|
|
|
def test_pickle_l_mode(self):
|
|
|
|
# Arrange
|
|
|
|
import pickle
|
|
|
|
|
|
|
|
# Act / Assert
|
|
|
|
for protocol in range(0, pickle.HIGHEST_PROTOCOL + 1):
|
|
|
|
self.helper_pickle_string(pickle, protocol, mode="L")
|
|
|
|
self.helper_pickle_file(pickle, protocol, mode="L")
|
|
|
|
|
|
|
|
def test_cpickle_l_mode(self):
|
|
|
|
# Arrange
|
|
|
|
try:
|
|
|
|
import cPickle
|
|
|
|
except ImportError:
|
|
|
|
return
|
|
|
|
|
|
|
|
# Act / Assert
|
|
|
|
for protocol in range(0, cPickle.HIGHEST_PROTOCOL + 1):
|
|
|
|
self.helper_pickle_string(cPickle, protocol, mode="L")
|
|
|
|
self.helper_pickle_file(cPickle, protocol, mode="L")
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|