Pillow/Tests/test_pickle.py

90 lines
2.9 KiB
Python
Raw Normal View History

import pickle
from PIL import Image
from .helper import PillowTestCase
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
2019-11-25 23:03:23 +03:00
with Image.open("Tests/images/hopper.jpg") as im:
filename = self.tempfile("temp.pkl")
if mode:
im = im.convert(mode)
2014-06-10 13:10:47 +04:00
2019-11-25 23:03:23 +03:00
# Act
with open(filename, "wb") as f:
pickle.dump(im, f, protocol)
with open(filename, "rb") as f:
loaded_im = pickle.load(f)
2014-06-10 13:10:47 +04:00
2019-11-25 23:03:23 +03:00
# Assert
self.assertEqual(im, loaded_im)
2014-06-10 13:10:47 +04:00
2019-06-13 18:54:46 +03:00
def helper_pickle_string(
self, pickle, protocol=0, test_file="Tests/images/hopper.jpg", mode=None
):
2019-11-25 23:03:23 +03:00
with Image.open(test_file) as im:
if mode:
im = im.convert(mode)
2014-06-10 13:10:47 +04:00
2019-11-25 23:03:23 +03:00
# Act
dumped_string = pickle.dumps(im, protocol)
loaded_im = pickle.loads(dumped_string)
2014-06-10 13:10:47 +04:00
2019-11-25 23:03:23 +03:00
# Assert
self.assertEqual(im, loaded_im)
2014-06-10 13:10:47 +04:00
def test_pickle_image(self):
# 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_pickle_p_mode(self):
# Act / Assert
2015-04-24 11:24:52 +03:00
for test_file in [
2019-06-13 18:54:46 +03: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",
"Tests/images/pil123p.png",
"Tests/images/itxt_chunks.png",
2014-06-10 13:10:47 +04:00
]:
2015-09-29 08:36:07 +03:00
for protocol in range(0, pickle.HIGHEST_PROTOCOL + 1):
2019-06-13 18:54:46 +03:00
self.helper_pickle_string(
pickle, protocol=protocol, test_file=test_file
)
2014-06-10 13:10:47 +04:00
def test_pickle_pa_mode(self):
# Act / Assert
for protocol in range(0, pickle.HIGHEST_PROTOCOL + 1):
self.helper_pickle_string(pickle, protocol, mode="PA")
self.helper_pickle_file(pickle, protocol, mode="PA")
2015-01-30 15:00:46 +03:00
def test_pickle_l_mode(self):
# 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_pickle_la_mode_with_palette(self):
# Arrange
2019-06-13 18:54:46 +03:00
filename = self.tempfile("temp.pkl")
2019-11-25 23:03:23 +03:00
with Image.open("Tests/images/hopper.jpg") as im:
im = im.convert("PA")
# Act / Assert
for protocol in range(0, pickle.HIGHEST_PROTOCOL + 1):
im.mode = "LA"
2019-06-13 18:54:46 +03:00
with open(filename, "wb") as f:
pickle.dump(im, f, protocol)
2019-06-13 18:54:46 +03:00
with open(filename, "rb") as f:
loaded_im = pickle.load(f)
im.mode = "PA"
self.assertEqual(im, loaded_im)