mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-10-31 07:57:27 +03:00 
			
		
		
		
	Restored filename in progress callback
This commit is contained in:
		
							parent
							
								
									3465a598e2
								
							
						
					
					
						commit
						b5f322831a
					
				|  | @ -669,11 +669,18 @@ def test_save_all_progress(): | |||
|     out = BytesIO() | ||||
|     progress = [] | ||||
| 
 | ||||
|     def callback(filename, frame_number, n_frames): | ||||
|         progress.append((filename, frame_number, n_frames)) | ||||
|     def callback(state): | ||||
|         progress.append(state) | ||||
| 
 | ||||
|     Image.new("RGB", (1, 1)).save(out, "PNG", save_all=True, progress=callback) | ||||
|     assert progress == [(0, 1, 1)] | ||||
|     assert progress == [ | ||||
|         { | ||||
|             "image_index": 0, | ||||
|             "image_filename": None, | ||||
|             "completed_frames": 1, | ||||
|             "total_frames": 1, | ||||
|         } | ||||
|     ] | ||||
| 
 | ||||
|     out = BytesIO() | ||||
|     progress = [] | ||||
|  | @ -685,13 +692,48 @@ def test_save_all_progress(): | |||
|             ) | ||||
| 
 | ||||
|     assert progress == [ | ||||
|         (0, 1, 7), | ||||
|         (1, 2, 7), | ||||
|         (2, 3, 7), | ||||
|         (2, 4, 7), | ||||
|         (2, 5, 7), | ||||
|         (2, 6, 7), | ||||
|         (2, 7, 7), | ||||
|         { | ||||
|             "image_index": 0, | ||||
|             "image_filename": "Tests/images/apng/single_frame.png", | ||||
|             "completed_frames": 1, | ||||
|             "total_frames": 7, | ||||
|         }, | ||||
|         { | ||||
|             "image_index": 1, | ||||
|             "image_filename": "Tests/images/apng/single_frame.png", | ||||
|             "completed_frames": 2, | ||||
|             "total_frames": 7, | ||||
|         }, | ||||
|         { | ||||
|             "image_index": 2, | ||||
|             "image_filename": "Tests/images/apng/delay.png", | ||||
|             "completed_frames": 3, | ||||
|             "total_frames": 7, | ||||
|         }, | ||||
|         { | ||||
|             "image_index": 2, | ||||
|             "image_filename": "Tests/images/apng/delay.png", | ||||
|             "completed_frames": 4, | ||||
|             "total_frames": 7, | ||||
|         }, | ||||
|         { | ||||
|             "image_index": 2, | ||||
|             "image_filename": "Tests/images/apng/delay.png", | ||||
|             "completed_frames": 5, | ||||
|             "total_frames": 7, | ||||
|         }, | ||||
|         { | ||||
|             "image_index": 2, | ||||
|             "image_filename": "Tests/images/apng/delay.png", | ||||
|             "completed_frames": 6, | ||||
|             "total_frames": 7, | ||||
|         }, | ||||
|         { | ||||
|             "image_index": 2, | ||||
|             "image_filename": "Tests/images/apng/delay.png", | ||||
|             "completed_frames": 7, | ||||
|             "total_frames": 7, | ||||
|         }, | ||||
|     ] | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
|  | @ -269,11 +269,18 @@ def test_save_all_progress(): | |||
|     out = BytesIO() | ||||
|     progress = [] | ||||
| 
 | ||||
|     def callback(filename, frame_number, n_frames): | ||||
|         progress.append((filename, frame_number, n_frames)) | ||||
|     def callback(state): | ||||
|         progress.append(state) | ||||
| 
 | ||||
|     Image.new("RGB", (1, 1)).save(out, "GIF", save_all=True, progress=callback) | ||||
|     assert progress == [(0, 1, 1)] | ||||
|     assert progress == [ | ||||
|         { | ||||
|             "image_index": 0, | ||||
|             "image_filename": None, | ||||
|             "completed_frames": 1, | ||||
|             "total_frames": 1, | ||||
|         } | ||||
|     ] | ||||
| 
 | ||||
|     out = BytesIO() | ||||
|     progress = [] | ||||
|  | @ -282,9 +289,23 @@ def test_save_all_progress(): | |||
|         with Image.open("Tests/images/chi.gif") as im2: | ||||
|             im.save(out, "GIF", save_all=True, append_images=[im2], progress=callback) | ||||
| 
 | ||||
|     expected = [(0, 1, 32)] | ||||
|     expected = [ | ||||
|         { | ||||
|             "image_index": 0, | ||||
|             "image_filename": "Tests/images/hopper.gif", | ||||
|             "completed_frames": 1, | ||||
|             "total_frames": 32, | ||||
|         } | ||||
|     ] | ||||
|     for i in range(31): | ||||
|         expected.append((1, i + 2, 32)) | ||||
|         expected.append( | ||||
|             { | ||||
|                 "image_index": 1, | ||||
|                 "image_filename": "Tests/images/chi.gif", | ||||
|                 "completed_frames": i + 2, | ||||
|                 "total_frames": 32, | ||||
|             } | ||||
|         ) | ||||
|     assert progress == expected | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
|  | @ -284,11 +284,18 @@ def test_save_all_progress(): | |||
|     out = BytesIO() | ||||
|     progress = [] | ||||
| 
 | ||||
|     def callback(filename, frame_number, n_frames): | ||||
|         progress.append((filename, frame_number, n_frames)) | ||||
|     def callback(state): | ||||
|         progress.append(state) | ||||
| 
 | ||||
|     Image.new("RGB", (1, 1)).save(out, "MPO", save_all=True, progress=callback) | ||||
|     assert progress == [(0, 1, 1)] | ||||
|     assert progress == [ | ||||
|         { | ||||
|             "image_index": 0, | ||||
|             "image_filename": None, | ||||
|             "completed_frames": 1, | ||||
|             "total_frames": 1, | ||||
|         } | ||||
|     ] | ||||
| 
 | ||||
|     out = BytesIO() | ||||
|     progress = [] | ||||
|  | @ -298,8 +305,28 @@ def test_save_all_progress(): | |||
|             im.save(out, "MPO", save_all=True, append_images=[im2], progress=callback) | ||||
| 
 | ||||
|     assert progress == [ | ||||
|         (0, 1, 4), | ||||
|         (0, 2, 4), | ||||
|         (1, 3, 4), | ||||
|         (1, 4, 4), | ||||
|         { | ||||
|             "image_index": 0, | ||||
|             "image_filename": "Tests/images/sugarshack.mpo", | ||||
|             "completed_frames": 1, | ||||
|             "total_frames": 4, | ||||
|         }, | ||||
|         { | ||||
|             "image_index": 0, | ||||
|             "image_filename": "Tests/images/sugarshack.mpo", | ||||
|             "completed_frames": 2, | ||||
|             "total_frames": 4, | ||||
|         }, | ||||
|         { | ||||
|             "image_index": 1, | ||||
|             "image_filename": "Tests/images/frozenpond.mpo", | ||||
|             "completed_frames": 3, | ||||
|             "total_frames": 4, | ||||
|         }, | ||||
|         { | ||||
|             "image_index": 1, | ||||
|             "image_filename": "Tests/images/frozenpond.mpo", | ||||
|             "completed_frames": 4, | ||||
|             "total_frames": 4, | ||||
|         }, | ||||
|     ] | ||||
|  |  | |||
|  | @ -173,11 +173,18 @@ def test_save_all_progress(): | |||
|     out = BytesIO() | ||||
|     progress = [] | ||||
| 
 | ||||
|     def callback(filename, frame_number, n_frames): | ||||
|         progress.append((filename, frame_number, n_frames)) | ||||
|     def callback(state): | ||||
|         progress.append(state) | ||||
| 
 | ||||
|     Image.new("RGB", (1, 1)).save(out, "PDF", save_all=True, progress=callback) | ||||
|     assert progress == [(0, 1, 1)] | ||||
|     assert progress == [ | ||||
|         { | ||||
|             "image_index": 0, | ||||
|             "image_filename": None, | ||||
|             "completed_frames": 1, | ||||
|             "total_frames": 1, | ||||
|         } | ||||
|     ] | ||||
| 
 | ||||
|     out = BytesIO() | ||||
|     progress = [] | ||||
|  | @ -187,10 +194,30 @@ def test_save_all_progress(): | |||
|             im.save(out, "PDF", save_all=True, append_images=[im2], progress=callback) | ||||
| 
 | ||||
|     assert progress == [ | ||||
|         (0, 1, 4), | ||||
|         (0, 2, 4), | ||||
|         (1, 3, 4), | ||||
|         (1, 4, 4), | ||||
|         { | ||||
|             "image_index": 0, | ||||
|             "image_filename": "Tests/images/sugarshack.mpo", | ||||
|             "completed_frames": 1, | ||||
|             "total_frames": 4, | ||||
|         }, | ||||
|         { | ||||
|             "image_index": 0, | ||||
|             "image_filename": "Tests/images/sugarshack.mpo", | ||||
|             "completed_frames": 2, | ||||
|             "total_frames": 4, | ||||
|         }, | ||||
|         { | ||||
|             "image_index": 1, | ||||
|             "image_filename": "Tests/images/frozenpond.mpo", | ||||
|             "completed_frames": 3, | ||||
|             "total_frames": 4, | ||||
|         }, | ||||
|         { | ||||
|             "image_index": 1, | ||||
|             "image_filename": "Tests/images/frozenpond.mpo", | ||||
|             "completed_frames": 4, | ||||
|             "total_frames": 4, | ||||
|         }, | ||||
|     ] | ||||
| 
 | ||||
| 
 | ||||
|  |  | |||
|  | @ -692,11 +692,18 @@ class TestFileTiff: | |||
|         out = BytesIO() | ||||
|         progress = [] | ||||
| 
 | ||||
|         def callback(filename, frame_number, n_frames): | ||||
|             progress.append((filename, frame_number, n_frames)) | ||||
|         def callback(state): | ||||
|             progress.append(state) | ||||
| 
 | ||||
|         Image.new("RGB", (1, 1)).save(out, "TIFF", save_all=True, progress=callback) | ||||
|         assert progress == [(0, 1, 1)] | ||||
|         assert progress == [ | ||||
|             { | ||||
|                 "image_index": 0, | ||||
|                 "image_filename": None, | ||||
|                 "completed_frames": 1, | ||||
|                 "total_frames": 1, | ||||
|             } | ||||
|         ] | ||||
| 
 | ||||
|         out = BytesIO() | ||||
|         progress = [] | ||||
|  | @ -708,10 +715,30 @@ class TestFileTiff: | |||
|                 ) | ||||
| 
 | ||||
|         assert progress == [ | ||||
|             (0, 1, 4), | ||||
|             (1, 2, 4), | ||||
|             (1, 3, 4), | ||||
|             (1, 4, 4), | ||||
|             { | ||||
|                 "image_index": 0, | ||||
|                 "image_filename": "Tests/images/hopper.tif", | ||||
|                 "completed_frames": 1, | ||||
|                 "total_frames": 4, | ||||
|             }, | ||||
|             { | ||||
|                 "image_index": 1, | ||||
|                 "image_filename": "Tests/images/multipage.tiff", | ||||
|                 "completed_frames": 2, | ||||
|                 "total_frames": 4, | ||||
|             }, | ||||
|             { | ||||
|                 "image_index": 1, | ||||
|                 "image_filename": "Tests/images/multipage.tiff", | ||||
|                 "completed_frames": 3, | ||||
|                 "total_frames": 4, | ||||
|             }, | ||||
|             { | ||||
|                 "image_index": 1, | ||||
|                 "image_filename": "Tests/images/multipage.tiff", | ||||
|                 "completed_frames": 4, | ||||
|                 "total_frames": 4, | ||||
|             }, | ||||
|         ] | ||||
| 
 | ||||
|     def test_saving_icc_profile(self, tmp_path): | ||||
|  |  | |||
|  | @ -127,11 +127,18 @@ class TestFileWebp: | |||
|         out = BytesIO() | ||||
|         progress = [] | ||||
| 
 | ||||
|         def callback(filename, frame_number, n_frames): | ||||
|             progress.append((filename, frame_number, n_frames)) | ||||
|         def callback(state): | ||||
|             progress.append(state) | ||||
| 
 | ||||
|         Image.new("RGB", (1, 1)).save(out, "WEBP", save_all=True, progress=callback) | ||||
|         assert progress == [(0, 1, 1)] | ||||
|         assert progress == [ | ||||
|             { | ||||
|                 "image_index": 0, | ||||
|                 "image_filename": None, | ||||
|                 "completed_frames": 1, | ||||
|                 "total_frames": 1, | ||||
|             } | ||||
|         ] | ||||
| 
 | ||||
|         out = BytesIO() | ||||
|         progress = [] | ||||
|  | @ -142,8 +149,22 @@ class TestFileWebp: | |||
| 
 | ||||
|         expected = [] | ||||
|         for i in range(42): | ||||
|             expected.append((0, i + 1, 43)) | ||||
|         expected.append((1, 43, 43)) | ||||
|             expected.append( | ||||
|                 { | ||||
|                     "image_index": 0, | ||||
|                     "image_filename": "Tests/images/iss634.webp", | ||||
|                     "completed_frames": i + 1, | ||||
|                     "total_frames": 43, | ||||
|                 } | ||||
|             ) | ||||
|         expected.append( | ||||
|             { | ||||
|                 "image_index": 1, | ||||
|                 "image_filename": None, | ||||
|                 "completed_frames": 43, | ||||
|                 "total_frames": 43, | ||||
|             } | ||||
|         ) | ||||
|         assert progress == expected | ||||
| 
 | ||||
|     def test_icc_profile(self, tmp_path): | ||||
|  |  | |||
|  | @ -618,7 +618,14 @@ def _write_multiple_frames(im, fp, palette): | |||
|                     if encoderinfo.get("duration"): | ||||
|                         previous["encoderinfo"]["duration"] += encoderinfo["duration"] | ||||
|                     if progress: | ||||
|                         progress(i, frame_count, n_frames) | ||||
|                         progress( | ||||
|                             { | ||||
|                                 "image_index": i, | ||||
|                                 "image_filename": getattr(imSequence, "filename", None), | ||||
|                                 "completed_frames": frame_count, | ||||
|                                 "total_frames": n_frames, | ||||
|                             } | ||||
|                         ) | ||||
|                     continue | ||||
|                 if encoderinfo.get("disposal") == 2: | ||||
|                     if background_im is None: | ||||
|  | @ -633,7 +640,14 @@ def _write_multiple_frames(im, fp, palette): | |||
|                 bbox = None | ||||
|             im_frames.append({"im": im_frame, "bbox": bbox, "encoderinfo": encoderinfo}) | ||||
|             if progress: | ||||
|                 progress(i, frame_count, n_frames) | ||||
|                 progress( | ||||
|                     { | ||||
|                         "image_index": i, | ||||
|                         "image_filename": getattr(imSequence, "filename", None), | ||||
|                         "completed_frames": frame_count, | ||||
|                         "total_frames": n_frames, | ||||
|                     } | ||||
|                 ) | ||||
| 
 | ||||
|     if len(im_frames) > 1: | ||||
|         for frame_data in im_frames: | ||||
|  |  | |||
|  | @ -51,7 +51,14 @@ def _save_all(im, fp, filename): | |||
|         if not animated: | ||||
|             _save(im, fp, filename) | ||||
|             if progress: | ||||
|                 progress(0, 1, 1) | ||||
|                 progress( | ||||
|                     { | ||||
|                         "image_index": 0, | ||||
|                         "image_filename": getattr(im, "filename", None), | ||||
|                         "completed_frames": 1, | ||||
|                         "total_frames": 1, | ||||
|                     } | ||||
|                 ) | ||||
|             return | ||||
| 
 | ||||
|     mpf_offset = 28 | ||||
|  | @ -83,7 +90,14 @@ def _save_all(im, fp, filename): | |||
|                 offsets.append(fp.tell() - offsets[-1]) | ||||
|             if progress: | ||||
|                 frame_number += 1 | ||||
|                 progress(i, frame_number, n_frames) | ||||
|                 progress( | ||||
|                     { | ||||
|                         "image_index": i, | ||||
|                         "image_filename": getattr(imSequence, "filename", None), | ||||
|                         "completed_frames": frame_number, | ||||
|                         "total_frames": n_frames, | ||||
|                     } | ||||
|                 ) | ||||
| 
 | ||||
|     ifd = TiffImagePlugin.ImageFileDirectory_v2() | ||||
|     ifd[0xB000] = b"0100" | ||||
|  |  | |||
|  | @ -283,7 +283,14 @@ def _save(im, fp, filename, save_all=False): | |||
| 
 | ||||
|             page_number += 1 | ||||
|             if progress: | ||||
|                 progress(i, page_number, number_of_pages) | ||||
|                 progress( | ||||
|                     { | ||||
|                         "image_index": i, | ||||
|                         "image_filename": getattr(im_sequence, "filename", None), | ||||
|                         "completed_frames": page_number, | ||||
|                         "total_frames": number_of_pages, | ||||
|                     } | ||||
|                 ) | ||||
| 
 | ||||
|     # | ||||
|     # trailer | ||||
|  |  | |||
|  | @ -1155,7 +1155,14 @@ def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images) | |||
|                         "duration", duration | ||||
|                     ) | ||||
|                     if progress: | ||||
|                         progress(i, frame_count, n_frames) | ||||
|                         progress( | ||||
|                             { | ||||
|                                 "image_index": i, | ||||
|                                 "image_filename": getattr(imSequence, "filename", None), | ||||
|                                 "completed_frames": frame_count, | ||||
|                                 "total_frames": n_frames, | ||||
|                             } | ||||
|                         ) | ||||
|                     continue | ||||
|             else: | ||||
|                 bbox = None | ||||
|  | @ -1163,7 +1170,14 @@ def _write_multiple_frames(im, fp, chunk, rawmode, default_image, append_images) | |||
|                 encoderinfo["duration"] = duration | ||||
|             im_frames.append({"im": im_frame, "bbox": bbox, "encoderinfo": encoderinfo}) | ||||
|             if progress: | ||||
|                 progress(i, frame_count, n_frames) | ||||
|                 progress( | ||||
|                     { | ||||
|                         "image_index": i, | ||||
|                         "image_filename": getattr(imSequence, "filename", None), | ||||
|                         "completed_frames": frame_count, | ||||
|                         "total_frames": n_frames, | ||||
|                     } | ||||
|                 ) | ||||
| 
 | ||||
|     # animation control | ||||
|     chunk( | ||||
|  |  | |||
|  | @ -2122,7 +2122,14 @@ def _save_all(im, fp, filename): | |||
|     if not hasattr(im, "n_frames") and not append_images: | ||||
|         _save(im, fp, filename) | ||||
|         if progress: | ||||
|             progress(0, 1, 1) | ||||
|             progress( | ||||
|                 { | ||||
|                     "image_index": 0, | ||||
|                     "image_filename": getattr(im, "filename", None), | ||||
|                     "completed_frames": 1, | ||||
|                     "total_frames": 1, | ||||
|                 } | ||||
|             ) | ||||
|         return | ||||
| 
 | ||||
|     cur_idx = im.tell() | ||||
|  | @ -2148,7 +2155,14 @@ def _save_all(im, fp, filename): | |||
|                     _save(ims, tf, filename) | ||||
|                     if progress: | ||||
|                         frame_number += 1 | ||||
|                         progress(i, frame_number, n_frames) | ||||
|                         progress( | ||||
|                             { | ||||
|                                 "image_index": i, | ||||
|                                 "image_filename": getattr(ims, "filename", None), | ||||
|                                 "completed_frames": frame_number, | ||||
|                                 "total_frames": n_frames, | ||||
|                             } | ||||
|                         ) | ||||
| 
 | ||||
|                     tf.newFrame() | ||||
|     finally: | ||||
|  |  | |||
|  | @ -188,7 +188,14 @@ def _save_all(im, fp, filename): | |||
|     if total == 1: | ||||
|         _save(im, fp, filename) | ||||
|         if progress: | ||||
|             progress(0, 1, 1) | ||||
|             progress( | ||||
|                 { | ||||
|                     "image_index": 0, | ||||
|                     "image_filename": getattr(im, "filename", None), | ||||
|                     "completed_frames": 1, | ||||
|                     "total_frames": 1, | ||||
|                 } | ||||
|             ) | ||||
|         return | ||||
| 
 | ||||
|     background = (0, 0, 0, 0) | ||||
|  | @ -304,7 +311,14 @@ def _save_all(im, fp, filename): | |||
|                     timestamp += duration | ||||
|                 frame_idx += 1 | ||||
|                 if progress: | ||||
|                     progress(i, frame_idx, total) | ||||
|                     progress( | ||||
|                         { | ||||
|                             "image_index": i, | ||||
|                             "image_filename": getattr(ims, "filename", None), | ||||
|                             "completed_frames": frame_idx, | ||||
|                             "total_frames": total, | ||||
|                         } | ||||
|                     ) | ||||
| 
 | ||||
|     finally: | ||||
|         im.seek(cur_idx) | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue
	
	Block a user