@@ -19,8 +19,8 @@ def create_thumbnail_heif(size):
1919 _ = Image .effect_mandelbrot (size , (- 3 , - 2.5 , 2 , 2.5 ), 100 )
2020 im_heif = pillow_heif .from_pillow (_ )
2121 im_heif .add_from_pillow (_ .crop ((0 , 0 , 256 , 256 )))
22- im_heif [0 ]. add_thumbnails ( boxes = [128 , 64 ])
23- im_heif [1 ]. add_thumbnails ( boxes = [128 , 64 ])
22+ pillow_heif . add_thumbnails ( im_heif [0 ], boxes = [128 , 64 ])
23+ pillow_heif . add_thumbnails ( im_heif [1 ], boxes = [128 , 64 ])
2424 im_heif .add_from_pillow (_ .crop ((0 , 0 , 192 , 192 )))
2525 _heif_buf = BytesIO ()
2626 exif = Image .Exif ()
@@ -216,12 +216,16 @@ def test_heif_thumbnail_references():
216216 ),
217217)
218218@pytest .mark .parametrize ("heif_file_buf" , (heif_buf , create_thumbnail_heif ((317 , 311 ))))
219- def test_heif_image_add_thumbs (thumbs , expected_after , heif_file_buf ):
219+ @pytest .mark .parametrize ("method" , ("HeifFile" , "HeifImage" ))
220+ def test_heif_add_thumbs (thumbs , expected_after , heif_file_buf , method ):
220221 output = BytesIO ()
221222 heif_file = pillow_heif .open_heif (heif_file_buf )
222- heif_file [0 ].add_thumbnails (thumbs )
223- heif_file [1 ].add_thumbnails (thumbs )
224- heif_file [2 ].add_thumbnails (thumbs )
223+ if method == "HeifFile" :
224+ pillow_heif .add_thumbnails (heif_file , thumbs )
225+ else :
226+ pillow_heif .add_thumbnails (heif_file [0 ], thumbs )
227+ pillow_heif .add_thumbnails (heif_file [1 ], thumbs )
228+ pillow_heif .add_thumbnails (heif_file [2 ], thumbs )
225229 heif_file .save (output , quality = - 1 )
226230 out_heif = pillow_heif .open_heif (output )
227231 for i in range (3 ):
@@ -232,25 +236,48 @@ def test_heif_image_add_thumbs(thumbs, expected_after, heif_file_buf):
232236@pytest .mark .parametrize (
233237 "thumbs,expected_after" ,
234238 (
235- (- 1 , 2 ),
236- (64 , 2 ),
237- (96 , 3 ),
238- ([96 ], 3 ),
239- ([0 , 84 ], 3 ),
240- ([96 , 84 ], 4 ),
239+ (- 1 , [2 , 2 , 0 ]),
240+ ([0 ], [2 , 2 , 0 ]),
241+ (64 , [2 , 2 , 1 ]),
242+ ([2048 ], [2 , 2 , 0 ]),
243+ (96 , [3 , 3 , 1 ]),
244+ ([0 , 84 ], [3 , 3 , 1 ]),
245+ ([96 , 84 ], [4 , 4 , 2 ]),
241246 ),
242247)
243- @pytest .mark .parametrize ("heif_file_buf" , (heif_buf , create_thumbnail_heif ((537 , 511 ))))
244- def test_heif_primary_add_thumbs (thumbs , expected_after , heif_file_buf ):
245- heif_file = pillow_heif .open_heif (heif_file_buf )
246- heif_file .add_thumbnails (thumbs )
248+ def test_pillow_add_thumbs (thumbs , expected_after ):
247249 output = BytesIO ()
248- heif_file .save (output , quality = - 1 )
250+ im = Image .open (heif_buf )
251+ for frame in ImageSequence .Iterator (im ):
252+ pillow_heif .add_thumbnails (frame , thumbs )
253+ im .save (output , format = "HEIF" , quality = - 1 , save_all = True )
254+ out_heif = pillow_heif .open_heif (output )
255+ for i in range (3 ):
256+ assert len (out_heif [i ].thumbnails ) == expected_after [i ]
257+ compare_hashes ([out_heif [0 ].to_pillow (), out_heif [0 ].thumbnails [0 ].to_pillow ()], hash_size = 8 , max_difference = 4 )
258+
259+
260+ @pytest .mark .parametrize (
261+ "thumbs,expected_after" ,
262+ (
263+ (- 1 , 0 ),
264+ ([0 ], 0 ),
265+ (64 , 1 ),
266+ ([2048 ], 0 ),
267+ ([0 , 128 ], 1 ),
268+ ([64 , 128 ], 2 ),
269+ ),
270+ )
271+ def test_pillow_add_thumbs_empty_info (thumbs , expected_after ):
272+ output = BytesIO ()
273+ im = Image .open (heif_buf )
274+ im .info .pop ("thumbnails" )
275+ pillow_heif .add_thumbnails (im , thumbs )
276+ im .save (output , format = "HEIF" , quality = - 1 )
249277 out_heif = pillow_heif .open_heif (output )
250- assert len (out_heif [0 ].thumbnails ) == len (heif_file [0 ].thumbnails )
251- assert len (out_heif [1 ].thumbnails ) == expected_after
252278 assert len (out_heif .thumbnails ) == expected_after
253- assert len (out_heif [2 ].thumbnails ) == len (heif_file [2 ].thumbnails )
279+ if expected_after >= 1 :
280+ compare_hashes ([out_heif [0 ].to_pillow (), out_heif .thumbnails [0 ].to_pillow ()], hash_size = 8 )
254281
255282
256283def test_heif_remove_thumbs ():
0 commit comments