11import pickle
22
3+ import pytest
34from PIL import Image
45
6+ from .helper import skip_unless_feature
57
6- def helper_pickle_file (tmp_path , pickle , protocol = 0 , mode = None ):
8+
9+ def helper_pickle_file (tmp_path , pickle , protocol , test_file , mode ):
710 # Arrange
8- with Image .open ("Tests/images/hopper.jpg" ) as im :
11+ with Image .open (test_file ) as im :
912 filename = str (tmp_path / "temp.pkl" )
1013 if mode :
1114 im = im .convert (mode )
@@ -20,9 +23,7 @@ def helper_pickle_file(tmp_path, pickle, protocol=0, mode=None):
2023 assert im == loaded_im
2124
2225
23- def helper_pickle_string (
24- pickle , protocol = 0 , test_file = "Tests/images/hopper.jpg" , mode = None
25- ):
26+ def helper_pickle_string (pickle , protocol , test_file , mode ):
2627 with Image .open (test_file ) as im :
2728 if mode :
2829 im = im .convert (mode )
@@ -35,41 +36,31 @@ def helper_pickle_string(
3536 assert im == loaded_im
3637
3738
38- def test_pickle_image (tmp_path ):
39+ @pytest .mark .parametrize (
40+ ("test_file" , "test_mode" ),
41+ [
42+ ("Tests/images/hopper.jpg" , None ),
43+ ("Tests/images/hopper.jpg" , "L" ),
44+ ("Tests/images/hopper.jpg" , "PA" ),
45+ pytest .param (
46+ "Tests/images/hopper.webp" , None , marks = skip_unless_feature ("webp" )
47+ ),
48+ ("Tests/images/hopper.tif" , None ),
49+ ("Tests/images/test-card.png" , None ),
50+ ("Tests/images/zero_bb.png" , None ),
51+ ("Tests/images/zero_bb_scale2.png" , None ),
52+ ("Tests/images/non_zero_bb.png" , None ),
53+ ("Tests/images/non_zero_bb_scale2.png" , None ),
54+ ("Tests/images/p_trns_single.png" , None ),
55+ ("Tests/images/pil123p.png" , None ),
56+ ("Tests/images/itxt_chunks.png" , None ),
57+ ],
58+ )
59+ def test_pickle_image (tmp_path , test_file , test_mode ):
3960 # Act / Assert
4061 for protocol in range (0 , pickle .HIGHEST_PROTOCOL + 1 ):
41- helper_pickle_string (pickle , protocol )
42- helper_pickle_file (tmp_path , pickle , protocol )
43-
44-
45- def test_pickle_p_mode ():
46- # Act / Assert
47- for test_file in [
48- "Tests/images/test-card.png" ,
49- "Tests/images/zero_bb.png" ,
50- "Tests/images/zero_bb_scale2.png" ,
51- "Tests/images/non_zero_bb.png" ,
52- "Tests/images/non_zero_bb_scale2.png" ,
53- "Tests/images/p_trns_single.png" ,
54- "Tests/images/pil123p.png" ,
55- "Tests/images/itxt_chunks.png" ,
56- ]:
57- for protocol in range (0 , pickle .HIGHEST_PROTOCOL + 1 ):
58- helper_pickle_string (pickle , protocol = protocol , test_file = test_file )
59-
60-
61- def test_pickle_pa_mode (tmp_path ):
62- # Act / Assert
63- for protocol in range (0 , pickle .HIGHEST_PROTOCOL + 1 ):
64- helper_pickle_string (pickle , protocol , mode = "PA" )
65- helper_pickle_file (tmp_path , pickle , protocol , mode = "PA" )
66-
67-
68- def test_pickle_l_mode (tmp_path ):
69- # Act / Assert
70- for protocol in range (0 , pickle .HIGHEST_PROTOCOL + 1 ):
71- helper_pickle_string (pickle , protocol , mode = "L" )
72- helper_pickle_file (tmp_path , pickle , protocol , mode = "L" )
62+ helper_pickle_string (pickle , protocol , test_file , test_mode )
63+ helper_pickle_file (tmp_path , pickle , protocol , test_file , test_mode )
7364
7465
7566def test_pickle_la_mode_with_palette (tmp_path ):
@@ -88,3 +79,15 @@ def test_pickle_la_mode_with_palette(tmp_path):
8879
8980 im .mode = "PA"
9081 assert im == loaded_im
82+
83+
84+ @skip_unless_feature ("webp" )
85+ def test_pickle_tell ():
86+ # Arrange
87+ image = Image .open ("Tests/images/hopper.webp" )
88+
89+ # Act: roundtrip
90+ unpickled_image = pickle .loads (pickle .dumps (image ))
91+
92+ # Assert
93+ assert unpickled_image .tell () == 0
0 commit comments